-
Notifications
You must be signed in to change notification settings - Fork 0
/
setapname.py
executable file
·136 lines (104 loc) · 3.31 KB
/
setapname.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
import re
import string
import sys
from pathlib import Path
from subprocess import call
from contextlib import contextmanager
# from python_hosts import Hosts, HostsEntry
ALPHA = string.ascii_lowercase + string.ascii_uppercase + string.digits + "-"
def filter_name(name: str) -> str:
return "".join(c for c in name.replace("_", "-") if c in ALPHA)
@contextmanager
def read_write():
call(["mount", "-o", "remount,rw", "remount", "/"])
yield
call(["mount", "-o", "remount,ro", "remount", "/"])
def get_serial() -> str:
"""
Returns from 8th position on the cpu serial number
Example:
original serial number: 00000000413c11de
return value: 413c11de
"""
with open("/proc/cpuinfo") as fd:
for line in fd:
if "Serial" in line:
return line.strip().split(":")[1].strip()[8:]
else:
return "FFF00000"
def get_hostname() -> str:
"""
Return /etc/hostname
"""
with open("/etc/hostname") as fd:
return fd.read().strip()
def create_hostname() -> str:
return f"Akku{get_serial()}"
def set_ap(host_name: str):
"""
Set the ssid of access point to provided host_name
"""
hostapd_conf = Path("/etc/hostapd/hostapd.conf")
name = f"ssid={host_name}"
new_config = re.sub(
r"^ssid=(.*)", name, hostapd_conf.read_text(), flags=re.MULTILINE,
)
try:
hostapd_conf.write_text(new_config)
except PermissionError:
pass
def set_hostname(host_name: str):
"""
Write host_name to /etc/hostname
"""
with read_write():
with open("/etc/hostname", "w") as fd:
fd.write(host_name + "\n")
def set_hostname_dhclient(hostname: str):
fmt = 'send host-name = "{}";'
regex = re.compile(r'send host\-name ?= ?"?(.+)"?;')
config = Path("/media/data/dhclient.conf")
old = config.read_text()
result = regex.search(old)
if result:
new = regex.sub(fmt.format(hostname), config.read_text())
if new != old:
config.write_text(new)
def set_hosts(hostname: str):
from python_hosts import Hosts, HostsEntry
hosts_file = Hosts()
entry = HostsEntry(entry_type="ipv4", address="127.0.0.1", names=[hostname])
current_hosts = [
host
for host in hosts_file.entries
if host.entry_type == "ipv4" and "localhost" not in host.names
]
for current_host in current_hosts:
for name in current_host.names:
hosts_file.remove_all_matching(name=name)
print(f"Adding entry to hosts: {entry}")
hosts_file.add([entry])
with read_write():
hosts_file.write()
def set_hostname_kernel(name: str):
call(["hostname", name])
def set_all(name: str):
set_ap(name)
set_hostname(name)
set_hostname_kernel(name)
set_hostname_dhclient(name)
set_hosts(name)
def main():
custom_hostname = Path("/media/data/custom_hostname").exists()
default_hostname = create_hostname()
etc_hostname = get_hostname()
print(f"Custom: {custom_hostname}")
print(f"default: {default_hostname}")
print(f"/etc/hostname: {etc_hostname}")
if not custom_hostname and default_hostname != etc_hostname:
set_all(default_hostname)
elif custom_hostname:
set_all(etc_hostname)
if __name__ == "__main__":
main()