在Python中使用NMAP时如何跳过IP?
-
12-12-2019 - |
题
我在python中使用nmap,并尝试使用文本文件扫描网络。所有扫描范围都在文本文件中,如:
192.168.1.1-100 192.168.1.120-200 ... 虽然,让我们说扫描没有找到Host 192.168.1.3,因为它是脱机。该计划将崩溃。有没有办法,我可以绕过这种崩溃?我可以使用像尝试/捕获的东西吗?
谢谢, 杰伊
counter = 0
with open('range.txt') as rangefile:
content = rangefile.readlines()
while counter < len(content):
nm = nmap.PortScanner()
#define the nmap scan here
nm.scan(content[counter], '517', '-sU -sT')
.
这是代码的样本
File "c:\...\nmapscan.py", line 63, in <module> therehost = Host.objects.get(ipv4_address=hosts) va.assessment.models.DoesNotExist: Host matching query does not exist. Lookup parameters were {'ipv4_address': u'134.250.16.103'}
.
这是错误
解决方案
nmap
需要两个参数进行排除。--exclude
占主机名(S)和--excludefile
采用包含需要排除的主机名称的文件。使用其中一个作为您的需求。
有关更多关于设置目标,请参阅 man 页面。
这是我的测试结果 -
Python 3.2.3 (default, May 3 2012, 15:54:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nmap
>>> nm=nmap.PortScanner()
>>> nm.scan('134.250.16.103','517', '-sU -sT')
{'nmap': {'scanstats': {'uphosts': '0', 'timestr': 'Sat Jul 28 12:54:27 2012', 'downhosts': '1', 'totalhosts': '1', 'elapsed': '3.06'}, 'scaninfo': {'udp': {'services': '517', 'method': 'udp'}, 'tcp': {'services': '517', 'method': 'connect'}}, 'command_line': 'nmap -oX - -p 517 -sU -sT 134.250.16.103'}, 'scan': {'134.250.16.103': {'status': {'state': 'down', 'reason': 'no-response'}, 'hostname': ''}}}
>>>
.
您可以使用try-catch -
try:
nm.scan(content[counter], '517', '-sU -sT')
except:
#handle exception...
.
因为您没有哪个服务器,您可以在继续前进到NMAP扫描之前ping服务器。
不隶属于 StackOverflow