gencliconf.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. import os
  3. def readIniConf():
  4. conf = dict()
  5. with open('config.ini') as fp:
  6. for line in fp:
  7. if len(line.strip()) == 0: continue
  8. s = line.split('=', 1)
  9. key = s[0].strip()
  10. value = s[1].strip()
  11. conf[key] = value
  12. return conf
  13. iniConf = readIniConf()
  14. server = iniConf['server']
  15. port = iniConf['server-port']
  16. sshTarget = iniConf['ssh-target']
  17. serverPk = iniConf['server-pubkey']
  18. serverSk = iniConf['server-privkey']
  19. network = iniConf['network-prefix']
  20. interface = iniConf['server-interface']
  21. def parseProfiles():
  22. names = list()
  23. pks = list()
  24. sks = list()
  25. with open("profiles") as fp:
  26. for line in fp:
  27. fields = line.strip().split(" ")
  28. names.append(fields[0])
  29. pks.append(fields[1])
  30. sks.append(fields[2])
  31. return names, pks, sks
  32. def cliConf(sk, i):
  33. config = "[Interface]\n"
  34. config = config + 'PrivateKey = ' + sk + '\n'
  35. config = config + 'Address = ' + network + str(i+10) + '/32\n\n'
  36. config = config + '[Peer]\n' + \
  37. 'PublicKey = ' + serverPk + '\n' + \
  38. 'AllowedIPs = ' + network + '0/24' + '\n' + \
  39. 'Endpoint = ' + server + ':' + port + '\n' + \
  40. 'PersistentKeepalive = 20\n'
  41. return config
  42. names, pks, sks = parseProfiles()
  43. for i in range(len(names)):
  44. os.system("mkdir -p " + './cli-confs/' + names[i])
  45. with open("./cli-confs/" + names[i] + '/wg123.conf', "w") as fp:
  46. fp.write(cliConf(sks[i], i))