python实现指定ip端口扫描方式
在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实现指定ip端口扫描方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
以上是 python实现指定ip端口扫描方式 的全部内容, 来源链接: utcz.com/z/312255.html