2021-07-20 11:59:52 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-07-20 17:30:49 +02:00
|
|
|
from typing import Optional
|
2021-07-20 11:59:52 +02:00
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import fcntl
|
|
|
|
import struct
|
|
|
|
|
|
|
|
|
2021-07-20 17:30:49 +02:00
|
|
|
def get_ip_address(ifname: str) -> str:
|
2021-07-20 11:59:52 +02:00
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15].encode()))[20:24])
|
|
|
|
|
|
|
|
|
2021-07-20 17:30:49 +02:00
|
|
|
def get_uptime() -> float:
|
2021-07-20 11:59:52 +02:00
|
|
|
with open('/proc/uptime', 'r') as f:
|
|
|
|
uptime_seconds = float(f.readline().split()[0])
|
|
|
|
|
|
|
|
return uptime_seconds
|
|
|
|
|
|
|
|
|
2021-07-20 17:30:49 +02:00
|
|
|
def get_hostname() -> str:
|
|
|
|
return socket.gethostname()
|
|
|
|
|
|
|
|
|
2021-07-26 22:24:46 +02:00
|
|
|
def get_version() -> str:
|
|
|
|
try:
|
|
|
|
with open('/version', 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
if line.lower().startswith("version:"):
|
|
|
|
return line.split(":", 2)[1].strip()
|
2021-07-20 17:30:49 +02:00
|
|
|
|
2021-07-26 22:24:46 +02:00
|
|
|
return "u"
|
|
|
|
except FileNotFoundError:
|
|
|
|
return "u"
|
2021-07-20 17:30:49 +02:00
|
|
|
|
|
|
|
|
2021-07-20 11:59:52 +02:00
|
|
|
def main():
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
2021-07-26 22:24:46 +02:00
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, b"enp3s0")
|
2021-07-20 17:30:49 +02:00
|
|
|
|
2021-07-20 11:59:52 +02:00
|
|
|
while True:
|
|
|
|
|
|
|
|
ipaddr = ""
|
|
|
|
try:
|
2021-07-26 22:24:46 +02:00
|
|
|
ipaddr = get_ip_address("enp3s0")
|
2021-07-20 11:59:52 +02:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2021-07-26 22:24:46 +02:00
|
|
|
message = f"BIRBOX|{ipaddr}|{get_hostname()}|{get_version()}|{int(get_uptime())}"
|
2021-07-20 11:59:52 +02:00
|
|
|
|
2021-07-26 22:24:46 +02:00
|
|
|
sock.sendto(message.encode("utf-8"), ('<broadcast>', 6969))
|
2021-07-20 11:59:52 +02:00
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("Starting beacon...")
|
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
print("Beacon stopped.")
|