Python script Scan remote port

Python script Scan remote port

python

 

#!/usr/bin/env python

import argparse
import socket
import sys

def scan_ports(host, start_port, end_port):
“”” Scan remote hosts “””
#Create socket
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
except socket.error,err_msg:
print ‘Socket creation failed. Error code: ‘+ str(err_msg[0]) + ‘ Error mesage: ‘ + err_msg[1]
sys.exit()
#Get IP of remote host
try:
remote_ip = socket.gethostbyname(host)
except socket.error,error_msg:
print error_msg
sys.exit()
#Scan ports
end_port += 1
for port in range(start_port,end_port):
try:
sock.connect((remote_ip,port))
print ‘Port ‘ + str(port) + ‘ is open’
sock.close()
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
except socket.error:
pass # skip various socket errors

if __name__ == ‘__main__’:
# setup commandline arguments
parser = argparse.ArgumentParser(description=’Remote Port Scanner’)
parser.add_argument(‘–host’, action=”store”, dest=”host”, default=’localhost’)
parser.add_argument(‘–start-port’, action=”store”, dest=”start_port”, default=1, type=int)
parser.add_argument(‘–end-port’, action=”store”, dest=”end_port”, default=100, type=int)
# parse arguments
given_args = parser.parse_args()
host, start_port, end_port = given_args.host,given_args.start_port, given_args.end_port
scan_ports(host, start_port, end_port)

 

root@ubuntu:~/python# python 9_4_scan_port.py –host=localhost –start-port=70 –end-port=85
Port 80 is open
root@ubuntu:~/python#

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>