Basic perl script (302 Views)
Reply
Advisor
Steve_617
Posts: 31
Registered: ‎04-04-2005
Message 1 of 11 (302 Views)
Accepted Solution

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;
~

Honored Contributor
Marcin Golembski_1
Posts: 634
Registered: ‎11-06-2001
Message 2 of 11 (302 Views)

Re: Basic perl script

open FI, "while ()
{
if ($_ =~ /Description/ || $_ =~ /Target/) {
@LIST=split/:/;
print "$LIST[0]\n";
print "$LIST[1]\n";
}
}
close FI;
Advisor
Steve_617
Posts: 31
Registered: ‎04-04-2005
Message 3 of 11 (302 Views)

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;
Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 4 of 11 (302 Views)

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
Honored Contributor
Marcin Golembski_1
Posts: 634
Registered: ‎11-06-2001
Message 5 of 11 (302 Views)

Re: Basic perl script

if($_ =~ /Target/) {
@LIST=split/:/;
$LIST[0] =~ s/.*Target\[(.*)\]/$1/;
$LIST[0] .= ".html";
print "$LIST[0]\n";
}
Honored Contributor
Marcin Golembski_1
Posts: 634
Registered: ‎11-06-2001
Message 6 of 11 (302 Views)

Re: Basic perl script

if($_ =~ /Target/) {
@LIST=split/:/;
$LIST[0] =~ s/.*Target\[(.*)\]/$1\.html/;
print "$LIST[0]\n";
}
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 7 of 11 (302 Views)

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.
Honored Contributor
Doug O'Leary
Posts: 815
Registered: ‎05-25-2005
Message 8 of 11 (302 Views)

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
Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 9 of 11 (302 Views)

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
Advisor
Steve_617
Posts: 31
Registered: ‎04-04-2005
Message 10 of 11 (302 Views)

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;
Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 11 of 11 (302 Views)

Re: Basic perl script

Just How I would write it ...

--8<---
#!/usr/bin/perl

use strict; # Never write without it
use warnings; # Shows you what you wanted to know

my $html_file = "/var/apache/htdocs/tmp1.html";
unlink $html_file;
open my $html, ">", $html_file or die "$html_file: $!";

foreach my $ifile (glob "/var/apache/htdocs/cfg/*.cfg") {
print STDERR "$ifile ...\n";
local (@ARGV, $/) = ($ifile);
$lines = <>;

(my $dist_name = $ifile) =~ s{.*/[^/]+.cfg$}{$1};

print $html << EOH;


Welcome to Infoman Report Centre
<meta http-equiv="refresh" content="60#" />
< circle="" outside="">
MRTG Statistics for Distribution $dist_name




EOH

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

print $html "\n\n";
close $html;
-->8---

Though I have serious doubt about this whole script.

I have already altered you HTML generation, as it was very wrong. Mine still isn't w3c compliant, but I have removed several parts already.

You are generating a single html file with several starts, each had two calls in your original,
inside a section and much more.

Have a look at the final outcome, and see if this is really what you want. I hope it's not.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.