Since I'm not able to test the code myself, I was expecting you to actually try to read it first, before running it. Just because you don't know much about programming now, this doesn't mean you can't start to learn a little about programming...
The line cited in the error message is the place where you have to insert some code yourself, which will translate the host name into an IP address (this is mentioned in the comment next to that line). Do you not know how you are getting the IP address for a given host name in your existing script?
Let's suppose you use a unix command like "host" to get an IP address -- e.g., on my macosx/BSD box, looking up the IP for perlmonks.org goes like this:
$ host www.perlmonks.org
www.perlmonks.org is an alias for perlmonks.org.
perlmonks.org has address 209.197.123.153
perlmonks.org has address 66.39.54.27
Probably you'll only get one IP per host in your list, and in any case, the first IP you get will probably work. So replace that line causing the error with something like these two lines:
my $ip = `host $host`;
$ip =~ s/.*?(\d+\.\d+\.\d+\.\d+).*/$1/s;
Now, if that fixes the syntax error but other problems still remain, you can just keep popping back for more corrections (which will annoy the rest of us), or you can start looking things up on your own, and see if you can figure out what to try next. | [reply] [d/l] [select] |
Oops. I forgot one thing...
How can I get the name of the node being walked to display for each port or at least at the beginning of the ports for that node? I will use the script to walk against a lot of devices and need to be able to differentiate between them. I'm guessing it has something to do with the chomp statement.
Here's what I have for the script:
#!/usr/bin/perl
use strict;
# make sure we can open input and output files
open( H, "/opt/home/johnsonr/scripts/nodes" ) or die "/opt/home/johnso
+nr/scripts/nodes: $!";
open( O, ">output.csv" ) or die "output.csv: $!";
# read host list
my @hosts = <H>;
close H;
chomp @hosts; # remove "\r\n" from ends of lines
my @iplist;
for my $host ( @hosts ) {
my $ip = `getent hosts $host`;
$ip =~ s/.*?(\d+\.\d+\.\d+\.\d+).*/$1/s;
# do whatever you do to get $ip for $host (and chomp if needed)
push @iplist, $ip;
}
my %ipdata;
my @infolist = qw/ifDescr ifOperStatus ifAdminStatus ifLastChange/;
my $cmdarg = "VER143r interfaces.ifTable.ifEntry."; # UPDATED to inclu
+de final "."
for my $ip ( @iplist ) {
for my $info ( @infolist ) {
my @pdata = `snmpwalk $ip $cmdarg$info`;
chomp @pdata; # remove line-terminations
s/.* = // for (@pdata); # remove redundant text
$ipdata{$ip}{$info} = [ @pdata ]; # create array ref in the ha
+sh structure
$ipdata{$ip}{lastport} = $#pdata; # keep track of how many por
+ts
}
}
# now print the csv data
for my $ip ( @iplist ) {
for my $portid ( 0 .. $ipdata{$ip}{lastport} ) {
my $outstr = '';
for my $info ( @infolist ) {
$outstr .= '"' . $ipdata{$ip}{$info}[$portid] . '",';
}
$outstr =~ s/,$/\n/; # replace trailing comma
print O $outstr;
}
}
| [reply] [d/l] |
How can I get the name of the node being walked to display for each port or at least at the beginning of the ports for that node?
If including the IP address at the beginning of each csv data row is good enough to satisfy this, I already mentioned how to do that at the bottom of my first reply. (Maybe that wasn't clear enough?)
If you mean that you need to include the host name string from the original host list input file, that's not too much to add, given that the @hosts array and the @iplist array are parallel. Changing the print loop to read as follows ought to suffice -- I'm just adding a line after the first "for" statement, and making a slight change to the line following the second "for" statement:
for my $ip ( @iplist ) {
my $host = shift @hosts; # get host name for this $ip
for my $portid ( 0 .. $ipdata{$ip}{lastport} ) {
my $outstr = "$host,";
for my $info ( @infolist ) {
$outstr .= '"' . $ipdata{$ip}{$info}[$portid] . '",';
}
$outstr =~ s/,$/\n/; # replace trailing comma
print O $outstr;
}
}
If/when you learn more about programming, you should be able to look at the script as it stands and see ways of improving it (e.g. putting the host name into the hash along with all the port data, so that all the printing is done from data in the hash). Anyway, good luck. | [reply] [d/l] |
OK. It works and works exactly how I wanted it to.
I can't thank you enough for your help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
| [reply] |