如何在 nmap python 脚本中扫描 IP 范围内的主机

how to exclude Hosts from IP range from scanning inside nmap python script

非常感谢您的建议如何在 nm.scan() 内排除主机扫描。我有以下脚本,当我输入范围时它可以完美运行:例如 10.0.0.0/24

import sys
import os
import subprocess
import csv
import nmap                         # import nmap.py module
try:
    nm = nmap.PortScanner()         # instantiate nmap.PortScanner object
except nmap.PortScannerError:
    print('Nmap not found', sys.exc_info()[0])
    sys.exit(0)
except:
    print("Unexpected error:", sys.exc_info()[0])
    sys.exit(0)

file = raw_input('\nEnter the name of the file where the scan will be saved/add .csv/: ')
ip_range = raw_input('\nEnter the IP range you want to scan/in the following foramt:x.x.x.x/mask: ')
nmap_arguments= raw_input('\nEnter the nmap arguments : ')
nm.scan(hosts=ip_range, arguments= nmap_arguments)
nm.command_line()                   # get command line used for the scan
nm.scaninfo()                       # get nmap scan informations {'tcp': {'services': '22-443', 'method'nect'}}
nm.all_hosts()                      # get all hosts that were scanned


if (len(sys.argv) > 1 and sys.argv[1]):
    save_csv_data(nm.csv(), path=sys.argv[1])
else:
    save_csv_data(nm.csv())

print "Completed!"

但是例如,如果我想扫描范围但排除 2 个主机,当我输入 :nm.scan(hosts='10.0.0.0/24 --exclude 10.0.0.1, 10.0.0.2, arguments= nmap_arguments) - 它仅排除 10.0.0.1 但仍在扫描 10.0.0.2.So 底线是如何在nmap()

里面输入IP部分

我让 it.Needed 把参数放在第一位:

nm.scan(arguments='-sT --open --exclude X.X.X.X,X.X.X.X',hosts='X.X.X.0/24')