This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fix_key_file_names.py
69 lines (53 loc) · 1.81 KB
/
fix_key_file_names.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
#!/usr/bin/env python2
import os
import sys
if len(sys.argv) != 2:
print('usage: ' + sys.argv[0] + ' /path/to/peers')
sys.exit(1)
peersDir = sys.argv[1]
def normalizeMac(mac):
mac = mac.lower()
normalized = ''
n = 0
for c in mac:
if c != ':':
if n > 0 and n % 2 == 0:
normalized = normalized + ':'
normalized = normalized + c
n += 1
return normalized
def toFilename(peer):
filename = ''
for field in ['name', 'mac', 'vpn', 'token']:
if peer.has_key(field):
filename = filename + peer[field]
filename = filename + '@'
return filename[:-1]
for filename in os.listdir(peersDir):
if len(filename) == 0 or filename[0] == '.':
continue
absFilename = peersDir + '/' + filename
if os.path.isfile(absFilename):
peerFile = open(absFilename, 'r')
try:
peerLines = peerFile.readlines()
peer = {}
mac = None
for line in peerLines:
parts = line.split()
if len(parts) > 0:
if parts[1] == 'Knotenname:':
peer['name'] = parts[2].lower()
elif parts[1] == 'MAC:':
peer['mac'] = normalizeMac(parts[2])
elif parts[1] == 'Token:':
peer['token'] = parts[2].lower()
elif parts[0] == 'key':
peer['vpn'] = parts[1].split('"')[1].lower()
newFilename = toFilename(peer)
if filename != newFilename:
os.rename(absFilename, peersDir + '/' + newFilename)
except Exception as e:
print('Error in %s, ignoring peer: %s' % (absFilename, e));
finally:
peerFile.close()