python实现指定ip端口扫描方式
时间:2020-10-20 15:06:19|栏目:Python代码|点击: 次
在Linux中判断一台主机是否可达,可以使用ping命令,而判断端口是否打开,可以使用telnet命令,但是telnet命令没有超时时间的参数,使用起来不是很方便,那么可以利用Python来完成一个端口扫描的功能
socket实现端口扫描
#!/usr/bin/env python import socket def get_ip_status(ip,port): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: server.connect((ip,port)) print('{0} port {1} is open'.format(ip, port)) except Exception as err: print('{0} port {1} is not open'.format(ip,port)) finally: server.close() if __name__ == '__main__': host = '10.0.0.11' for port in range(20,100): get_ip_status(host,port)
telnetlib实现端口扫描
import telnetlib def get_ip_status(ip,port): server = telnetlib.Telnet() # 创建一个Telnet对象 try: server.open(ip,port) # 利用Telnet对象的open方法进行tcp链接 print('{0} port {1} is open'.format(ip, port)) except Exception as err: print('{0} port {1} is not open'.format(ip,port)) finally: server.close() if __name__ == '__main__': host = '10.0.0.11' for port in range(20,100): get_ip_status(host,port)
多线程实现高效扫描
#!/usr/bin/env python import telnetlib import threading def get_ip_status(ip,port): server = telnetlib.Telnet() try: server.open(ip,port) print('{0} port {1} is open'.format(ip, port)) except Exception as err: print('{0} port {1} is not open'.format(ip,port)) finally: server.close() if __name__ == '__main__': host = '10.0.0.11' threads = [] for port in range(20,100): t = threading.Thread(target=get_ip_status,args=(host,port)) t.start() threads.append(t) for t in threads: t.join()
栏 目:Python代码
下一篇:Windows+Anaconda3+PyTorch+PyCharm的安装教程图文详解
本文标题:python实现指定ip端口扫描方式
本文地址:http://www.codeinn.net/misctech/14061.html