python最大的特點就是各種強大的模塊實現自己想要的功能。這裡介紹一個強大的工具––nmap。
windows環境下要先安裝nmap,地址
https://nmap.org/download.html,安裝 nmap-7.80-setup.exe,下面會用到。然後pip install nmap安裝python模塊。利用tkinter實現圖形化界面。功能實現程序:
<code>import
tkinteras
tkimport
nmap topwin = tk.Tk() topwin.title("ipScanV1.0"
) topwin.geometry("400x200+20+20"
) topwin.attributes("-alpha"
,1
) topwin["bg"
] ="snow"
topwin.resizable(False
,False
) infotext = tk.Text(topwin,bg ="Turquoise"
) scrobar = tk.Scrollbar(infotext) scrobar.pack(side="right"
, fill="y"
) scrobar.config(command=infotext.yview) infotext.config(yscrollcommand=scrobar.set) infotext.tag_configure('color'
, foreground='Yellow'
,background ="RoyalBlue"
) infotext.place(x=4
,y=40
,width =400
,height =160
) ipscanset = tk.Label(topwin,text ="輸入掃描IP段:"
).place(x=10
,y=10
) iptext = tk.Entry(topwin) iptext.place(x=100
,y=10
,width =140
,height =24
,anchor ="nw"
)def
ipscan
()
: ipyn = nmap.PortScanner(nmap_search_path=('nmap'
,r'C:\Program Files\Nmap\nmap.exe'
)) ip = iptext.get()if
len(ip) ==0
: infotext.insert(tk.END,"請輸入正確IP或IP段!\n"
) infotext.insert(tk.END,"...............................\n"
)else
: ping_scan = ipyn.scan(hosts=ip,arguments="-sn"
) infotext.insert(tk.END,"...............................\n"
)if
len(ping_scan['scan'
]) ==0
: infotext.insert(tk.END,"%s該網段無設備在線!\r\n"
%(ip))else
:for
resultin
ping_scan['scan'
].keys(): infotext.insert(tk.END,"%s 設備在線!\r\n"
%(result)) scanbtn = tk.Button(topwin,text ="掃描"
,command = ipscan).place(x=250
,y=10
,width =50
,height =24
) topwin.mainloop() /<code>
注意:nmap.PortScanner(nmap_search_path=('nmap',r'C:\Program Files\Nmap\nmap.exe'))一定要按照自己安裝的路徑添加,否則報錯。
軟件實現效果如下圖所示: