Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Messed up output 1

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
I have the following program which worked fine when it queried 1 router for info and outputed the query to a table. Now I need to query 2 routers and output the results side by side. I think I need to somehow have one foreach loop query both routers and use one write statement. Here is my code:

# This program performs an SNMPWALK command for the Cisco BGP MIB and prints the results.


# Set up variables and import modules

use strict;
use Socket;
my $cs = "jf7RKD76";
my $OID = ".1.3.6.1.2.1.15.3.1.2";
my $EAST = "172.16.255.187";
my $WEST = "172.16.255.251";
chomp $EAST;
chomp $WEST;
my $ipeast;
my $ipwest;
my $stateast;
my $statwest;
my $reseast;
my $reswest;
my $hosteast;
my $hostwest;
my $answest;
my $anseast;

# Perform snmpwalk on inputed variable from OpenView and resolve ipaddresses

my @reseast = `snmpwalk -c $cs $EAST $OID`;

foreach (@reseast) {
($ipeast,$stateast) = /(\d+\.\d+\.\d+\.\d+).*?\s+(\w+)\Z/;
$reseast = inet_aton("$ipeast");
$hosteast = gethostbyaddr($reseast, AF_INET);
$anseast=$hosteast || $ipeast;
write();
}

my @reswest = `snmpwalk -c $cs $WEST $OID`;

foreach (@reswest) {
($ipwest,$statwest) = /(\d+\.\d+\.\d+\.\d+).*?\s+(\w+)\Z/;
$reswest = inet_aton("$ipwest");
$hostwest = gethostbyaddr($reswest, AF_INET);
$answest=$hostwest || $ipwest;
write();
}
# Output format for write command


format STDOUT_TOP =
------------ -------------
.

format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<
$anseast $stateast $answest $statwest
.


The output looks like this now:

tsg-ws28.osu.qwest.net idle
qwest7-denver-rtr1 established
63.236.121.60 idle
uac08-rtr01-qwest4.aon.storabili established
uac13-rtr02-qwest9.aon.storabili established
uac01-rtr02-inslogic1.aon.storab established
uac02-rtr01-covisint.aon.storabi established
198.94.30.43 idle
199.93.193.59 active
uac16-rtr01-ctc01.aon.storabilit established
uac06-rtr01-qwest2.aon.storabili established
uac09-rtr01-qwest5.aon.storabili idle
uac09-rtr02-qwest5.aon.storabili established
208.45.147.60 established
rtr01.ams02.storability.net established
rtr02.ams01.storability.net established
rtr02.mcd01.storability.net established
216.34.168.201 idle
uac15-rtr02-mydoc.aon.storabilit established
217.71.130.45 idle
217.71.130.45 idle uac11-rtr02-qwest7.aon.storabili established
217.71.130.45 idle hoc-ws4.iad2.qwest.net established
217.71.130.45 idle uac13-rtr01-qwest9.aon.storabili established
217.71.130.45 idle uac01-rtr01-inslogic1.aon.storab established
217.71.130.45 idle uac02-rtr02-covisint.aon.storabi established
217.71.130.45 idle 199.93.193.58 active
217.71.130.45 idle uac16-rtr02-ctc01.aon.storabilit established
217.71.130.45 idle uac06-rtr02-qwest2.aon.storabili established
217.71.130.45 idle uac09-rtr01-qwest5.aon.storabili established
217.71.130.45 idle uac10-rtr02-qwest6.aon.storabili established
217.71.130.45 idle rtr02.ams02.storability.net established
217.71.130.45 idle rtr01.ams01.storability.net established
217.71.130.45 idle uac05-rtr01-workscape.aon.storab established
217.71.130.45 idle rtr01.mcd01.storability.net established
217.71.130.45 idle uac15-rtr01-mydoc.aon.storabilit established


I want the results side by side.

Any ideas?
 
hey there,

if you're asking a question about formatting output, you probably don't have to put the rest of your program into the question...

anyway, yes, as the loops are currently set up, the $answest and $statwest variables don't even get defined until the second loop, so every call to 'write' prints nothing in their spot. then, in the second loop, the two east variables don't change from the value they were last set to, so that's why that one is printed over and over. therefore, you'll have to do the printing to output with only a single loop. what you can do is keep the two loops you have now, but instead of writing in each loop, have a data structure, i'd suggest a simple two dimentional array, that you can store the four variables in each row, and have a row for every line to be printed out. then, add a loop after that to just go through the rows of that array, setting the four elements to their respective variables, and then calling write.

if you need help implementing a 2D array, search through this forum and see what you can get, as i remember stepping through this for someone at least once. otherwise, come back and ask again, and maybe i'll even make a FAQ about it.

good luck,
stillflame &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Stillflame,

Thanks for the help, I see what you are saying, but it is a little above me at this point. I have changed my program to write the results of the foreach loops into hashes. I think I want hash of hashes though. That would give me all 4 variables in one hash and then I could loop through and take each hash slice and send it to the write statement.

Here is my new code:

foreach (@reseast) {
($ipeast,$stateast) = /(\d+\.\d+\.\d+\.\d+).*?\s+(\w+)\Z/;
$reseast = inet_aton(&quot;$ipeast&quot;);
$hosteast = gethostbyaddr($reseast, AF_INET);
$anseast=$hosteast || $ipeast;
$haeast{$anseast} = $stateast;
}


foreach (@reswest) {
($ipwest,$statwest) = /(\d+\.\d+\.\d+\.\d+).*?\s+(\w+)\Z/;
$reswest = inet_aton(&quot;$ipwest&quot;);
$hostwest = gethostbyaddr($reswest, AF_INET);
$answest=$hostwest || $ipwest;
$hawest{$answest} = $statwest;
}

How do I get the 4 variables into a hash of hashes, IF that is the best way to go.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top