Python3利用scapy局域網實現自動多線程arp掃描功能
from scapy.all import *import threading二、實現ip掃描
1.獲取c段ip地址
在ARP()里面有ip地址,我們可以從里面提取出前3段出來
ARP().show()
然后通過從后查找最后一個.得到最后一段位數,然后總長度-最后一段長度就能取出前3段
tip=ARP().psrcprint(tip[:(len(tip)-tip[::-1].find(’.’))])
2.arp掃描函數實現
然后就是建立函數實現掃描了,構造arp包->發送包->判斷是否響應->輸出信息
def ScanIp(ip):pkt=Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip)res=srp1(pkt,timeout=10,verbose=0)if res:print(res.psrc)print(res.hwsrc)
然后來在加個判斷返回的ip跟我們要掃描的ip是否一致,然后加上異常處理
def ScanIp(ip):pkt=Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip)try:res=srp1(pkt,timeout=10,verbose=0)if res.psrc==ip:print(res.psrc)print(res.hwsrc)except:pass
現在把輸出結果美化一下,不然直接print很難看
def ScanIp(ip):pkt=Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip)try:res=srp1(pkt,timeout=10,verbose=0)if res.psrc==ip:print(’IP MAC’)print(’[+]’+res.psrc+’ ’+res.hwsrc)except:pass
嘗試調用一下
ScanIp(’192.168.123.1’)
現在看起來就很舒服
3.多線程
現在我們只需要循環一下c段ip然后用多線程跑起來就行了
for i in range(1,256):ip=tip+str(i)Go=threading.Thread(target=ScanIp,args=(ip,))Go.start()
然后看一下效果好像不是我們想要的因為IP MAC輸出了很多次看起來很難受
然后這里把輸出移動到函數外的for循環上方,然后判斷一下__name__,這樣就完成了所有的功能了
from scapy.all import *import threading tip=ARP().psrctip=tip[:(len(tip)-tip[::-1].find(’.’))] def ScanIp(ip):pkt=Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip)try:res=srp1(pkt,timeout=10,verbose=0)if res.psrc==ip:print(’[+]’+res.psrc+’ ’+res.hwsrc)except:pass if __name__==’__main__’:print(’IP MAC’)for i in range(1,256):ip=tip+str(i)Go=threading.Thread(target=ScanIp,args=(ip,))Go.start()
運行效果
到此這篇關于Python3利用scapy局域網實現自動多線程arp掃描功能的文章就介紹到這了,更多相關Python scapy實現arp掃描內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: