Open Port Scanner Script in Python/Perl

0
142
Open Port Scanner Script in Python/Perl
Open Port Scanner Script in Python/Perl

Hello friends as i am new to Python Programming language and am still in the process of learning it so i thought of creating some tools for all the penetration testers out there so it can help them all the information shared in this article is only for educational purposes and should not be used for malicious tasks .

Python Code For Port Scanner

#!/usr/bin/env python
#!Developer Osama Mahmood
#!Email : [email protected]
#!Website : http://securitytraning.com

from socket import *
import os

if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
print "*******************************************"
print "************Simple Port Scanner************"
print "*******************************************"

if __name__ == '__main__':
targetserver = raw_input('Enter host to scan: ')
targetIP = gethostbyname(targetserver)
print 'Ready to scan :3 ', targetIP

#scan reserved ports
for i in range(1, 1025):
s = socket(AF_INET, SOCK_STREAM)

result = s.connect_ex((targetIP, i))

if(result == 0) :
print 'Port %d: OPEN' % (i,)
s.close()

print '***********************************************'
print "Scanning finished"
print '***********************************************'

Perl Code For Port Scanner

#!/usr/bin/perl
#Osama Mahmood
# TCP Port scanner

use IO::Socket;

# flush the print buffer immediately
$| = 1;

# Take input from user - hostname, start port , end port
print "Enter Target/hostname : ";

# Need to chop off the newline character from the input
chop ($target = <stdin>);
print "Start Port : ";
chop ($start_port = <stdin>);
print "End Port : ";
chop ($end_port = <stdin>);

# start the scanning loop
foreach ($port = $start_port ; $port <= $end_port ; $port++)
{
#\r will refresh the line
print "\rScanning port $port";

#Connect to port number
$socket = IO::Socket::INET->new(PeerAddr => $target , PeerPort => $port , Proto => 'tcp' , Timeout => 1);

#Check connection
if( $socket )
{
print "\r = Port $port is open.\n" ;
}
else
{
#Port is closed, nothing to print
}
}

print "\n\nFinished Scanning $target\n";

exit (0);

So i hope that you will like this and do tell us how did it worked and if there should be any improvement that should be made to improve the code.

THanks and stay tuned for more.

LEAVE A REPLY

Please enter your comment!
Please enter your name here