2019python脚本测试汇总

 

201905 作用:端口扫描工作

#!/usr/bin/python-3.7.3/bin/env python3.7
#-*-coding:UTF-8-*-
#端口扫描
import time
import socket
from gevent import monkey
monkey.patch_all()
import gevent
import gevent.pool

def TCP_connect(ip,port):
    """模拟TCP连接"""
    TCP_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    TCP_sock.settimeout(0.5)  #设置连接超时
    try:
        result = TCP_sock.connect_ex((ip,int(port)))
        if result == 0:
            print("[*]\t<%s>\t\t<开启>\t"% port)
        else:
            #print("[!]%s 端口 关闭"% port)
            pass
        TCP_sock.close()
    except socket.error as e:
        print("[!]错误:",e)


def scan_port(ip):
    """扫描端口"""
    print("[*]开始扫描目标端口...")
    print("[*]")
    print("[*]\t端口号:\t\t是否开启:")
    start = time.time()
    g = gevent.pool.Pool(4) #设置线程数
    run_list = []
    for port in [22,23,1521,3306,8080,8088,9092,10050,10051]:
        run_list.append(g.spawn(TCP_connect,ip,port))
    gevent.joinall(run_list)
    end = time.time()
    print("[*]")
    print("[*] 总耗时%s."% time.strftime("%H:%M:%S",time.gmtime(end-start)))

def test_scan():
    hosts_ip = ["10.227.6.11","10.225.134.18","10.227.6.14","10.227.6.15","192.168.9.11","192.168.9.12","192.168.9.13","192.168.9.14"]
    for i in hosts_ip:
        print("[*] 以下是<%s>的端口信息:" % i)
        scan_port(i)
        print("+"*10 + "该主机端口扫描工作结束!" + "+"*10)
    print("end,tks!")


if __name__=="__main__":
    test_scan()
View Code

 

posted on 2019-07-30 11:28  微子天明  阅读(233)  评论(0编辑  收藏  举报

导航