python自定义端口扫描程序,可以指定地址和端口号
这是一个简单的网络程序,可以用来扫描指定ip的常用端口
""" 皮蛋编程(https://www.pidancode.com) 创建日期:2022/4/1 功能描述:python自定义端口扫描程序,可以指定地址和端口号 """ from socket import * tgtHost = input("请输入要扫描的ip地址: ") tgtPorts = input("请输入要扫描的端口号,多个端口使用逗号分开:") def connScan(tgtHost, tgtPort): try: connSkt = socket(AF_INET, SOCK_STREAM) connSkt.settimeout(10) connSkt.connect((tgtHost, tgtPort)) connSkt.settimeout(None) print("%d/tcp open" % tgtPort) except: print("%d/tcp closed" % tgtPort) def portScan(tgtHost, tgtports): global tgtPorts if tgtPorts == "yes" or tgtPorts == "ya" or tgtPorts == "sure" or tgtPorts == "ok" or tgtPorts == "y" or tgtPorts == "Y": tgtPorts = 21, 22, 23, 25, 42, 43, 53, 67, 79, 80, 102, 110, 115, 119, 123, 135, 137, 143, 161, 179, 379, 389, 443, 445, 465, 636, 993, 995, 1026, 1080, 1090, 1433, 1434, 1521, 1677, 1701, 1720, 1723, 1900, 2409, 3101, 3306, 3389, 3390, 3535, 4321, 4664, 5190, 5500, 5631, 5632, 5900, 7070, 7100, 8000, 8080, 8799, 8880, 9100, 19430, 39720 try: tgtIP = gethostbyname(tgtHost) except: print(" Cannot resolve '%s': Unknown host" % tgtHost) return try: tgtName = gethostbyaddr(tgtIP) print('\n Scan results for: %s' % tgtName) except: print('\n Scan results for: %s' % tgtIP) tgtPorts = tgtPorts.split(',') for port in tgtPorts: print('Scanning port %s' % port) connScan(tgtHost, int(port)) def main(): portScan(tgtHost, tgtPorts) if __name__ == '__main__': main()
代码在python3.9环境下测试通过。
相关文章