python-多线程单IP扫描器和IP段扫描器

闲的蛋疼话几十分钟写的,IP段扫描器那个生成IP段是网上抄来的,实在叼。我花了好久才弄懂。
代码很短也很简单,我就直接帖上来了

代码:

单IP扫描:

import socket,sys
import threading
socket.setdefaulttimeout(2)
def Scan(ipaddr,port):
    try:
        conn = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        conn.connect((ipaddr,port))
        conn.send('BakaBaka')
        results = conn.recv(100)
        print('[+] %s %d TCP-Port is open\r\n'%(ipaddr,port))
        print('[*] '+str(results))
        conn.close()
        f.write('[+] %s %d TCP-Port is open\r\n'%(ipaddr,port))
        f.write('[*] '+str(results)+'\r\n')
    except:
        print('[+] %s %d TCP-Port is close\r\n'%(ipaddr,port))
        return 'error'
def Port(strPort,endPort,ipaddr):
    results = []
    for a in range(strPort,endPort+1):
        t =threading.Thread(target=Scan,args=(ipaddr,a))
        t.start()
if __name__ == '__main__':

    print('Use it: PythonProgram.py ipAddr strPort endPort')
    print('Such As: main.py 127.0.0.1 80 90')
    if len(sys.argv) == 4:
        f = open('results.txt',"w")
        Port(int(sys.argv[2]),int(sys.argv[3]),sys.argv[1])

IP段扫描:

#encoding: UTF-8
import socket,sys,threading
socket.setdefaulttimeout(2)
def Scan(ipaddr,port):
    try:
        conn = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        conn.connect((ipaddr,port))
        conn.send('BakaBaka')
        results = conn.recv(100)
        print('[+] %s %d TCP-Port is open\r\n'%(ipaddr,port))
        print('[*] '+str(results))
        conn.close()
        f.write('[+] %s %d TCP-Port is open\r\n'%(ipaddr,port))
        f.write('[*] '+str(results)+'\r\n')
    except:
        print('[+] %s %d TCP-Port is close\r\n'%(ipaddr,port))
def ip2num(ip):
    ip=[int(x) for x in ip.split('.')]
    return ip[0] <<24 | ip[1]<<16 | ip[2]<<8 |ip[3]
def num2ip(num):
    return '%s.%s.%s.%s' %( (num & 0xff000000) >>24,
                            (num & 0x00ff0000) >>16,
                            (num & 0x0000ff00) >>8,
                            num & 0x000000ff )
def get_ip(ip):
    start,end = [ip2num(x) for x in ip.split('-') ]

    return [ num2ip(num) for num in range(start,end+1) if num & 0xff ]
if __name__ == '__main__':
    f = open('result.txt','w')
    if(len(sys.argv) != 3):
        print("Use it: Program.py ipaddr-ipaddr port1")
        print('Such As: 192.168.1.101-192.168.2.1 80')
        sys.exit(0)
    else:
        iplist = get_ip(sys.argv[1])
        for i in iplist:
            t = threading.Thread(target=Scan,args=(i,int(sys.argv[2])))
            t.start()
Tags: python

已有 2 条评论 »

    1. ShellV 2016-05-31 at 00:05

      来自某本书

      1. ⑨BIE 2016-06-01 at 21:06

        请务必告诉我!!!QVQ

添加新评论 »