Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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:
64.136.192.0-64.136.223.255 64.136.224.0-64.136.255.255 64.137.0.0-64.137.255.255 64.138.0.0-64.138.127.255 64.138.128.0-64.138.191.255 64.138.192.0-64.138.255.255 64.139.0.0-64.139.63.255 64.139.64.0-64.139.95.255 64.139.96.0-64.139.127.255 64.139.128.0-64.139.191.255 64.139.192.0-64.139.223.255 64.139.224.0-64.139.255.255 64.140.0.0-64.140.63.255 64.140.64.0-64.140.95.255 64.140.96.0-64.140.111.255 64.140.128.0-64.140.191.255 64.140.192.0-64.140.255.255 64.141.128.0-64.141.191.255 64.141.192.0-64.141.255.255 64.142.0.0-64.142.127.255 64.142.128.0-64.142.255.255 64.143.0.0-64.143.255.255 64.144.0.0-64.144.255.255 64.145.0.0-64.145.63.255 64.145.64.0-64.145.95.255 64.145.96.0-64.145.127.255 64.145.128.0-64.145.255.255 64.146.0.0-64.146.127.255 64.146.128.0-64.146.255.255 64.147.0.0-64.147.31.255 64.147.32.0-64.147.47.255 64.147.48.0-64.147.63.255 64.147.64.0-64.147.79.255 64.147.96.0-64.147.127.255 64.147.128.0-64.147.159.255 64.147.160.0-64.147.191.255 64.147.192.0-64.147.207.255 64.147.208.0-64.147.223.255 64.148.0.0-64.149.255.255 64.150.0.0-64.150.127.255 64.150.128.0-64.150.159.255 64.150.160.0-64.150.161.255 64.150.162.0-64.150.162.255 64.150.163.0-64.150.163.255 64.150.164.0-64.150.167.255 64.150.168.0-64.150.175.255 64.150.176.0-64.150.191.255 64.151.0.0-64.151.63.255 64.151.64.0-64.151.127.255 64.151.128.0-64.151.191.255 64.152.0.0-64.159.255.255 64.160.0.0-64.175.255.255 64.176.0.0-64.177.255.255 64.178.0.0-64.178.63.255 64.178.64.0-64.178.95.255 64.178.96.0-64.178.127.255 64.178.160.0-64.178.175.255 64.178.176.0-64.178.191.255 64.178.224.0-64.178.255.255 64.179.0.0-64.179.127.255 64.179.128.0-64.179.191.255 64.179.192.0-64.179.207.255 64.179.224.0-64.179.239.255 64.179.240.0-64.179.255.255 64.181.0.0-64.181.127.255 64.181.128.0-64.181.255.255 64.182.0.0-64.182.255.255 64.183.0.0-64.183.127.255 64.183.128.0-64.183.159.255 64.183.160.0-64.183.175.255 64.183.176.0-64.183.191.255 64.183.192.0-64.183.255.255 64.184.0.0-64.184.127.255 64.184.128.0-64.184.191.255 64.184.224.0-64.184.255.255 64.185.0.0-64.185.63.255 64.185.64.0-64.185.95.255 64.185.96.0-64.185.127.255 64.185.128.0-64.185.159.255 64.185.160.0-64.185.191.255 64.185.224.0-64.185.239.255 64.185.240.0-64.185.255.255 64.186.32.0-64.186.63.255 64.186.64.0-64.186.79.255 64.186.80.0-64.186.95.255 64.186.96.0-64.186.127.255 64.186.128.0-64.186.159.255 64.186.160.0-64.186.175.255 64.186.176.0-64.186.191.255 64.186.192.0-64.186.207.255 64.186.208.0-64.186.223.255 64.186.224.0-64.186.255.255 64.187.64.0-64.187.95.255 64.187.96.0-64.187.127.255 64.187.128.0-64.187.255.255 64.188.0.0-64.188.31.255 64.188.32.0-64.188.47.255 64.188.48.0-64.188.63.255 64.188.64.0-64.188.127.255 64.188.128.0-64.188.255.255 64.189.0.0-64.189.255.255 64.190.0.0-64.190.255.255 64.191.0.0-64.191.127.255 64.191.128.0-64.191.191.255 64.191.192.0-64.191.255.255 64.192.0.0-64.195.255.255 64.196.0.0-64.199.255.255 64.200.0.0-64.200.255.255 64.201.0.0-64.201.31.255 64.201.64.0-64.201.79.255 64.201.80.0-64.201.95.255 64.201.96.0-64.201.127.255 64.201.128.0-64.201.143.255 64.201.144.0-64.201.159.255 64.201.224.0-64.201.239.255 64.201.240.0-64.201.255.255 64.202.0.0-64.202.31.255 64.202.32.0-64.202.47.255 64.202.48.0-64.202.63.255 64.202.64.0-64.202.79.255 64.202.80.0-64.202.95.255 64.202.96.0-64.202.127.255 64.202.128.0-64.202.143.255 64.202.160.0-64.202.191.255 64.202.192.0-64.202.207.255 64.202.208.0-64.202.223.255 64.202.224.0-64.202.239.255 64.202.240.0-64.202.255.255 64.203.0.0-64.203.63.255 64.203.64.0-64.203.95.255 64.203.96.0-64.203.111.255 64.203.112.0-64.203.127.255 64.203.128.0-64.203.191.255 64.203.192.0-64.203.255.255 64.204.0.0-64.205.255.255 64.206.0.0-64.206.255.255 64.207.0.0-64.207.63.255 64.207.64.0-64.207.95.255 64.207.96.0-64.207.111.255 64.207.112.0-64.207.127.255 64.207.128.0-64.207.191.255 64.207.192.0-64.207.223.255 64.207.224.0-64.207.255.255 64.208.0.0-64.208.255.255 64.209.0.0-64.209.127.255 64.209.128.0-64.209.143.255 64.209.144.0-64.209.159.255 64.209.160.0-64.209.191.255 64.209.192.0-64.209.207.255 64.209.208.0-64.209.223.255 64.209.224.0-64.209.255.255 64.210.0.0-64.210.127.255 64.210.128.0-64.210.159.255 64.210.160.0-64.210.191.255 64.210.192.0-64.210.255.255 64.211.0.0-64.211.127.255 64.211.128.0-64.211.191.255 64.211.192.0-64.211.223.255 64.211.224.0-64.211.255.255 64.212.0.0-64.215.255.255 64.216.0.0-64.219.255.255 64.220.0.0-64.221.255.255 64.222.0.0-64.222.63.255 64.222.64.0-64.222.127.255 64.222.128.0-64.222.191.255 64.222.192.0-64.222.255.255 64.223.0.0-64.223.63.255 64.223.64.0-64.223.127.255 64.223.128.0-64.223.191.255 64.223.192.0-64.223.255.255 64.224.0.0-64.227.255.255 64.232.0.0-64.232.255.255 64.233.0.0-64.233.127.255 64.233.128.0-64.233.159.255 64.233.160.0-64.233.191.255 64.233.192.0-64.233.255.255 64.234.0.0-64.234.63.255 64.234.64.0-64.234.95.255 64.234.96.0-64.234.111.255 64.234.112.0-64.234.127.255 64.234.128.0-64.234.191.255 64.234.192.0-64.234.223.255 64.234.224.0-64.234.239.255 64.234.240.0-64.234.255.255 64.235.0.0-64.235.15.255 64.235.16.0-64.235.31.255 64.235.32.0-64.235.63.255 64.235.64.0-64.235.95.255 64.235.112.0-64.235.127.255 64.235.128.0-64.235.143.255 64.235.144.0-64.235.159.255 64.235.160.0-64.235.191.255 64.235.224.0-64.235.255.255 64.236.0.0-64.236.255.255 64.237.0.0-64.237.15.255 64.237.16.0-64.237.31.255 64.237.32.0-64.237.63.255 64.237.64.0-64.237.95.255 64.237.96.0-64.237.111.255 64.237.112.0-64.237.127.255 64.238.0.0-64.238.15.255 64.238.16.0-64.238.31.255 64.238.32.0-64.238.47.255 64.238.48.0-64.238.63.255 64.238.64.0-64.238.95.255 64.238.96.0-64.238.127.255 64.238.128.0-64.238.143.255 64.238.144.0-64.238.159.255 64.238.160.0-64.238.191.255 64.238.192.0-64.238.223.255 64.238.224.0-64.238.255.255 64.239.0.0-64.239.127.255 64.239.128.0-64.239.191.255 64.239.192.0-64.239.223.255 64.239.224.0-64.239.239.255 64.239.240.0-64.239.255.255 64.240.0.0-64.243.255.255 64.244.0.0-64.245.255.255 64.246.0.0-64.246.63.255 64.246.64.0-64.246.95.255 64.246.96.0-64.246.127.255 64.246.128.0-64.246.159.255 64.246.160.0-64.246.191.255 64.246.192.0-64.246.223.255 64.246.224.0-64.246.239.255 64.246.240.0-64.246.255.255 64.247.0.0-64.247.63.255 64.247.64.0-64.247.127.255 64.247.176.0-64.247.191.255 64.247.192.0-64.247.223.255 64.247.224.0-64.247.255.255 64.248.0.0-64.249.255.255 64.250.0.0-64.250.15.255 64.250.16.0-64.250.31.255 64.250.32.0-64.250.47.255 64.250.48.0-64.250.63.255 64.250.64.0-64.250.79.255 64.250.96.0-64.250.111.255 64.250.112.0-64.250.127.255 64.250.128.0-64.250.143.255 64.250.144.0-64.250.159.255 64.250.160.0-64.250.175.255 64.250.176.0-64.250.191.255 64.250.192.0-64.250.223.255 64.250.224.0-64.250.255.255 64.251.0.0-64.251.31.255 64.251.32.0-64.251.47.255 64.251.48.0-64.251.63.255 64.251.96.0-64.251.111.255 64.251.112.0-64.251.127.255 64.251.128.0-64.251.159.255 64.251.160.0-64.251.191.255 64.251.192.0-64.251.207.255 64.251.208.0-64.251.223.255 64.251.224.0-64.251.239.255 64.251.240.0-64.251.255.255 64.252.0.0-64.252.255.255 64.253.0.0-64.253.31.255 64.253.32.0-64.253.63.255 64.253.64.0-64.253.79.255 64.253.80.0-64.253.95.255 64.253.96.0-64.253.127.255 64.253.160.0-64.253.191.255 64.253.192.0-64.253.207.255 64.253.208.0-64.253.223.255 64.253.224.0-64.253.255.255 64.254.32.0-64.254.63.255
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???

Comment on Code for port scanner on range of IPs
Select or Download Code
Re: Code for port scanner on range of IPs
by 2teez (Priest) on Aug 27, 2012 at 07:04 UTC
    Hi,

    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 (Chancellor) on Aug 27, 2012 at 09:51 UTC
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.

Re: Code for port scanner on range of IPs
by NetWallah (Abbot) 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.
                       -SNL

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-10-26 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls