Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Code for port scanner on range of IPs

by ckj (Chaplain)
on Aug 27, 2012 at 03:02 UTC ( #989904=perlquestion: print w/replies, xml ) Need Help??
ckj has asked for the wisdom of the Perl Monks concerning the following question:

Hi MOnks, Please suggest how can I check opened port for range of IPs. Below is my code
#!/usr/bin/perl # TCP Port scanner print q{ ================================================= SIMPLE TCP PORT SCANNER Coded by: CKJ ================================================= }; use IO::Socket; $source_dir= "C:/ips.txt"; open(FH,"$source_dir") or die $!; while(my $line= <FH> ){ while($line=~/(\d+.\d+.\d+.\d+)-(\d+.\d+.\d+.\d+)/g){ print "IPs are: $1 to $2\n"; $min_ip = $1; $max_ip = $2; for (; $min_ip<=$max_ip; $min_ip++) { my %ports = (80,8080,81,8181); my ( $daddr ); { $daddr = inet_aton($min_ip) || die("Failed to locate target + host: $target"); print " =================================================== +=============\n"; print " Scanning $min_ip on ports $port-$maxport\n"; print " This might take a while depending on how many ports + you have\n"; print " =================================================== +=============\n"; foreach $port(%ports) { print "\n == Scanning port $port =="; if( IO::Socket::INET->new(PeerAddr=>"$min_ip:$port",Pro +to=>'tcp',Timeout=>1)) { print "\n==== Port $port is open ====\n" ; } else{ print "\nPort $port is NOT open\n"; } } } print "\n\n\nFinished Scanning $min_ip up to port $maxport\n\n +"; } } } print "\n Thanks for using my port scanner\n"; exit (0); close (FH);
The ips are right now taken from a text file which format is something like this: ips.txt:
Please help e what could be the best way to execute it properly for all the IPs and also is there any better way to execute the program faster???

Replies are listed 'Best First'.
Re: Code for port scanner on range of IPs
by 2teez (Vicar) on Aug 27, 2012 at 07:04 UTC

    Please use warnings and strict in your scripts. It will help identify several errors to start with.
    There are several variables that were not defined that just "popped" up everywhere such as "$target","$maxport","$port" used before the second foreach loop, then another "$port" used in the second foreach loop.

    You really don't need the second while loop with the REs to get your variables $min_ip and $max_ip like so:

    ... while($line=~/(\d+.\d+.\d+.\d+)-(\d+.\d+.\d+.\d+)/g){ ...
    that you could achieve within the first while loop like so:
    ... my($min_ip,$max_ip)=split/-/,$line; ...

Re: Code for port scanner on range of IPs
by marto (Bishop) on Aug 27, 2012 at 09:51 UTC
Re: Code for port scanner on range of IPs
by NetWallah (Canon) on Aug 27, 2012 at 15:00 UTC
    IN addition to the comments above,
    my %ports = (80,8080,81,8181);
    should be:
    my @ports = (80,8080,81,8181); ## Array, not hash
    You should also consider using a module like NetAddr::IP to manage IP address analysis, and iterating ranges.

    In fact, Nmap::Scanner Could probably do the entire job for you very professionally.

                 I hope life isn't a big joke, because I don't get it.

Re: Code for port scanner on range of IPs
by sundialsvc4 (Abbot) on Aug 27, 2012 at 11:16 UTC

    One thing you need to look for is overlapping ranges of IPs in that input-file.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://989904]
Approved by philipbailey
NodeReaper edits tomorrow's obituaries

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-03-19 02:01 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (231 votes). Check out past polls.