python网络扫描程序

import re
import subprocess
from netaddr import IPNetwork, AddrFormatError
from prettytable import PrettyTable

def nmap_report(data):
bytes_data = data.decode(‘utf-8’)
mac_flag = “”
ip_flag = “”
host_table = PrettyTable([“IP”, “MAC”, “Vendor”])
numbers_of_hosts = bytes_data.count(‘Host is up’)
for line in bytes_data.split(“\n”):
if “MAC Address:” in line:
mac = line.split(“(”)[0].replace(“MAC Address: “,””)
vendor = line.split(“(”)[1].replace(“)”,“”)
mac_flag = “ready”
elif “Nmap scan report for” in line:
ip = re.search(r"Nmap scan report for (.*)", line).groups()[0]
ip_flag = “ready”

    if mac_flag == "ready" and ip_flag == "ready":
        host_table.add_row([ip, mac, vendor])
        mac_flag = ""
        ip_flag = ""

print("number of live hosts is {}".format(numbers_of_hosts))
print(host_table)

network = “192.168.182.1/24”

try:
IPNetwork(network)
p = subprocess.Popen([“sudo”, “nmap”, “-sP”, network], stdout=subprocess.PIPE)
nmap_report(p.stdout.read())
except AddrFormatError:
print(“please Enter a valid network IP address in x.x.x.x/y format”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值