1. 程式人生 > 其它 >使用python-nmap實現簡單掃描器

使用python-nmap實現簡單掃描器

技術標籤:筆記python

使用python-nmap實現簡單掃描器

使用python版本:2.7.18
linux系統:Ubuntu 20.04

首先需要在PC中安裝nmap

sudo apt install nmap
#!/usr/bin/python
# coding=utf-8
import nmap    # 匯入nmap模組

nm = nmap.PortScanner()   # 建立PortScanner物件,要是PC中沒有nmap會觸發PortScanner異常
nm.scan('www.baidu.com', '1-1024')
for host in nm.all_hosts(
): # 以列表形式返回scan()函式引數指定的主機資訊 print '--------------------------------------------------------------------' print 'Host : {0} ({1})'.format(host, nm[host].hostname()) # 輸出主機與IP名稱 print 'State : {0}'.format(nm[host].state()) # 輸出主機狀態, 若線上則返回up for proto in nm[host].all_protocols(): # 以列表形式顯示主機中掃描的所有協議
print '------------------' print 'Protocol : {0}'.format(proto) lport = list(nm[host][proto]) # 獲取埠資訊 lport.sort() for port in lport: print('port : {0}\tstate : {1}'.format(port, nm[host][proto][port])) # 列印埠資訊 print '--------------------------------------------------------------------'

執行結果
在這裡插入圖片描述