吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1461|回复: 0
收起左侧

[Python 转载] python批量扫描在线主机

[复制链接]
mstscexe 发表于 2021-11-26 15:22
本帖最后由 mstscexe 于 2021-11-26 16:40 编辑

1、功能
      实现根据指定端口批量扫描主机在线情况。
      非传统ping方式,采用scapy tcp检测方法。
2、结果 image.png

3、代码
     IP少可以修改iplist
     IP较多可修改为host.txt方式
     取消注释 for ipaddr in host:
     注释:for ipaddr in iplist:
4、运行环境需要安装
pip install scapy
npcap https://nmap.org/npcap/dist/npcap-1.55.exe
[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# -- coding: utf-8 --
import sys,os,time,datetime
from scapy.all import *
ports = 2202
iplist = ['14.215.177.39',
'14.215.177.48',
'14.215.177.53',
'14.215.177.46',
'14.215.177.54',
'14.215.177.55']
online_num = 0
offline_num = 0
# msgbox
def msgbox(ip,ports,state):
    if state == "Offline":
        print("\n%s:%s -- \033[1;31;40m %s \033[0m\n" % (ip, ports, state))
    else:
        print("\n%s:%s -- \033[1;31;32m %s \033[0m\n" % (ip, ports, state))
def clog(ip,state):
    if state == "Offline":
        os.system('echo '+ ip + ' -- Offline >> offlinehost.log')
    elif state == "Online":
        os.system('echo '+ ip + ' -- Online >> onlinehost.log')
    else:
        os.system('echo  > offlinehost.log')
        os.system('echo  > onlinehost.log')
# scan
def online_check(iplist,ports,online_num,offline_num):
    for ipaddr in iplist: #iplist
    #for ipaddr in host: #host.txt
        pkt = sr1(IP(dst=ipaddr)/TCP(dport=ports, flags="S"), timeout=2)
        if pkt == None:
            msgbox(ipaddr,ports,"Offline")
            clog(ipaddr,"Offline")
            offline_num += 1
        #elif pkt.haslayer(TCP):
        elif pkt.getlayer(TCP).flags == 0x12:
            send_rst = sr(IP(dst=ipaddr)/TCP(dport=ports, flags="AR"), timeout=2)
            msgbox(ipaddr,ports,"Online")
            clog(ipaddr,"Online")
            online_num += 1
        elif pkt.getlayer(TCP).flags == 0x14:
            msgbox(ipaddr,ports,"Offline")
            clog(ipaddr,"Offline")
            offline_num += 1
    return (online_num,offline_num)
# online_check
if __name__ == "__main__":
    mtime = time.strftime('%Y-%m-%d %H:%M:%S')
    clog(0,"")
    print("---------------------------------------------Ready start scanning---------------------------------------------")
    print(mtime)
    try:
        f =open("host.txt",'r')
        f.close()
    except IOError:
        f = open("host.txt",'w')
    f = open('host.txt')
    host = f.read().splitlines()
    f.close()
    run = online_check(iplist,ports,online_num,offline_num)
    run
    online_num = run[0]
    offline_num = run[1]
    count_host = online_num + offline_num
    print("---------------------------------------------------Scan done---------------------------------------------------")
    print(mtime)
    print("\nCount Scan host: \033[1;31;33m %s \033[0m, Online: \033[1;31;32m %s \033[0m , Offline: \033[1;31;40m %s \033[0m , More info please see offlinehost.log/onlinehost.log.\n\n" % (count_host, online_num, offline_num))
    os.system("pause")


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-29 03:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表