python構(gòu)造icmp echo請(qǐng)求和實(shí)現(xiàn)網(wǎng)絡(luò)探測(cè)器功能代碼分享
python發(fā)送icmp echo requesy請(qǐng)求
import socket
import struct
def checksum(source_string):
sum = 0
countTo = (len(source_string)/2)*2
count = 0
while count<countTo:
thisVal = ord(source_string[count + 1])*256 + ord(source_string[count])
sum = sum + thisVal
sum = sum & 0xffffffff
count = count + 2
if countTo<len(source_string):
sum = sum + ord(source_string[len(source_string) - 1])
sum = sum & 0xffffffff
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
answer = ~sum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
def ping(ip):
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, 1)
packet = struct.pack(
"!BBHHH", 8, 0, 0, 0, 0
)
chksum=checksum(packet)
packet = struct.pack(
"!BBHHH", 8, 0, chksum, 0, 0
)
s.sendto(packet, (ip, 1))
if __name__=='__main__':
ping('192.168.41.56')
掃描探測(cè)網(wǎng)絡(luò)功能(網(wǎng)絡(luò)探測(cè)器)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
探測(cè)網(wǎng)絡(luò)主機(jī)存活。
'''
import os
import struct
import array
import time
import socket
import IPy
import threading
class SendPingThr(threading.Thread):
'''
發(fā)送ICMP請(qǐng)求報(bào)文的線程。
參數(shù):
ipPool -- 可迭代的IP地址池
icmpPacket -- 構(gòu)造的icmp報(bào)文
icmpSocket -- icmp套字接
timeout -- 設(shè)置發(fā)送超時(shí)
'''
def __init__(self, ipPool, icmpPacket, icmpSocket, timeout=3):
threading.Thread.__init__(self)
self.Sock = icmpSocket
self.ipPool = ipPool
self.packet = icmpPacket
self.timeout = timeout
self.Sock.settimeout( timeout + 3 )
def run(self):
time.sleep(0.01) #等待接收線程啟動(dòng)
for ip in self.ipPool:
try:
self.Sock.sendto(self.packet, (ip, 0))
except socket.timeout:
break
time.sleep(self.timeout)
class Nscan:
'''
參數(shù):
timeout -- Socket超時(shí),默認(rèn)3秒
IPv6 -- 是否是IPv6,默認(rèn)為False
'''
def __init__(self, timeout=3, IPv6=False):
self.timeout = timeout
self.IPv6 = IPv6
self.__data = struct.pack('d', time.time()) #用于ICMP報(bào)文的負(fù)荷字節(jié)(8bit)
self.__id = os.getpid() #構(gòu)造ICMP報(bào)文的ID字段,無(wú)實(shí)際意義
@property #屬性裝飾器
def __icmpSocket(self):
'''創(chuàng)建ICMP Socket'''
if not self.IPv6:
Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
else:
Sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.getprotobyname("ipv6-icmp"))
return Sock
def __inCksum(self, packet):
'''ICMP 報(bào)文效驗(yàn)和計(jì)算方法'''
if len(packet) & 1:
packet = packet + '\0'
words = array.array('h', packet)
sum = 0
for word in words:
sum += (word & 0xffff)
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
return (~sum) & 0xffff
@property
def __icmpPacket(self):
'''構(gòu)造 ICMP 報(bào)文'''
if not self.IPv6:
header = struct.pack('bbHHh', 8, 0, 0, self.__id, 0) # TYPE、CODE、CHKSUM、ID、SEQ
else:
header = struct.pack('BbHHh', 128, 0, 0, self.__id, 0)
packet = header + self.__data # packet without checksum
chkSum = self.__inCksum(packet) # make checksum
if not self.IPv6:
header = struct.pack('bbHHh', 8, 0, chkSum, self.__id, 0)
else:
header = struct.pack('BbHHh', 128, 0, chkSum, self.__id, 0)
return header + self.__data # packet *with* checksum
def isUnIP(self, IP):
'''判斷IP是否是一個(gè)合法的單播地址'''
IP = [int(x) for x in IP.split('.') if x.isdigit()]
if len(IP) == 4:
if (0 < IP[0] < 223 and IP[0] != 127 and IP[1] < 256 and IP[2] < 256 and 0 < IP[3] < 255):
return True
return False
def makeIpPool(self, startIP, lastIP):
'''生產(chǎn) IP 地址池'''
IPver = 6 if self.IPv6 else 4
intIP = lambda ip: IPy.IP(ip).int()
ipPool = {IPy.intToIp(ip, IPver) for ip in range(intIP(startIP), intIP(lastIP)+1)}
return {ip for ip in ipPool if self.isUnIP(ip)}
def mPing(self, ipPool):
'''利用ICMP報(bào)文探測(cè)網(wǎng)絡(luò)主機(jī)存活
參數(shù):
ipPool -- 可迭代的IP地址池
'''
Sock = self.__icmpSocket
Sock.settimeout(self.timeout)
packet = self.__icmpPacket
recvFroms = set() #接收線程的來(lái)源IP地址容器
sendThr = SendPingThr(ipPool, packet, Sock, self.timeout)
sendThr.start()
while True:
try:
recvFroms.add(Sock.recvfrom(1024)[1][0])
except Exception:
pass
finally:
if not sendThr.isAlive():
break
return recvFroms & ipPool
if __name__=='__main__':
s = Nscan()
ipPool = s.makeIpPool('192.168.0.1', '192.168.0.254')
print( s.mPing(ipPool) )
- 使用Python編寫(xiě)簡(jiǎn)單網(wǎng)絡(luò)爬蟲(chóng)抓取視頻下載資源
- Python 網(wǎng)絡(luò)編程起步(Socket發(fā)送消息)
- python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(一)
- Python 網(wǎng)絡(luò)編程說(shuō)明
- python網(wǎng)絡(luò)編程之UDP通信實(shí)例(含服務(wù)器端、客戶端、UDP廣播例子)
- python socket網(wǎng)絡(luò)編程步驟詳解(socket套接字使用)
- python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(三):socket網(wǎng)絡(luò)服務(wù)器
- 以Python的Pyspider為例剖析搜索引擎的網(wǎng)絡(luò)爬蟲(chóng)實(shí)現(xiàn)方法
- Python使用urllib2獲取網(wǎng)絡(luò)資源實(shí)例講解
- python如何查看系統(tǒng)網(wǎng)絡(luò)流量的信息
相關(guān)文章
關(guān)于Python?Tkinter?復(fù)選框?->Checkbutton
這篇文章主要介紹了關(guān)于Python?Tkinter復(fù)選框Checkbutton,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
python如何將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組
這篇文章主要介紹了python如何將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Python實(shí)現(xiàn)病毒仿真器的方法示例(附demo)
這篇文章主要介紹了Python實(shí)現(xiàn)病毒仿真器的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
利用Python+PyQt5實(shí)現(xiàn)簡(jiǎn)易瀏覽器的實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于如何利用Python+PyQt5實(shí)現(xiàn)簡(jiǎn)易瀏覽器的相關(guān)資料,Qt 的主要優(yōu)勢(shì)是可以開(kāi)發(fā)跨平臺(tái)的圖形界面程序,基于 Qt 的應(yīng)用能夠借助于各平臺(tái)的原生性在不同類的設(shè)備上運(yùn)行,而無(wú)須修改任何代碼庫(kù),需要的朋友可以參考下2021-07-07
Python如何生成隨機(jī)數(shù)及random隨機(jī)數(shù)模塊應(yīng)用
這篇文章主要介紹了Python如何生成隨機(jī)數(shù)及random隨機(jī)數(shù)模塊應(yīng)用,首先我們要知道在python中用于生成隨機(jī)數(shù)的模塊是random,在使用前需要import。由此展開(kāi)內(nèi)容介紹,需要的小伙伴可以參考一下2022-06-06
如何構(gòu)建第二個(gè)Django的應(yīng)用程序
這篇文章主要介紹了如何構(gòu)建第二個(gè)Django的應(yīng)用程序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Python list列表中刪除多個(gè)重復(fù)元素操作示例
這篇文章主要介紹了Python list列表中刪除多個(gè)重復(fù)元素操作,結(jié)合實(shí)例形式分析了Python刪除list列表重復(fù)元素的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-02-02

