Tutorials for writing Python scripts to bulk configure VPNs

Source: Internet
Author: User
Origin

We all know that the recent network is not harmonious, slow speed does not say, the VPN is always broken, fortunately, the ladder provides a lot of server can switch, but the ladder server is very many, Linux Network Manager does not support batch add configuration, even configuration files can not copy new, The configuration of each server has to be manually added, very cumbersome.

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

As a qualified developer, I would of course have thought of using a program to build the configuration in bulk, and I chose Python.
Find the location of the configuration file

In order to create the configuration in bulk, you need to know where the configuration file is, such as your own ladder VPN address contains example words, so it is convenient to find.

Copy the Code code as follows:

grep ' Example ' ~/.config-r
grep ' Example '/etc/-R

So it's easy to navigate to the location of the configuration file

Copy the Code code as follows:

grep:/etc/networkmanager/system-connections/yunti.pptp.a:permission denied
grep:/etc/networkmanager/system-connections/yunti.pptp.b:permission denied
grep:/etc/networkmanager/system-connections/yunti.pptp.c:permission denied

Understanding the configuration file structure

Take a configuration file and look it up:

[Connection]id=yunti.pptp.tw1uuid=063db9b5-5915-4f3e-8bb4-2fe58abf5be5type=vpnpermissions=user:greatghoul:; Autoconnect=false[vpn]service-type=org.freedesktop.networkmanager.pptpgateway=tw1.example.comrequire-mppe= yesuser=greatghoulrefuse-chap=yesrefuse-eap=yespassword-flags=1refuse-pap=yes[ipv4]method=autodns=8.8.8.8; 8.8.4.4;ignore-auto-dns=true

Obviously, there are so few parts that need to be dynamically generated

    • Connection.id, this need is unique.
    • Connection.uuid is a UUID.
    • Connection.permissions to add your username.
    • Vpn.gateway the address of the VPN server
    • Vpn.user the account name of the VPN service
    • Ipv4.dns as you like to configure.

Now that you know it, start working.
Preparing configuration information and templates

First of all, let's prepare the material:

Vpn_servers = [  {' id ': ' yunti.pptp.a ', ' Gateway ': ' a.example.com '},  {' id ': ' yunti.pptp.b ', ' Gateway ': ' B.examp Le.com '},  {' id ': ' yunti.pptp.c ', ' Gateway ': ' c.example.com '},]

The UUID in the configuration needs to be dynamically generated.

>>> Import uuid>>> str (UUID.UUID1 ()) ' 0621ba62-888a-11e3-805c-44334c786649 '

As for Connection.permissions, Vpn.user and Ipv4.dns, write directly in the configuration template.

tpl.cfg[connection]id=% (ID) suuid=% (UUID) Stype=vpnpermissions=user:greatghoul:;autoconnect=false[vpn] service-type=org.freedesktop.networkmanager.pptpgateway=% (Gateway) srequire-mppe=yesuser=greatghoulrefuse-chap= Yesrefuse-eap=yespassword-flags=1refuse-pap=yes[ipv4]method=autodns=8.8.8.8;8.8.4.4;ignore-auto-dns=true

To generate a VPN connection configuration file

The only thing left is to traverse the VPN server information and generate the 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)

I tested that although the file name of the VPN profile is all right, but if the connection information is modified in NetworkManager, NetworkManager automatically resets the profile to Connection Name (that is, the ID in the configuration file). So when creating a file, it's good to keep the filename consistent with the ID.

Another point to note is that the connection profile must belong to Root:root and the permission is set to 600 because we need to execute the script via sudo, so here's just a chmod to control.

Os.chmod (filename, 0600)

Complete script

https://gist.github.com/greatghoul/9066705
Enjoy the results

Modify the tpl.cfg in the associated user name for yourself, and then execute the following command.

$ 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.ccreating 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 with the ladder:)

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.