Hey guys,
I hope you're all doing well. I'm new to raspberry pi and python. I'm doing a project where I'll use a raspberry pi to communicate with an other board sending mac commands. To get started, I want to write a code to just simply ping my second board.
If i just open a terminal and ping the ip address of my device, it works properly. I get the following
64 bytes from 169.254.169.197: icmp_seq=8 ttl=64 time=0.092 ms
Now I want to get ping my device writing python code in the Thonny IDE.
I wrote the below code (with some google searches & chat gpt). However I always end up in the failure state. Would anyone have an idea why my code can't seem to ping the same device I successfully ping in the command line?
BTW my pi does not have any firewalls.
thank you for the help.
Kind Regards
import socket
import struct
import time
def send_ping(dest_ip):
# Create a raw socket
icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
# Define the ICMP Echo Request packet
icmp_packet = struct.pack('!BBHHH', 8, 0, 0, 0, 1) + b'pingdata'
# Calculate the ICMP checksum
checksum = 0
for i in range(0, len(icmp_packet), 2):
checksum += (icmp_packet << 8) + icmp_packet[i + 1]
checksum = (checksum >> 16) + (checksum & 0xFFFF)
checksum = ~checksum & 0xFFFF
icmp_packet = struct.pack('!BBHHH', 8, 0, checksum, 0, 1) + b'pingdata'
# Send the ICMP packet
icmp_socket.sendto(icmp_packet, (dest_ip, 0))
# Receive the ICMP reply
try:
response, _ = icmp_socket.recvfrom(1024)
icmp_type = struct.unpack('!B', response[20:21])[0]
# Check if it's an Echo Reply (ICMP type 0)
if icmp_type == 0:
return True
except socket.error:
pass
return False
def main():
dest_ip = "169.254.169.197"
while True:
success = send_ping(dest_ip)
if success:
print("Yeah")
else:
print("Failure")
time.sleep(1)
if __name__ == "__main__":
main()
I hope you're all doing well. I'm new to raspberry pi and python. I'm doing a project where I'll use a raspberry pi to communicate with an other board sending mac commands. To get started, I want to write a code to just simply ping my second board.
If i just open a terminal and ping the ip address of my device, it works properly. I get the following
64 bytes from 169.254.169.197: icmp_seq=8 ttl=64 time=0.092 ms
Now I want to get ping my device writing python code in the Thonny IDE.
I wrote the below code (with some google searches & chat gpt). However I always end up in the failure state. Would anyone have an idea why my code can't seem to ping the same device I successfully ping in the command line?
BTW my pi does not have any firewalls.
thank you for the help.
Kind Regards
import socket
import struct
import time
def send_ping(dest_ip):
# Create a raw socket
icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
# Define the ICMP Echo Request packet
icmp_packet = struct.pack('!BBHHH', 8, 0, 0, 0, 1) + b'pingdata'
# Calculate the ICMP checksum
checksum = 0
for i in range(0, len(icmp_packet), 2):
checksum += (icmp_packet << 8) + icmp_packet[i + 1]
checksum = (checksum >> 16) + (checksum & 0xFFFF)
checksum = ~checksum & 0xFFFF
icmp_packet = struct.pack('!BBHHH', 8, 0, checksum, 0, 1) + b'pingdata'
# Send the ICMP packet
icmp_socket.sendto(icmp_packet, (dest_ip, 0))
# Receive the ICMP reply
try:
response, _ = icmp_socket.recvfrom(1024)
icmp_type = struct.unpack('!B', response[20:21])[0]
# Check if it's an Echo Reply (ICMP type 0)
if icmp_type == 0:
return True
except socket.error:
pass
return False
def main():
dest_ip = "169.254.169.197"
while True:
success = send_ping(dest_ip)
if success:
print("Yeah")
else:
print("Failure")
time.sleep(1)
if __name__ == "__main__":
main()
Statistics: Posted by lecher8 — Wed Jan 03, 2024 10:28 pm