【python】批量掃描IP地址80/8080埠
阿新 • • 發佈:2019-02-04
主要是在實習當中用到的IP埠掃描的一個操作,python語言,輸出到txt文件中,同時顯示在dos輸出視窗上。
程式碼:
檔名:port_scan.py
程式碼注意事項:1.寫入的那個txt檔案前面是/而不是\不然容易當成轉義字元#!/usr/bin/env python import socket def check_port(address,port): s=socket.socket() try: s.connect((address,port)) return True except socket.error,e: return False def check_server(address, start_port, end_port): port = start_port while(port<=end_port): check = check_port(address, port) if (check): f1.write('Server %s Port %d is on' % (address, port)+"\n") print 'Server %s Port %d is on' % (address, port) port=port+1 if __name__=='__main__': f1 = open("C:\Users\Administrator\Desktop\saomiao/xxx.txt", "a") from optparse import OptionParser parser=OptionParser() parser.add_option("-a","--address",dest="address",default='localhost',help="Address for server",metavar="ADDRESS") parser.add_option("-f","--file",dest="file",default='',help="Address list",metavar="LIST") parser.add_option("-s","--start",dest="start_port",type="int",default=80,help="start port",metavar="SPORT") parser.add_option("-e","--end",dest="end_port",type="int",default=80,help="end port",metavar="EPORT") (options,args)=parser.parse_args() print 'options: %s, args: %s' % (options, args) port=options.start_port if (options.file == ''): check_server(options.address, options.start_port, options.end_port) else: f = open(options.file,'r') currentServer = f.readline().strip() while(currentServer): #print 'server %s' % currentServer check_server(currentServer, options.start_port, options.end_port) currentServer = f.readline().strip() f.close() f1.close()
使用方法:
1.dos中進入port_scan.py路徑,用cd(空格)路徑名稱
2.輸入命令python port_scan.py -f ip.txt (ip.txt是你需要掃描的ip清單)
3.靜靜等待就可以了,會顯示出開放的埠,如掃8080,只要把上面的80都改成8080即可