-
Notifications
You must be signed in to change notification settings - Fork 0
/
heckenschere.py
executable file
·159 lines (132 loc) · 4.4 KB
/
heckenschere.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
import crypt
import os
import re
from subprocess import check_output
DEFAULT_MESH_SSID="Buschfunk_Mesh"
#config_file = "boot/buschfunk.txt"
#if_mesh_file = "/tmp/etc/network/interfaces.d/mesh"
#hostname_file = "/tmp/hostname"
#shadow_file = "/tmp/shadow"
config_file = "/boot/buschfunk.txt"
if_mesh_file = "/etc/network/interfaces.d/mesh"
dns_file = "/etc/dnsmasq.d/ap_ns"
hostname_file = "/etc/hostname"
shadow_file = "/etc/shadow"
dry_run = False
def get_pid(name):
try:
return map(int,check_output(["pidof",name]).split())
except:
return []
def get_dns_config(ip):
dns = """
address=/busch.funk/%s
""" % (ip)
return dns
def get_mesh_config(ip, channel = "3", essid = DEFAULT_MESH_SSID):
mesh = """
#auto wlan0
iface wlan0 inet static
address %s
netmask 255.255.255.0
wireless-channel %s
wireless-essid %s
wireless-mode ad-hoc
""" % (ip, channel, essid)
return mesh
def write_config(config_file, cfg):
if not dry_run:
with open(config_file, "w") as myfile:
myfile.write(cfg)
def is_current_mesh_config(ip, channel = "3", essid = DEFAULT_MESH_SSID):
with open(if_mesh_file) as myfile:
all = myfile.read()
return (ip in all and "channel " + channel in all and essid in all)
def is_current_pw(pw):
with open(shadow_file) as myfile:
entries = myfile.readlines()
for e in entries:
if "pi:$6$" in e:
hash_all = e.split(":")[1]
salt_end = hash_all.find("$",4)
if salt_end == -1:
print "Error: no salt found!"
return False
salt = hash_all[0:salt_end+1]
npw = crypt.crypt(pw, salt)
return (npw == hash_all)
return False
res = {}
with open(config_file) as myfile:
for line in myfile:
if "=" in line and line[0] != '#':
name, var = line.partition("=")[::2]
res[name.strip()] = var.strip()
print(res)
reboot = False
if res.has_key("hostname"):
#print "Checking hostname.."
with open(hostname_file) as myfile:
h = myfile.read().strip()
if h == res['hostname']:
pass
else:
print("updating hostname to " + res['hostname'])
write_config(hostname_file, res['hostname']+"\n")
reboot = True
else:
print "Error: please set a hostname in %s!!" % config_file
if res.has_key('mesh_ip'):
if res.has_key('mesh_channel'):
if not is_current_mesh_config(res['mesh_ip'], channel = res['mesh_channel']):
print "updating mesh network config"
write_config(if_mesh_file, get_mesh_config(ip = res['mesh_ip'], channel = res['mesh_channel']))
reboot = True
else:
if not is_current_mesh_config(res['mesh_ip']):
print "updating mesh network config"
write_mesh_config(if_mesh_file, get_mesh_config(ip = res['mesh_ip']))
reboot = True
else:
print "Error: please set mesh_ip in %s!!" % config_file
if res.has_key('password'):
if not is_current_pw(res['password']):
print("Changing password for user pi...")
if not dry_run:
os.system("echo 'pi:%s' | chpasswd" % res['password'])
if res.has_key('serval'):
if res['serval'] == '1': # activate
if get_pid('servald') == []:
if not dry_run:
print("enabling serval")
os.system("systemctl enable servald.service")
os.system("systemctl start servald.service")
else:
pass
# already activated
else:
if get_pid('servald') == []:
pass
# already deactivated
else:
if not dry_run:
print("disabling serval")
os.system("systemctl disable servald.service")
os.system("systemctl stop servald.service")
location = "66.5436144,25.84719730000006"
if res.has_key('location'):
if len(res['location'].split(",")) == 2:
location = re.sub('[\s+]', '', res['location'])
with open("/tmp/location.latlon.txt", "w") as gps:
gps.write(location + "\n")
if res.has_key('hdmi'):
if res['hdmi'] == '0':
os.system("vcgencmd display_power 0")
else:
os.system("vcgencmd display_power 1")
else:
os.system("vcgencmd display_power 0")
if reboot:
print("Rebooting to apply new settings!")
os.system("reboot")