python呼叫nmap掃瞄區域網存活主機和埠

2021-10-07 13:09:52 字數 3158 閱讀 8681

執行結果

參考文章

python2

sudo apt install nmap

pip install nmap json

sudo python2 scan.py

**經過除錯,使用root可直接執行

#!python2 

# scan.py must be sudo to run

import nmap

import json

nm = nmap.portscanner()

iprange = raw_input("input ip range(eg:192.168.224.0/24)>")

port = raw_input("input target port(eg:8888)>")

if not port:

port = 8888

if not iprange:

iprange = "192.168.224.0/24"

print("scan ip range " + iprange)

a = nm.scan(iprange, str(port), '-ss -o')

a = a.get("scan")

target = 0

for key, value in a.items():

if value.get("tcp").get(port).get("state") == "open":

print "target ip maybe :" + key

print "some information"

print value

target = target + 1

if target == 0:

print "no target machine detectd...."

請查閱參考文件一,有詳細的解釋

返回的結果a形式如下:a是個字典

, 'scaninfo': }, 'command_line': 'nmap -ox - -p 8888 -ss -o 192.168.224.0/24'}, 'scan': , 'uptime': , 'vendor': {}, 'addresses': , 'tcp': }, 'hostnames': , 'osmatch': , 'line': '50174', 'name': 'linux 2.6.32', 'accuracy': '100'}], 'portused': [, ]}, '192.168.224.1': , 'vendor': , 'addresses': , 'tcp': }, 'hostnames': , 'osmatch': , 'portused': }, '192.168.224.2': , 'vendor': , 'addresses': , 'tcp': }, 'hostnames': , 'osmatch': , 'line': '1615', 'name': 'aethra starvoice 1042 adsl router', 'accuracy': '91'}, ], 'line': '2920', 'name': 'apc aos 5', 'accuracy': '91'}, ], 'line': '9384', 'name': 'bluebird superdos', 'accuracy': '91'}, ], 'line': '9587', 'name': 'brother nc-130h print server', 'accuracy': '91'}, ], 'line': '9767', 'name': 'brother hl-1870n printer', 'accuracy': '91'}, ], 'line': '9786', 'name': 'brother hl-2070n or mfc-5460cn printer', 'accuracy': '91'}, ], 'line': '9805', 'name': 'brother hl-2070n printer', 'accuracy': '91'}, ], 'line': '10112', 'name': 'brother hl-5070n printer', 'accuracy': '91'}, ], 'line': '10480', 'name': 'brother mfc-7820n printer', 'accuracy': '91'}, ], 'line': '10595', 'name': 'brother mfc-9420cn printer', 'accuracy': '91'}], 'portused': }, '192.168.224.254': , 'vendor': , 'addresses': , 'tcp': }, 'hostnames': , 'osmatch': , 'portused': }}}
為了更好地測試,最好使用jupyter或控制台去單步執行,去使用dict的get方法去逐步解析,防止錯誤

for key, value in a.items():

if value.get("tcp").get(port).get("state") == "open":

print "target ip maybe :" + key

print "some information"

print value

target = target + 1

zhao@zhao:~/desktop/project$ sudo python scan.py

[sudo] password for zhao:

input ip range(eg:192.168.224.0/24)>

input target port(eg:8888)>

scan ip range 192.168.224.0/24

target ip maybe :192.168.224.132

some information

, 'uptime': , 'vendor': {}, 'addresses': , 'tcp': }, 'hostnames': , 'osmatch': , 'line': '50174', 'name': 'linux 2.6.32', 'accuracy': '100'}], 'portused': [, ]}

python中python-nmap模組的使用

nmap命令掃瞄存活主機

python使用nmap埠掃瞄

coding utf 8 usr bin env python import nmap import optparse def nmapscan tgthost,tgtport 建立nmap掃瞄器,初始化portscanner模組 class portscanner init self,nmap s...

Nmap之基礎掃瞄

指定目標掃瞄 1.單個目標掃瞄 由上可知,目標主機172.16.204.208上開啟的埠有3580 8080及占用埠的服務 目標主機的mac位址為68 a3 c4 f5 b3 88 目標主機是活動的 up 掃瞄目標主機的時間為27.48秒。持 ipv6協議 2.多個目標掃瞄 第一台是膝上型電腦,其裝...

nmap系統版本掃瞄

osscan limit 限定有埠開放的主機,提高 o和 a引數的掃瞄速度 osscan guest給nmap建議的作業系統,類似於sqlmap的引數 max os tries 次數 設定重試次數 預設為5 提高準確性和提供掃瞄速度 device type裝置路由印表機 runnung作業系統名猜測...