Add code to actually configure interface
This commit is contained in:
parent
d67f528dc7
commit
d93f821f69
@ -10,10 +10,12 @@ import sys
|
||||
import ax25
|
||||
import ax25.ports
|
||||
import ax25.socket
|
||||
import subprocess
|
||||
|
||||
# Settings
|
||||
CLIENT_CALLSIGN = "N0CALL-7"
|
||||
BEACON_CALLSIGN = "KI5QKX-10" # We expect the server to be beaconing from here
|
||||
AX_IFACE = "ax0"
|
||||
|
||||
# Missing from 'socket'
|
||||
ETH_P_AX25 = 2
|
||||
@ -49,14 +51,69 @@ lease_expiration = None
|
||||
def build_request(network_name):
|
||||
return f"0.1|CRAP_REQUEST|{CLIENT_CALLSIGN}|{network_name}".encode('utf-8')
|
||||
|
||||
# Configure network
|
||||
def interface_needs_update(ip_address, netmask, iface=AX_IFACE):
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["ip", "-4", "addr", "show", "dev", iface],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
output = result.stdout
|
||||
logger.debug(f"Interface status:\n{output}")
|
||||
|
||||
cidr_ip = f"{ip_address}/{netmask}"
|
||||
return cidr_ip not in output
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
logger.error(f"Error checking interface: {e}")
|
||||
return True # Assume needs update if we can't tell
|
||||
|
||||
def configure_network_interface(ip_address, netmask, gateway, dns_server, iface=AX_IFACE):
|
||||
try:
|
||||
if interface_needs_update(ip_address, netmask, iface):
|
||||
logger.info("Configuring network interface...")
|
||||
|
||||
# Flush existing addresses
|
||||
subprocess.run(["ip", "addr", "flush", "dev", iface], check=True)
|
||||
|
||||
# Add new IP
|
||||
cidr_ip = f"{ip_address}/{netmask}"
|
||||
subprocess.run(["ip", "addr", "add", cidr_ip, "dev", iface], check=True)
|
||||
|
||||
# Set up route
|
||||
# subprocess.run(["ip", "route", "add", "default", "via", gateway, "dev", iface], check=True)
|
||||
|
||||
# Update DNS
|
||||
# with open("/etc/resolv.conf", "w") as resolv:
|
||||
# resolv.write(f"nameserver {dns_server}\n")
|
||||
|
||||
logger.info(f"Network interface {iface} configured successfully.")
|
||||
else:
|
||||
logger.info(f"Interface {iface} already configured with {ip_address}/{netmask}. No changes needed.")
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
logger.error(f"Failed to configure network: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error during network config: {e}")
|
||||
|
||||
# Apply network configuration (mock)
|
||||
def apply_network_config(assigned_ip, gateway, dns, lease_time):
|
||||
global configured, lease_expiration
|
||||
logger.info(f"Applying network config:")
|
||||
logger.info(f" Assigned IP: {assigned_ip}")
|
||||
logger.info(f" Gateway: {gateway}")
|
||||
logger.info(f" DNS Server: {dns}")
|
||||
# logger.info(f" Gateway: {gateway}")
|
||||
# logger.info(f" DNS Server: {dns}")
|
||||
logger.info(f" Lease Time: {lease_time} seconds")
|
||||
|
||||
configure_network_interface(
|
||||
ip_address=assigned_ip.split('/')[0],
|
||||
netmask=assigned_ip.split('/')[1] if '/' in assigned_ip else "24",
|
||||
gateway=gateway,
|
||||
dns_server=dns
|
||||
)
|
||||
|
||||
configured = True
|
||||
lease_expiration = time.time() + int(lease_time)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user