Script improvements regarding finding eth0 IP.

This commit is contained in:
2023-08-31 01:24:45 +02:00
parent 3b87786066
commit 99903dcbb2

View File

@@ -1,14 +1,17 @@
import socket
import struct
import sys
from datetime import datetime
import queue
import socket
import struct
# ip address of network interface
MCAST_IF_IP = '192.168.178.66'
multicast_group = '230.120.10.2'
server_address = ('', 8123)
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("1.1.1.1", 80))
return s.getsockname()[0]
multicast_group = "230.120.10.2"
server_address = ("", 8123)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -21,7 +24,7 @@ print("Start")
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4s4s', group, socket.inet_aton(MCAST_IF_IP))
mreq = struct.pack("4s4s", group, socket.inet_aton(get_ip_address()))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
print("Ready")
@@ -34,26 +37,24 @@ while True:
data, address = sock.recvfrom(1024)
data_str = data.decode("utf-8").strip()
print(address, ": ", data_str)
data_str = datetime.now().strftime('%b-%d-%Y_%H%M%S') + ": " + data_str
data_str = datetime.now().strftime("%b-%d-%Y_%H%M%S") + ": " + data_str
buffer.put(data_str)
if buffer.full():
buffer.get()
if "NTP-Update not successful" in data_str or "Start program" in data_str:
f = open("log.txt",'a')
while not buffer.empty():
f = open("log.txt", "a")
while not buffer.empty():
f.write(buffer.get())
f.write("\n")
f.close()
saveCounter = 20
if saveCounter > 0:
f = open("log.txt",'a')
f = open("log.txt", "a")
f.write(data_str)
f.write("\n")
if saveCounter == 1:
f.write("\n")
f.close()
saveCounter -= 1