Python Scapy实现Ping扫描

#!/usr/bin/python3
# -*- coding: utf-8 -*- 
# --author:valecalida--
from scapy.layers.inet import IP, ICMP, sr1
from random import randint
from ipaddress import ip_network
from threading import Thread
import time


def ping_single(ip):
    ip_id = randint(1, 65535)
    icmp_id = randint(1, 65535)
    icmp_seq = randint(1, 65535)
    packet = IP(dst=ip, ttl=64, id=ip_id)/ICMP(id=icmp_id, seq=icmp_seq)
    response = sr1(packet, timeout=1, verbose=0)
    if response:
        print("[+] %s is alive" % str(ip))


def ping_scan(network):
    ip_list = ip_network(network)
    for ip in ip_list:
        t = Thread(target=ping_single, args=[str(ip)])
        t.start()


if __name__ == '__main__':

    host = '10.87.51.0/24'
    t1 = time.time()
    ping_scan(host)
    t2 = time.time()
    print("[+] 本次扫描共花费 %s 秒" % (t2 - t1))

运行结果:

[+] 10.87.51.1 is alive
[+] 10.87.51.3 is alive
[+] 本次扫描共花费 9.59500789642334 秒

花费时间还是比较长,下一版考虑优化

更多请关注:

https://valecasec.github.io/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值