1748170 Members
4060 Online
108758 Solutions
New Discussion юеВ

Basic perl script

 
SOLVED
Go to solution
Steve_617
Advisor

Basic perl script

Hi there, I am trying to write a script using perl, and need some help.
I have a mrtg.cfg file that I would like to build a web page from.

My problem is I am trying to find a quick way to select 2 fields that I want to convert as a url. The fields that I am interested are those that start with Target, which is the name of the URL that I want to point to and the Description is the name that I would like to give that link.

Can someone maybe just show me how to pull those values into a variable or array, and I should be able to go on from there.

I have attached the input file . This is what I have tried so far.


#! /usr/bin/perl
open FI, "#open FO, ">>/var/apache/htdocs/mrtgweb.html;
while ($line=)
{
#@LIST=(split/;/,($line));
@LIST=($line);
print @LIST[0];
print @LIST[1];
sleep 1;
}
exit;
~

10 REPLIES 10
Marcin Golembski_1
Honored Contributor

Re: Basic perl script

open FI, "while ()
{
if ($_ =~ /Description/ || $_ =~ /Target/) {
@LIST=split/:/;
print "$LIST[0]\n";
print "$LIST[1]\n";
}
}
close FI;
Steve_617
Advisor

Re: Basic perl script

Ok I thought I would be able to continue.
I am trying to get rid of the brackets around
list 0 Target[10.79.192.254_10.79.230.62]

I must convert that line to look like
10.79.192.254_10.79.230.62.html

What I tried to do was
#! /usr/bin/perl
if ($_ =~ /Description/ || $_ =~ /Target/) {
@LIST=split/:/;
#$ULINK = $LIST[0];
$ULINK = split { '\[\]' , $LIST[0]};
$UNAME = $LIST[2];

#print "list 1 $LIST[1]\n";
print "list 0 $LIST[0]\n";
print "list 2 $LIST[2]\n";
print "link is $ULINK\n" ;
print "name is $UNAME\n" ;
print "\n\n\n";
}
}
close FI;
H.Merijn Brand (procura
Honored Contributor

Re: Basic perl script

> I am trying to get rid of the brackets
> around
> list 0 Target[10.79.192.254_10.79.230.62]
>
> I must convert that line to look like
> 10.79.192.254_10.79.230.62.html

s{.*\bTarget\s*\[([^]+)\]}{$1.html};

or

s{.*Target\s*\[\s*}{} && s{\s*\].*}{.html};

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Marcin Golembski_1
Honored Contributor

Re: Basic perl script

if($_ =~ /Target/) {
@LIST=split/:/;
$LIST[0] =~ s/.*Target\[(.*)\]/$1/;
$LIST[0] .= ".html";
print "$LIST[0]\n";
}
Marcin Golembski_1
Honored Contributor

Re: Basic perl script

if($_ =~ /Target/) {
@LIST=split/:/;
$LIST[0] =~ s/.*Target\[(.*)\]/$1\.html/;
print "$LIST[0]\n";
}
Hein van den Heuvel
Honored Contributor

Re: Basic perl script

Here are some regexprs which my help.

while (<>) {
next if /^\s*#/; # skip comment lines
$ip = $1 if /Target\[([0-9._]+)\]/;
if (/Desc.*TD>(.*)<\/TD/) {
$desc = $1;
print "ip = $ip, desc = $desc\n";
}
}

Sample output:

ip = 10.79.192.254_10.79.230.58, desc = GigabitEthernet0/3

Enjoy
Hein.
Doug O'Leary
Honored Contributor
Solution

Re: Basic perl script

Hey;

I'd probably do it as follows:

#!/usr/bin/perl

open FI, $ARGV[0];
my @lines= ;
my $lines = join('', @lines);

while ($lines =~ m{^Target(.*?)}mgs)
{ my $stanza = $1;
my ($url) = $stanza =~ m{\[(.*?)\].*};
my ($desc) = $stanza =~ m{.*Description:(.*?).*};
print "URL: $url.html\n";
print "Desc: $desc\n";
print "-" x 40 . "\n";
}

Sample output, using your data file:
URL: 10.79.192.254_10.255.200.1.html
Desc: GigabitEthernet0/3
----------------------------------------
URL: 10.79.192.254_10.79.226.174.html
Desc: Serial4/0 CXMM City Deep 370707-00004-01
----------------------------------------
URL: 10.79.192.254_10.79.230.58.html
Desc: Serial3/0:17 Welgedaght Hostel Cct number 280707-03942-01
----------------------------------------


------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
H.Merijn Brand (procura
Honored Contributor

Re: Basic perl script

open FI, $ARGV[0];
my @lines= ;
my $lines = join('', @lines);

as FI is in ARGV, this is easier written using

my $lines = join " ", <>;

if FI's name was not in @ARGV, you can fake that

my $lines;
{ local @ARGV = ("file.name");
$lines = join " ", <>;
}

Whatever of the above is used, the remaining $lines still contains all the newlines

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve_617
Advisor

Re: Basic perl script

Hi Guys
Thanks for all your input. My script is working, and I am sharing it. Still a novice, so any suggestions and enhancements from you guys would help.
Thanks again for everybody's input

#!/usr/bin/perl

unlink "/var/apache/htdocs/tmp1.html";

@FILES=`ls /var/apache/htdocs/cfg/*.cfg`;
foreach $FILE (@FILES) {
print ($FILE);

open FO, ">> /var/apache/htdocs/tmp1.html" or die;
open FI, "<$FILE" or die;
#open FI, "#open FI, $ARGV[0];
my @lines= ;
my $lines = join('', @lines);

my @NAMER=split(/\//, $FILE);
print " $NAMER[5] \n";
my @NAMER1=split('.cfg', $NAMER[5]);

print "printing file @NAMER \n";
print " four $NAMER1[4] \n";
print " zero $NAMER1[0] \n";
print " one $NAMER1[1] \n";
#

print FO "
";

print FO "";
print FO "";
print FO " Welcome to Infoman Report CEntre ";
print FO "<meta http-equiv="refresh" content="60#;" url="http://196.9.207.23/tmp1.html" />";
print FO " < circle="" outside="">" ;
#echo \
\$spoavg\

\

\
\
>>/tmp/tmp200
print FO " MRTG Statistics for Distribution $NAMER1[0] ";
print FO "
\n";
print FO "";
print FO "";
print FO "
";
print FO "";

while ($lines =~ m{^Target(.*?)}mgs)
{ my $stanza = $1;
my ($url) = $stanza =~ m{\[(.*?)\].*};
my ($desc) = $stanza =~ m{.*Description:(.*?).*};
# print FO "URL: $url.html\n";
# print FO "Desc: $desc\n";
print FO " Desc: $desc\n";
print FO "
\n";
print "-" x 40 . "\n";
}

}

print FO "";
close FO;
exit;