jlandrig has asked for the wisdom of the Perl Monks concerning the following question:

Hello about 2 days ago our ping script started generating errors during the running of the script Error shown:
Checking "devicename"... alive Use of uninitialized value in split at +script/ping2.pl line 74, <IN> line 17 Use of uninitialized value $NBTHost in string eq at script.ping2.pl li +ne 76 <IN> . line 17 Not unique Use of uninitialized value $NBTHost in concatenation <.> or string at +script/ping2.pl line 97, <IN> line 17.
I am a novice when it comes to this so any help would be appriciated This is the full script
#!/usr/bin/env perl use warnings; use strict; # # Author: # # 2011(C) # # Change History # 0.1 Initial Release # 0.2 20150109.01 Added epoport.txt file to pass ePO po +rt # # Description: Reads list of server names by their host names, checks +to see if # they are alive via ping, completes an nbtstat -a and returns any lin +es containing # the host name and then checks to see if ePO port is open on the aliv +e system. # # Input: # srvlst.txt containing one hostname per line # epoport.txt containing ePO port # # Output: # PingResults.csv # Format: Hostname, IP address, Alive status (Yes|No|Unknown Ho +st|Unreachable|Unknown status), NBTSTAT result (NUL|$hostname), ePO p +ort status (Open|closed) my $epoport = ''; my $socket = ""; my $hostcnt = 0; open (my $conf, '<epoport.txt') or die "Run aborted. Cannot open the e +PO port file (epoport.txt)"; while(<$conf>) { my $line = $_; chomp($line); ($epoport) = split(",", $line); }; close ($conf); print "ePO port: $epoport\n\n"; open (IN, "<srvlst.txt") or die "Run aborted. Cannot open endpoint lis +t file (srvlst.txt)"; open (OUT, ">PingResults.csv") or die "Run aborted. Unable to open res +ult file (PingResults.csv)"; print OUT "Hostname,IP,Alive,NBTHost,Port $epoport\n"; print "Start to check hosts\n"; while (<IN>) { my $HOST = $_; chomp $HOST; print "Checking $HOST... "; my $IP = ''; my $NBTHost = ''; my $Alive = 'No'; my $ePOPort = 'Closed'; my $out = ""; ++$hostcnt; my $PingReply = `PING -n 1 -w 1 $HOST`; if ($PingReply =~ /unreachable/) { print "unreachable\n"; $Alive = 'Unreachable'; } elsif ($PingReply =~ /Reply from/) { $Alive = 'Yes'; print "alive "; my @Temp = split / /, $PingReply; $IP = $Temp[2]; $IP =~ s/\[//g; $IP =~ s/\]//g; my $NBTResults = `nbtstat -a $IP | find \"UNIQUE\"`; while (index($NBTResults,' ') > -1) { $NBTResults =~ s/ / /g; } my @NBTResults = split /\n/, $NBTResults; # We only need the first returned hostname of the NBT results +(they're usually duplicates) my @LineTemp = split / /, $NBTResults[0]; $NBTHost = $LineTemp[1]; if ($NBTHost eq $HOST) { $NBTHost = ''; print "unique\n"; # Check ePO open port my $epoResponse = CheckPort($HOST,$epoport); if ($epoResponse == 1) { $ePOPort = "Open"; } else { $ePOPort = "Closed"; }; } else { print "Not unique\n"; $Alive = 'Unknown status'; } } elsif ($PingReply =~ /could not find host/) { print "unknown\n"; $Alive = 'Unknown host'; } else { print "not alive\n"; $Alive = 'No'; } print OUT "$HOST,$IP,$Alive,$NBTHost,$ePOPort\n"; } close OUT; close IN; print "\nCheck completed. Checked $hostcnt hosts\n"; sub CheckPort { use IO::Socket::INET; my ($host,$port) = @_; my $open = eval{ $socket = new IO::Socket::INET # opens a socket ( PeerAddr => $host, PeerPort => $port, Proto => getprotobyname('tcp') || 6 # tcp socket or sopcket 6(for +old comps) )}; if ($open) { return 1; } else { return 0; } }
thank you

Replies are listed 'Best First'.
Re: Ping Script Error running on 64bit server
by roboticus (Chancellor) on Nov 15, 2017 at 22:23 UTC

    jlandrig:

    The error:

    Use of uninitialized value in split at script/ping2.pl line 74, <IN> line 17

    Indicates that $NBTResults[0] is undefined. That probably means that the $NBTResults string just consisted of newlines. In that event, split would return nothing, so $NBTResults[0] would be undefined. That probably leads to the next two errors.

    I'd suggest checking for that and skipping processing when that occurs by putting something like:

    next unless @NBTResults;

    just before line 74.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Since the code is designed to display the Nbtstat-a value if it does not match the host file. skipping would work but unfortunatly the script is no longer displaying if the Host is pingable or not output just reads
      Hostname,IP,Alive,NBTHost,Port 55081 ComputerName,IP,Unknown status,,Closed
      But it should have reported computername,IP,YES,,CLOSED this is only happening when we run the script from a Windows 64 bit OS.

        Try running a simplified version of the script and print the responses

        #!/usr/bin/env perl use warnings; use strict; my $serverlist = 'srvlst.txt'; open IN, '<',$serverlist or die "Run aborted. Cannot open endpoint list file ($serverlist)"; while (my $HOST = <IN>) { chomp $HOST; print "Pinging $HOST\n"; my $PingReply = `PING -n 1 -w 1 $HOST`; print "[$PingReply]\n"; if ($PingReply =~ /Reply from/) { my @Temp = split / /, $PingReply; my $IP = $Temp[2]; $IP =~ s/\[//g; $IP =~ s/\]//g; print "IP = [$IP]\n"; my $NBTResults = `nbtstat -a $IP | find \"UNIQUE\"`; print "nbtstat result [$NBTResults]\n"; } }
        poj
Re: Ping Script Error running on 64bit server
by Laurent_R (Canon) on Nov 15, 2017 at 19:47 UTC
    Please use <code> and </code> tags for your code as well as data examples if any.

    See also Markup in the Monastery.

    Update at 23:02 UTC: Thank you for updating your post and making it readable. It would be nice, though, to indicate that you changed your two posts.