本文将利用python调用nmap完成扫描,并将扫描结果通过表格的方式展示出来。以便扫描者更加清晰明了的观察扫描结果。

python nmap

实验环境

  • Python3
  • nmap7.9

代码如下:

#!/usr/bin/env pthon
#--*--coding=utf-8--*--
#kali blog(https://blog.bbskali.cn)
#逍遥子大表哥
import os
from terminaltables import AsciiTable
 
gateway = input('YOU IP Address:')
print ("\033[31m稍等 正在扫描\033[0m")
print ("当前网关变量为:",gateway)
def scan():
    scan = os.popen("nmap " + gateway + " -n -P").read()
    f = open('scan.txt','w')
    f.write(scan)
    f.close()
    devices = os.popen(" grep report scan.txt | awk '{print $5}' ").read()
    
    devices_mac = os.popen("grep MAC scan.txt | awk '{print $3}'").read() + os.popen("ip addr | grep 'state UP' -A1 | tail -n1 | awk '{print $2}' | cut -f1  -d'/' ").read().upper() # get devices mac and localhost mac address
    devices_name = os.popen("grep MAC scan.txt | awk '{print $4 ,S$5 $6}'").read() + "\033[1;32m(本机)\033[1;m"
    port = os.popen(" grep tcp scan.txt -A 0  | awk '{print $1,$2}'  | cut -f1  -d'/' ").read()
    table_data = [
        ['IP Address', 'Mac Address', 'Manufacturer','port'],
        [devices, devices_mac, devices_name,port]
    ]
    table = AsciiTable(table_data)
    print(table.table)
    os.popen("rm -f scan.txt")
 
if __name__ == '__main__':
    scan()

扫描结果

nmap扫描结果

视频演示

Last modification:August 7th, 2021 at 10:08 pm
正在沿街乞讨中……