Created
February 18, 2014 08:15
-
-
Save greatghoul/9066705 to your computer and use it in GitHub Desktop.
Linux下批量创建云梯vpn配置 http://www.g2w.me/2014/01/batch-create-vpn-config-files-with-python/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# http://forums.fedoraforum.org/showthread.php?t=272322 | |
import os | |
import uuid | |
import fnmatch | |
import subprocess | |
CURRENT_DIR = os.path.dirname(__file__) | |
CFG_DIR = '/etc/NetworkManager/system-connections' | |
VPN_SERVERS = [ | |
{ 'id': 'yunti.pptp.a', 'gateway': 'a.example.com' }, | |
{ 'id': 'yunti.pptp.b', 'gateway': 'b.example.com' }, | |
{ 'id': 'yunti.pptp.c', 'gateway': 'c.example.com' }, | |
] | |
def add_connection(tpl, conn_info): | |
filename = os.path.join(CFG_DIR, conn_info['id']) | |
print ' Creating file:', filename | |
out = open(filename, 'w') | |
out.write(tpl % conn_info) | |
out.close() | |
os.chmod(filename, 0600) | |
def cleanup(): | |
print 'Cleaning up yunti connection files...' | |
for cfg_file in os.listdir(CFG_DIR): | |
if fnmatch.fnmatch(cfg_file, 'yunti.*'): | |
filename = os.path.join(CFG_DIR, cfg_file) | |
print ' Removing file:', filename | |
os.remove(filename) | |
def create_all(): | |
tpl = open(os.path.join(CURRENT_DIR, 'tpl.cfg'), 'r').read() | |
print 'Creating yunti connection files under', CFG_DIR | |
for conn_info in VPN_SERVERS: | |
conn_info.update(uuid=str(uuid.uuid1())) | |
add_connection(tpl, conn_info) | |
if __name__ == '__main__': | |
cleanup() | |
create_all() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[connection] | |
id=%(id)s | |
uuid=%(uuid)s | |
type=vpn | |
permissions=user:greatghoul:; | |
autoconnect=false | |
[vpn] | |
service-type=org.freedesktop.NetworkManager.pptp | |
gateway=%(gateway)s | |
require-mppe=yes | |
user=greatghoul | |
refuse-chap=yes | |
refuse-eap=yes | |
password-flags=1 | |
refuse-pap=yes | |
[ipv4] | |
method=auto | |
dns=8.8.8.8;8.8.4.4; | |
ignore-auto-dns=true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment