This article studies the main Python port scanning program, the specific example code is as follows.
Let's start with the first port scanner code.Get the IP and port number of the local machine.
import socket def get_my_ip(): try: csock = (socket.AF_INET, socket.SOCK_DGRAM) (('8.8.8.8', 80)) (addr, port) = () () return addr,port except : return "127.0.0.1" def int_to_ip(int_ip): return socket.inet_ntoa(('I', (int_ip))) def ip_to_int(ip): return (("I", socket.inet_aton(str(ip)))[0]) (ip,port)=get_my_ip() print "ip=%s port=%d" %(ip,port)
#!/usr/bin/python # -*- coding: utf-8 -*- import optparse from socket import * from threading import * screenLock = Semaphore(value=1) def connScan(tgtHost, tgtPort): try: connSkt = socket(AF_INET, SOCK_STREAM) ((tgtHost, tgtPort)) ('ViolentPython\r\n') results = (100) () print '[+] %d/tcp open' % tgtPort print '[+] ' + str(results) except: () print '[-] %d/tcp closed' % tgtPort finally: () () def portScan(tgtHost, tgtPorts): try: tgtIP = gethostbyname(tgtHost) except: print "[-] Cannot resolve '%s': Unknown host" %tgtHost return try: tgtName = gethostbyaddr(tgtIP) print '\n[+] Scan Results for: ' + tgtName[0] except: print '\n[+] Scan Results for: ' + tgtIP setdefaulttimeout(1) for tgtPort in tgtPorts: t = Thread(target=connScan,args=(tgtHost,int(tgtPort))) () def main(): parser = ('usage %prog '+\ '-H <target host> -p <target port>') parser.add_option('-H', dest='tgtHost', type='string',\ help='specify target host') parser.add_option('-p', dest='tgtPort', type='string',\ help='specify target port[s] separated by comma') (options, args) = parser.parse_args() tgtHost = tgtPorts = str().split(',') if (tgtHost == None) | (tgtPorts[0] == None): print exit(0) portScan(tgtHost, tgtPorts) if __name__ == '__main__': main()
Python socket-based port scanning program
#------------------------------------------------------------------------------- # Name: PortScan # Purpose: Scanning for port openings on network segment hosts # Author: Hao Chen # Python3.4 #------------------------------------------------------------------------------- import socket def main(): ip_start=input('Please enter startIP:(default (setting):127.0.0.1)') if ip_start=='': ip_start='127.0.0.1' ip_end='127.0.0.1' else: ip_end=input('Please enter the end IP:') if ip_end=='': ip_end='127.0.0.1' s=input('Please enter the start port of the target host:(default (setting)扫描常用端口)') if s=='': portList=[21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015] else: startport=int(s) s=input('Please enter the destination host end port:(default (setting):65535)') if s=='': endport=65535 else: endport=int(s) portList=[i for i in range(startport,endport+1)] while 1: #ip_start<ip_end x1=ip_start.rfind('.'); x2=ip_end.rfind('.') if int(ip_start[x1+1:])>int(ip_end[x2+1:]): break; # Start scanning for ports for port in portList: print('Scanning %s : %d' %(ip_start,port)) try: sk = (socket.AF_INET, socket.SOCK_STREAM) (10) ((ip_start,port)) (None) print('Server %s port %d OK!' % (ip_start,port)) () # Results saved in files f=open("IP_Port.txt",'a') (ip_start+' : '+str(port)+'\n') () except Exception: print('Server %s port %d is not connected!' % (ip_start,port)) #Update ip_start i=ip_start.rfind('.') x=int(ip_start[i+1:])+1 ip_start=ip_start[:i+1]+str(x) print('Scan completed, results saved in IP_Port.txt file') if __name__ == '__main__': main()
summarize
Above is this article on the Python socket-based port scanning program example code of the entire content, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!