pybatmesh/naxalnet
Pranav Jerry 715f4b7308 Updated rewrite and README.md
The rewrite is partly over, with only the iwd
part remaining. The README was updated and a commented-out
section which had no comment closing tag was fixed.
2021-06-04 22:27:54 +05:30

72 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Setup a working BATMAN Advanced network
with systemd-networkd and iwd
"""
import sys
from pathlib import Path
from shutil import copy
from dasbus.connection import SystemMessageBus
NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd"
NETWORKD_VOLATILE_DIR = "/run/systemd/network"
RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf"
RESOLV_CONF = "/etc/resolv.conf"
# Copy networkd configs to volatile dir.
# See man:systemd.networkm(5)
try:
print("Copying network config files")
dest = Path(NETWORKD_VOLATILE_DIR)
src = Path(NETWORKD_CONFIGS)
# Create the volatile directory if it doesn't exist
dest.mkdir(parents=True, exist_ok=True)
# Copy all files in src to dest
for i in src.iterdir():
copy(i, dest)
except:
sys.exit("An error occured")
# Symlink resolvd.conf to systemd's stub-resolvd.conf
# This is needed for DNS resolution to work.
# see https://wiki.archlinux.org/title/Systemd-resolved#DNS
try:
print("Checking resolv.conf")
r = Path(RESOLV_CONF)
if r.exists():
print(r, "already exists. Removing it")
r.unlink()
print("Linking resolv.conf")
r.symlink_to(RESOLVED_STUB_RESOLVE)
except:
sys.exit("An error occured while linking resolv.conf")
# connect to the System bus
bus = SystemMessageBus()
# iwd proxy
iwd = bus.get_proxy("net.connman.iwd", "/")
# Get list of all devices
print("Finding connected devices")
objects = iwd.GetManagedObjects()
device_paths = []
for name, obj in objects.items():
if "net.connman.iwd.Device" in obj:
# add all devices to the list
print("Found device:", obj["net.connman.iwd.Device"]["Name"])
device_paths.append(name)
# TODO: On first devices, start ad-hoc
# If there is a second device, start AP
# in it
print("Bye")