Hi Eyal, On Thursday 20 Jan 2011 15:38:00 Eyal B. wrote: > I'm writing a scripts that check the TTL of the ping and found the OS. > According the TTL - the script should let me know which OS it is : > Linux/ Windows or Unix (Hash table) > > I'm getting an error on the line where I should use the TTL variable - > and take the right value from the hash (%list) :Use of uninitialized > value in print at D:\system\perl\os-rec\os-rec5_.pl line 24 > , <HANDLE> line 3. > > Any idea ? >
First of all, you should use a CPAN module for Ping (maybe http://search.cpan.org/dist/Net-Ping/ ). Otherwise, here are some comments on your code. > > #! C:\Perl\bin\perl -w > use strict; > use warnings; You shouldn't specify -w in the sha-bang. "use warnings;" (which you also have) is better. > > my %list = (60,"linux",61,"linux",62,"linux",63,"linux",64,"linux", > 65,"linux",125,"Windows",126,"Windows",127,"Windows",128,"Windows", > 250,"Unix",251,"Unix",252,"Unix",253,"Unix",254,"Unix",255,"Unix", > 256,"Unix",257,"Unix",258,"Unix",259,"Unix",260,"Unix"); It's not a list -it's a hash. And you should use the fat comma ("=>"), and use some indentation. > my $path = "hosts.txt" ; > my $machine_IP ; > my $cmd ; > Don't predeclare the variables. Declare them when needed. > # read machines hosts names List from the txt file c:1.txt and take > the TTL data to th e variable: $line > open (MACHINES,$path) or die "Couldn't open file for writing"; Use three args open and don't use bareword file handles. > while ($machine_IP = <MACHINES>) > { > chomp($machine_IP) ; > my $line ; > $cmd = "ping $machine_IP"; > open(HANDLE,"$cmd|"); You should use three-args open, lexical filehandles and "or die". Furthermore, use $cmd is not needed here as you can interpolate directly from $cmd. > while ($line = <HANDLE>) > { > if("$line" =~ "TTL=") No need for wrapping $line in double quotes here as $line is a string. > { > $line =~ s/.*TTL=//; > print "TTL = $line\n"; > print $list{"$line"} ; You can do all that in one regex match. if (my ($ttl) = $line =~ m{TTL=(\d+)}) > # print "Machine $machine_IP is $list{$line}" ; > last; } > } > } > close HANDLE; > close MACHINES; For more information see: http://perl-begin.org/tutorials/bad-elements/ Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Escape from GNU Autohell - http://www.shlomifish.org/open- source/anti/autohell/ Chuck Norris can make the statement "This statement is false" a true one. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/