Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Host discovery tool problem

by perlaintdead (Scribe)
on Jan 02, 2012 at 02:55 UTC ( #945826=perlquestion: print w/ replies, xml ) Need Help??
perlaintdead has asked for the wisdom of the Perl Monks concerning the following question:

im writing this tool called "white rabbit" and it seems to have some errors! threads don't close correctly and other errors. you might notice that i use Modules that aren't utilized because the tool is not done yet. any help would be greatly appreciated

#!/usr/bin/perl # White Rabit # knok knok, neo use NetPacket::ICMP; use IO::Socket::INET; use IO::Socket::SSL; use Socket; use Net::Ping; use LWP::Simple; use threads; use threads::shared; use Getopt::Long; my($Host,$options,$ping,$i,$port,$Status,$SSLSocketTest,$SockTest,$i1, +$i2,$i3,$i4,$i5,$i6,$i7,$i8,$i9,$i10); GetOptions("host=s" => \$Host); $Status = "\nhost looks down\n"; print "White Rabit v1\nIntence host discovery tool. START TIME ". loca +ltime() ."\n"; print "-" x 70 . "\n"; # valid ip? unless(inet_aton($Host)){ print "Invalid IP Addr! IT'S A TRAP\n"; exit(); } #lets try some Sockets for($i=0;$i<3;$i++){ if($i == 0){ $port=80; } if($i == 1){ $port=443; } if($i == 2){ $port=53; } my $SockTest = IO::Socket::INET->new( PeerAddr => $Host, PeerPort => $port, Proto => 'tcp' ) or die "" and goto Next; print "tcp Socket $Host:$port opened\n"; $Status = "$Host looks up :)\n"; Next: close($SockTest); } for($i=0;$i<3;$i++){ if($i == 0){ $port=80; } if($i == 1){ $port=443; } if($i == 2){ $port=53; } $SSLSocketTest = IO::Socket::SSL->new( "$Host:$port" ) or die +goto Next2; print "SSL Socket $Host:$port opened\n"; $Status = "$Host looks up :)\n"; Next2: close($SSLSocketTest); } # pings a plenty my $thr1 = threads->new(\&pingsub1); my $thr2 = threads->new(\&pingsub2); my $thr3 = threads->new(\&pingsub3); my $thr4 = threads->new(\&pingsub4); my $thr5 = threads->new(\&pingsub5); sleep(.5); $thr1->join; sleep(.5); $thr2->join; sleep(.5); $thr3->join; sleep(.5); $thr4->join; sleep(.5); $thr5->join; my $thr6 = threads->new(\&pingsub6); my $thr7 = threads->new(\&pingsub7); my $thr8 = threads->new(\&pingsub8); my $thr9 = threads->new(\&pingsub9); my $thr10 = threads->new(\&pingsub10); sleep(.5); $thr6->join; sleep(.5); $thr7->join; sleep(.5); $thr8->join; sleep(.5); $thr9->join; sleep(.5); $thr10->join; # rapid attacks of the rabid rabbits! sub pingsub1{ for($i1=0;$i1<20;$i1++){ $ping = Net::Ping->new("icmp"); $ping->port_number($i1); print "icmp ping reply on port $i1\n" and $Status = "$Host loo +ks up :)\n" if $ping->ping($Host); } } sub pingsub2{ for($i2=20;$i2<40;$i2++){ $ping = Net::Ping->new("icmp"); $ping->port_number($i2); print "icmp ping reply on port $i2\n" and $Status = "$Host loo +ks up :)\n" if $ping->ping($Host); } } sub pingsub3{ for($i3=40;$i3<60;$i3++){ $ping = Net::Ping->new("icmp"); $ping->port_number($i3); print "icmp ping reply on port $i3\n" and $Status = "$Host loo +ks up :)\n" if $ping->ping($Host); } } sub pingsub4{ for($i4=60;$i4<80;$i4++){ $ping = Net::Ping->new("icmp"); $ping->port_number($i4); print "icmp ping reply on port $i4\n" and $Status = "$Host loo +ks up :)\n" if $ping->ping($Host); } } sub pingsub5{ for($i5=80;$i5<101;$i5++){ $ping = Net::Ping->new("icmp"); $ping->port_number($i5); print "icmp ping reply on port $i5\n" and $Status = "$Host loo +ks up :)\n" if $ping->ping($Host); } } sub pingsub6{ for($i6=0;$i6<20;$i6++){ $ping = Net::Ping->new("tcp"); $ping->port_number($i6); print "tcp ping reply on port $i6\n" and $Status = "$Host look +s up :)\n" if $ping->ping($Host); } } sub pingsub7{ for($i7=20;$i7<40;$i7++){ $ping = Net::Ping->new("tcp"); $ping->port_number($i2); print "tcp ping reply on port $i2\n" and $Status = "$Host look +s up :)\n" if $ping->ping($Host); } } sub pingsub8{ for($i8=40;$i8<60;$i8++){ $ping = Net::Ping->new("tcp"); $ping->port_number($i8); print "tcp ping reply on port $i8\n" and $Status = "$Host look +s up :)\n" if $ping->ping($Host); } } sub pingsub9{ for($i9=60;$i9<80;$i9++){ $ping = Net::Ping->new("tcp"); $ping->port_number($i9); print "tcp ping reply on port $i9\n" and $Status = "$Host look +s up :)\n" if $ping->ping($Host); } } sub pingsub10{ for($i10=80;$i10<101;$i10++){ $ping = Net::Ping->new("tcp"); $ping->port_number($i10); print "tcp ping reply on port $i10\n" and $Status = "$Host loo +ks up :)\n" if $ping->ping($Host); } } print "\n\n" . $Status . "END TIME " . localtime(); exit();

Comment on Host discovery tool problem
Download Code
Re: Host discovery tool problem
by Eliya (Vicar) on Jan 02, 2012 at 03:20 UTC
    for($i=0;$i<3;$i++){ if($i == 0){ $port=80; } if($i == 1){ $port=443; } if($i == 2){ $port=53; } ... }

    On a side note, as $i isn't being used in the rest of the loop, this could be shorter written as

    for my $port (80, 443, 53) { ... }
Re: Host discovery tool problem
by GrandFather (Cardinal) on Jan 02, 2012 at 04:56 UTC

    If you find yourself writing the same code more than once, especially if your variable names start including numbers, it's time to write a common sub and refactor your code. Consider the following rework of the major portion of your code:

    #!/usr/bin/perl use warnings; use strict; use threads; use IO::Socket::INET qw(); use NET::Ping qw(); use Time::HiRes qw(); my $kHost = '127.0.0.1'; my @ports = (80, 443, 53); for my $port (@ports) { my $SockTest = IO::Socket::INET->new( PeerAddr => $kHost, PeerPort => $port, Proto => 'tcp' ) or next; print "tcp Socket $kHost:$port opened\n"; close ($SockTest); } # pings a plenty for my $mode ('icmp', 'tcp') { my @threads = map { threads->new(sub {doPing($kHost, $_ * 20, $mode)}) } 0 .. 4; for my $thread (@threads) { Time::HiRes::usleep (500_000); $thread->join(); } print "Done $mode test\n"; } print "Done\n"; sub doPing { my ($host, $portBase, $mode) = @_; for my $port ($portBase .. $portBase + 19) { my $ping = Net::Ping->new($mode); $ping->port_number($port); print "$mode ping reply on port $port\n" if $ping->ping($host) +; } }

    Prints (with a chunk omitted):

    tcp Socket 127.0.0.1:80 opened tcp Socket 127.0.0.1:443 opened icmp ping reply on port 0 icmp ping reply on port 20 icmp ping reply on port 21 icmp ping reply on port 1 icmp ping reply on port 2 icmp ping reply on port 22 icmp ping reply on port 23 ... icmp ping reply on port 96 icmp ping reply on port 97 icmp ping reply on port 98 icmp ping reply on port 99 Done icmp test tcp ping reply on port 80 Done tcp test Done

    There are a few issues with your code aside from general bad coding such as declaring all your variables up front, using C for loops and cut and pasting large swathes of code.  or die "" and goto Next; isn't going to do whatever it is you expect it to because the program will exit when it hits the die without executing the goto.

    sleep sleeps for integer seconds so sleep 0.5 is the same as sleep 0.

    $Status is essentially useless because it is written to in multiple places (including from threads) but its value is only shown in one place where it is unaffected by any changes made in the treads in any case.

    True laziness is hard work

      thank you for you great wisdom. i don't have much experience with perl

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://945826]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-12-29 05:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls