-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathipm.py
More file actions
34 lines (28 loc) · 1.17 KB
/
ipm.py
File metadata and controls
34 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import subprocess
import socket
# Specify the IP address and port of the device you want to send a message to
device_ip = '192.168.88.102'
port = 5555
# Run the ping command to check the connectivity to the device
ping_command = ['ping', '-c', '4', device_ip] # '-c' specifies the number of pings to send
process = subprocess.Popen(ping_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
# Check if the ping was successful
if process.returncode == 0:
print("Device with IP address", device_ip, "is reachable.")
# Create a socket object and connect to the device
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((device_ip, port))
# Send a message to the device
message = "Hello, device! How are you doing?"
sock.sendall(message.encode())
print("Message sent successfully to", device_ip)
except Exception as e:
print("Failed to connect to device with IP address", device_ip)
print("Error:", str(e))
finally:
sock.close()
else:
print("Device with IP address", device_ip, "is not reachable.")
print('Error:', error.decode('utf-8'))