Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
4/11/2013 4:34:42 PM EDT
my input data file "hosts" has a hostname on a line followed by an ip address on the next line.  then another host and then another ip.  at very end of file I have put in ### on a separate line to help me bail out of the while loop.  Im using $cnt as a counter to increase by 2 each iteration so that each iteration reads both a host and its associated IP address.  All my data prints as expected but for some reason its an infinite loop after all my data prints and it never exits the loop and just continues printing iterations of the loop with null hostname and IPs.  ?????

#!/usr/bin/perl

open FILE, "hosts" or die "Couldn't open file: $!";
@agents = <FILE>;
close FILE;

chomp(@agents);
$cnt=0;
while (@agents[$cnt] ne "###") {
       print "select @agents[$cnt]\n";
       print "1\n";
       $cnt++;
       print "hostname @agents[$cnt]\n";
       print "ip-address @agents[$cnt]\n";
       print "description @agents[$cnt-1]\n";
       $cnt++;
}
4/11/2013 11:32:55 PM EDT
[#1]
"while" is pretty prone to that sort of thing.  If, for any reason, the terminal condition isn't met, you're going to have a bad day.  You've likely got something in the hosts file of which you aren't aware which is preventing your terminal comparison from ever being true, my guess would be a carriage-return in addition to a newline.  

In any event, for sanity, try a "foreach" on the array instead of a "while", and throw in a conditional exit in case you hit your terminal condition before EOF.  Plan on data coming in bad, not good.

BTW, it's $array[$cnt], not @array[$cnt].
4/12/2013 2:29:42 PM EDT
[#2]
yeah it was something dumb like that.  in vi my input file doenst show anything wierd but if i > the output of the script to a file and then less it theres tons of ^M in there.

all for naught now anyways, the corp server doesnt have any perl modules installed so i cant expect or net:telnet or anything for a while, ill get back on it later though.

thanks