'''这是一个端口全链接扫描的脚本,扫描结果会比较准确,可是比较费时间''' '''运行环境 Python3 ''' from socket import * def portScanner(host,port): try: s = socket(AF_INET,SOCK_STREAM) #注意参数 s.connect((host,port)) #注意括号 (host,port) print('[+] %d open' % port) s.close() except: #若是端口链接失败,则输出$port close print('[-] %d close' % port) def main(): setdefaulttimeout(1) ports = [20, 22, 23, 80, 111, 3306] #定义要扫描的端口,也能够在下面定义 for p in range(1,1024): for p in ports: portScanner('192.168.60.130',p) #设置要扫描的主机为192.168.60.130 if __name__ == '__main__': #“Make a script both importable and executable” main() # 若是这文件中的代码被外部的python文件调用是不会被执行的
ps: 关于Python中的 if __name__ == '__main__'
的解释能够参考:https://www.cnblogs.com/kex1n/p/5975575.htmlhtml