genservconf.py 1.6 KB

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