Python 实现 Ping 功能
【生活经历分享】华师国培 华师伴学 合同都是坑 消费者付款后无法退款
和华师国培签合同需小心,合同中都是保护华师的条款,没有保护消费者的条款。
收到钱,就算你因对培训质量不满意,也不能退款。因合同消费者维权肯定十分艰难。
华师伴学的授课方式是看录制的视频,不是真人现场教学。是否是您和孩子想要的学习方式?
各位打算报名的,交费要谨慎!
其他人在小红书上发的,转:
深圳市华师国培教育科技有限公司,黑心机构,大家擦亮眼睛,别被骗了,消费欺诈,虚假承诺,签合同各种坑,收到钱了不履行承诺不退款,乱扣费,维权艰难! - 小红书
Python 实现 Ping 功能(但目前功能不成功),源代码如下:
1 import threading 2 import time 3 4 from random import randint 5 6 from scapy.all import IP, TCP, ICMP, sr1 7 8 class pingOneAddress(threading.Thread): 9 def __init__(self, ipStr: str): 10 super().__init__() 11 self.setDaemon(True) 12 self.running = None 13 self.ipStr = ipStr 14 self._stop_event = threading.Event() 15 self.start() 16 17 @staticmethod 18 def icmp_ping(target_ip: str): 19 ret = False 20 # 构造ICMP请求包 21 id_ip = randint(1, 65535) 22 id_ping = randint(1, 65535) # 随机产生 ping ID 位 23 seq_ping = randint(1, 65535) # 随机产生 ping 序列号位 24 # icmp = IP(dst=target_ip, ttl=64,id=id_ip) / ICMP(type=8,id=id_ping,seq=seq_ping) 25 # icmp = IP(dst=target_ip, ttl=64, id=id_ip) / ICMP(type=8) / b'welcome tst ping' 26 icmp = IP(dst=target_ip, ttl=64,id=id_ip) / ICMP(id=id_ping,seq=seq_ping) / b'welcome tst ping.welcome tt ping' 27 # 发送ICMP请求并接收响应 28 print(icmp) 29 reply = sr1(icmp, timeout=3, verbose=False) 30 31 if reply: # 此处一直返回 None 32 replyRes = reply[0] 33 replyAns = reply[1] 34 if reply[IP].src == target_ip: 35 if replyAns.listname == 'Unanswered': 36 print(f"Ping:没有从 {target_ip} 收到回应") 37 else: 38 print(f"Ping:从 {target_ip} 收到回应") # 设备开机也执行不到此行?????? 39 ret = True 40 # print(f"收到来自 {reply[IP].src} 的回应,序列号 {reply[ICMP].seq}") 41 else: 42 print(f"Ping:没有回应: {target_ip}") 43 44 return ret 45 46 def run(self): 47 print(f"ping {self.ipStr} start...") 48 while self._stop_event: 49 time.sleep(6) 50 ret_val = pingOneAddress.icmp_ping(self.ipStr) 51 52 def stop(self): 53 print("ping end.") 54 self.running = False # 设置标志位为 False 来停止 55 self._stop_event.set() 56 self.join()
这是运行的 Log:
ping 172.20.3.34 start... running count No.1 running count No.2 running count No.3 running count No.4 running count No.5 running count No.6 IP / ICMP 172.20.3.2 > 172.20.3.34 echo-request 0 / Raw running count No.7 running count No.8 WARNING: MAC address to reach destination not found. Using broadcast. running count No.9 running count No.10 running count No.11 Ping:没有回应: 172.20.3.34
情况说明:
命令行执行 ping 是功能的,说明网络没有问题。

arp -a 返回中也有这样的行:172.20.3.34 aa-bb-cc-dd-ee-21 静态
Wireshark 中抓包可以看到如下信息:

使用网上另一种 ARP Ping的实现,但仍然是不通。最后,直接调用系统的 ping 命令,类似于在命令行直接执行,是可以通的。
1 # arp_remote = arpOneAddress("172.20.3.34") 2 # os_remote = sysPingOneAddress("172.20.3.34") 3 4 class arpOneAddress(threading.Thread): 5 def __init__(self, ipStr: str): 6 super().__init__() 7 self.setDaemon(True) 8 self.running = None 9 self.ipStr = ipStr 10 self._stop_event = threading.Event() 11 self.start() 12 13 @staticmethod 14 def arp_ping(target_ip: str): 15 ret = False 16 # 构造ARP请求包 17 arp_dst_ip_port = "%s/24" %target_ip 18 reply_arp = srp(Ether(dst="aa:bb:cc:dd:ee:01")/ARP(pdst=arp_dst_ip_port), timeout=2) 19 20 if reply_arp: 21 replyRes = reply_arp[0] 22 replyAns = reply_arp[1] 23 print(f"ARP:有回应: {replyRes} {replyAns}") 24 if replyAns.listname == "Unanswered": 25 print(f"ARP:没有从 {target_ip} 收到回应") ### 执行此行,Ping 还是不通的 26 else: 27 print(f"ARP:从 {target_ip} 收到回应") 28 else: 29 print(f"ARP:没有回应: {target_ip}") 30 31 32 return ret 33 34 def run(self): 35 print(f"ping {self.ipStr} start...") 36 while self._stop_event: 37 time.sleep(6) 38 ret_val = arpOneAddress.arp_ping(self.ipStr) 39 40 def stop(self): 41 print("ping end.") 42 self.running = False # 设置标志位为 False 来停止 43 self._stop_event.set() 44 self.join() 45 46 47 class sysPingOneAddress(threading.Thread): 48 def __init__(self, ipStr: str): 49 super().__init__() 50 self.setDaemon(True) 51 self.running = None 52 self.ipStr = ipStr 53 self._stop_event = threading.Event() 54 self.start() 55 56 @staticmethod 57 def os_ping(target_ip: str): 58 ret = True 59 import subprocess 60 61 # 使用 subprocess.run() 执行 ping 命令 62 print(f"call system command: ping {target_ip}") 63 # -n 1 指定了发送 4 个 ICMP 回显请求(在Windows中是 -n 参数,而在Linux中是 -c) 64 result = subprocess.run(['ping', '-n', '4', target_ip], stdout=subprocess.PIPE, text=True) 65 # 打印输出结果 66 print(result.stdout) 67 if -1 != result.stdout.find("请求找不到主机") and -1 != result.stdout.find("无法访问目标主机"): 68 ret = False 69 if -1 != result.stdout.find("请求超时"): 70 ret = False 71 72 return ret 73 74 def run(self): 75 print(f"ping {self.ipStr} start...") 76 while self._stop_event: 77 time.sleep(6) 78 ret_val = sysPingOneAddress.os_ping(self.ipStr) 79 80 def stop(self): 81 print("ping end.") 82 self.running = False # 设置标志位为 False 来停止 83 self._stop_event.set() 84 self.join()

浙公网安备 33010602011771号