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

时间:2017-08-18 08:22:47

标签: python-2.7 nmap

非常感谢您的建议如何在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底线是如何输入IP部分在nmap()

1 个答案:

答案 0 :(得分:0)

我明白了。需要先把论点放在一边:

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