python端口扫描程序 python socket模块实现端口扫描

发布时间:2020-03-26编辑:脚本学堂
本文分享一个简单的python端口扫描程序,使用python的socket模块实现的,有兴趣的朋友,可以研究下,用来学习python socket编程不错的。

python实现的简单网络程序,可以用来扫描指定ip的常用端口。

代码:
 

复制代码 代码示例:
#!/bin/python
#edit: www.jb200.com
#
from socket import *
tgtHost=raw_input("What is the web address?: ")
tgtPorts=raw_input("What are the ports seperated by commas? or would you like to test all?: ")
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()