扫描所有端口
import socket, threading, os, timedef port_thread(ip, start, step, timeout):for port in range(start, start + step):s = socket.socket()s.settimeout(timeout)try:s.connect((ip, port))print(f"port[{port}] 可用")except Exception as e:passfinally:s.close()def port_scan(ip, start=1, end=65536, step=100, timeout=0.5):for p in range(start, end, step):threading.Thread(target=port_thread, args=(ip, p, step, timeout)).start()if __name__ == '__main__':ip = '192.168.110.130'port_scan(ip=ip, timeout=0.2)
扫描常见端口
def port_scan(ip, timeout=0.5):port_list = [7, 21, 22, 23, 25, 43, 53, 67, 68, 69, 79, 80, 81, 88, 109, 110, 113, 119, 123, 135, 135,137, 138, 139, 143, 161, 162, 179, 194, 220, 389, 443, 445, 465, 513, 520, 520, 546, 547,554, 563, 631, 636, 991, 993, 995, 1080, 1194, 1433, 1434, 1494, 1521, 1701, 1723, 1755,1812, 1813, 1863, 3269, 3306, 3307, 3389, 3544, 4369, 5060, 5061, 5355, 5432, 5671, 5672, 6379,7001, 8080, 8081, 8088, 8443, 8883, 8888, 9443, 9988, 9988, 15672, 50389, 50636, 61613, 61614]for port in port_list:s = socket.socket()s.settimeout(timeout)try:s.connect((ip, port))print(f"port[{port}] 可用")except Exception as e:passfinally:s.close()if __name__ == '__main__':ip = '192.168.110.130'port_scan(ip=ip, timeout=0.2)