Use Python to create a ladder VPN connection configuration in batches

As we all know, the recent network is not very harmonious, the speed is slow, and the VPN is always disconnected. Fortunately, Yunladder
provides a lot of servers that can be switched, but Yunladder has a lot of servers, and Linux Network Manager does not support batching. Adding configuration, even configuration files cannot be copied and created, and the configuration of each server has to be manually added, which is very troublesome.

Of course, you can also open the configuration every time you switch, and just change the address, but this is also very inconvenient.

As a qualified developer, of course I would like to programmatically generate configuration in batches, I chose to use Python.

Find the location of the configuration file
To create configurations in batches, you must first know where the configuration file is. For example, your Yunladder VPN address contains the word "example", which makes it easy to find.

grep 'example' ~/.config -r
grep 'example' /etc/ -r
easily locates the location of the configuration file

grep: /etc/NetworkManager/system-connections/yunti.ppt p.a : Permission denied
grep: /etc /NetworkManager/system-connections/yunti.pptp.b: Permission denied
grep: /etc/NetworkManager/system-connections/yunti.pptp.c: Permission denied
Understand the configuration file structure
Take a configuration file and see:

[connection]
id=yunti.pptp.tw1
uuid=063db9b5-5915-4f3e-8bb4-2fe58abf5be5
type=vpn
permissions=user:greatghoul:;
autoconnect=false

[vpn]
service-type=org.freedesktop.NetworkManager.pptp
gateway=tw1.example.com
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
显然,有这么几个部分需要动态生成的

connection.id 这个需要是唯一的
connection.uuid 就是 uuid 生成一个就好了
connection.permissions 要添加上你的用户名嘛
vpn.gateway VPN 服务器的地址
vpn.user VPN 服务的帐户名
ipv4.dns can be configured as you like.
Now that you understand it, let's start working.

Prepare configuration information and templates
First, let's prepare the materials:

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' },
]
The uuid needs to be dynamically generated in the configuration

>>> import uuid
>>> str(uuid.uuid1())
'0621ba62-888a-11e3-805c-44334c786649'
As for connection.permissions, vpn.user and ipv4.dns can be directly written in the configuration template.

tpl.cfg

[connection]
id=%(id)s
uuid=%(uuid)s
type=vpn
permissions=user:greatghoul:;




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
generates a VPN connection configuration file
The rest is to traverse the VPN server information and generate a template

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 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)
OK, but if the connection information is modified in NetworkManager, NetworkManager will automatically rename the configuration file to Connection Name (that is, the id in the configuration file), so when creating a file, it is better to keep the file name and id consistent. .

Another point to note is that the connection configuration file must belong to root:root and have permissions set to 600, because we need to execute the script through sudo , so we only need to control chmod here.

os.chmod(filename, 0600)
Complete script

https://gist.github.com/greatghoul/9066705

Enjoy the results
Modify the relevant username in tpl.cfg to your own, and then execute the following commands.

$ sudo python create_yunti_config.py
Cleaning up yunti connection files...
  Removing file: /etc/NetworkManager/system-connections/yunti.pptp.a
  Removing file: /etc/NetworkManager/system-connections/yunti.pptp.b
  Removing file: /etc/NetworkManager/system-connections/yunti.pptp. c
Creating yunti connection files under /etc/NetworkManager/system-connections
  Creating file: /etc/NetworkManager/system-connections/yunti.pptp.a
  Creating file: /etc/NetworkManager/system-connections/yunti.pptp.b
  Creating file : /etc/NetworkManager/system-connections/yunti.pptp.c Let's
start using the ladder :)
Python, Ladder VPN

Connect

PS: The first time you click on the VPN connection will ask for a password.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325269031&siteId=291194637