- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Basic perl script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2006 06:44 PM
тАО06-21-2006 06:44 PM
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;
~
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2006 07:32 PM
тАО06-21-2006 07:32 PM
Re: Basic perl script
{
if ($_ =~ /Description/ || $_ =~ /Target/) {
@LIST=split/:/;
print "$LIST[0]\n";
print "$LIST[1]\n";
}
}
close FI;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 12:42 AM
тАО06-22-2006 12:42 AM
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
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 01:18 AM
тАО06-22-2006 01:18 AM
Re: Basic perl script
> 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 01:28 AM
тАО06-22-2006 01:28 AM
Re: Basic perl script
@LIST=split/:/;
$LIST[0] =~ s/.*Target\[(.*)\]/$1/;
$LIST[0] .= ".html";
print "$LIST[0]\n";
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 01:31 AM
тАО06-22-2006 01:31 AM
Re: Basic perl script
@LIST=split/:/;
$LIST[0] =~ s/.*Target\[(.*)\]/$1\.html/;
print "$LIST[0]\n";
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 01:37 AM
тАО06-22-2006 01:37 AM
Re: Basic perl script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 01:50 AM
тАО06-22-2006 01:50 AM
SolutionI'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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 01:59 AM
тАО06-22-2006 01:59 AM
Re: Basic perl script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 08:44 PM
тАО06-22-2006 08:44 PM
Re: Basic perl script
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 "
print FO "<meta http-equiv="refresh" content="60#;" url="http://196.9.207.23/tmp1.html" />";
print FO " < circle="" outside="">" ;
#echo \
\$spoavg\\
\\
>>/tmp/tmp200print 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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2006 09:16 PM
тАО06-22-2006 09:16 PM
Re: Basic perl script
--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;
<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