内网主机存活检测程序
源码
from scapy.all import *
from scapy.layers.inet import *
import logging
# 防止扫描的主机未存活时报错
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
src = input("输入你的 ip 地址:")
# 默认掩码为 24 位
net_cut = src.split(".")
net = ""
for i in range(3):
# 输出 ip 前 24 位
net += net_cut[i]+"."
for i in range(1,255):
# ip 前 24 位加 1~254 遍历内网内所有 ip
dst = net+f"{i}"
# 构造 icmp 报文
pkt=IP(src=src,dst=dst)/ICMP()
# 发送数据包,并设置超时时间,去除多余输出
res = sr1(pkt,timeout = 0.2,verbose=False)
# 如果收到包并且状态码为 0,则确认存活,输出结果
if res and res.type==0:
print(f"[+] {dst} is alive")
else:
pass