使用nmap端口扫描的代码,在kali linux系统中运行?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用nmap端口扫描的代码,在kali linux系统中运行?相关的知识,希望对你有一定的参考价值。
源码:
import nmap
import optparse
def nmapScan(tgtHost,tgtPort):
nmScan = nmap.PortScanner()
nmScan.scan(tgtHost,tgtPort)
state = nmScan [tgtHost] ['tcp'] [int(tgtPort)] ['state']
print("[*]" + tgtHost + " tcp/" + tgtPort + " " + state)
def main() :
parser = optparse.OptionParser('usage%prog ' +\
'-H <target host> -P <target port>')
parser.add_option('-H', dest='tgtHost', type ='string', \
help='specify target host')
parser.add_option('-P', dest='tgtPort', type='string', \
help='specify target port[s] separated by comma')
(options, args) = parser.parse_args()
tgtHost = options.tgtHost
tgtPorts= str(options.tgtPort).split(' ')
if(tgtHost ==None) | (tgtPorts[0] ==None) :
print (parser.usage)
exit(0)
for tgtPort in tgtPorts :
nmapScan(tgtHost, tgtPort)
if __name__ =='__main__' :
main()
报错:
└─$ python nmapScan.py -H 10.50.60.125 -P 21
Traceback (most recent call last):
File "nmapScan.py", line 27, in <module>
main()
File "nmapScan.py", line 24, in main
nmapScan(tgtHost, tgtPort)
File "nmapScan.py", line 7, in nmapScan
state = nmScan [tgtHost] ['tcp'] [int(tgtPort)] ['state']
File "/usr/local/lib/python2.7/dist-packages/nmap/nmap.py", line 568, in __getitem__
return self._scan_result['scan'][host]
KeyError: '10.50.60.125'
查了一下报错文件: File "/usr/local/lib/python2.7/dist-packages/nmap/nmap.py", line 568, in __getitem__
报错代码:
def __getitem__(self, host):
"""
returns a host detail
"""
if sys.version_info[0]==2:
assert type(host) in (str, unicode), 'Wrong type for [host], should be a string
[was 0]'.format(type(host))
else:
assert type(host) is str, 'Wrong type for [host], should be a string [was
0]'.format(type(host))
return self._scan_result['scan'][host]
不知道是哪里错了,求大神指点!
kali NMAP端口扫描工具
NMAP扫描服务器端口开放情况
用法:
1.扫描单个网址或IP地址
如 :nmap 192.168.2.1/nmap www.xx.com
用UDP协议扫nmap -sU 192.168.2.1/nmap www.xx.com
显示如下:
Starting Nmap 7.91 ( https://nmap.org ) at 2021-11-09 01:47 EST
Nmap scan report for 192.168.2.1
Host is up (0.0080s latency).
Not shown: 993 closed ports
PORT STATE SERVICE
7/udp open|filtered echo
13/udp open|filtered daytime
19/udp open|filtered chargen
67/udp open|filtered dhcps
68/udp open|filtered dhcpc
MAC Address: A4:xx:xx:57:xx:6xx
2.扫描内网整个网段
如 nmap 192.168.12.0/24
3.扫描多个主机
nmap 192.168.2.10 192.168.1.20
以上是关于使用nmap端口扫描的代码,在kali linux系统中运行?的主要内容,如果未能解决你的问题,请参考以下文章