Tutorial on writing Python scripts to bulk configure VPNs

Source: Internet
Author: User
Tags chmod require uuid python script

This article mainly introduces the preparation of Python script batch configuration VPN tutorial, building a VPN in the country is currently a very popular behavior (um ...), need friends can refer to the next

Origin

We all know, the recent network is not harmonious, slow not to say, VPN still always broken, fortunately, the ladder provides a lot of servers can switch, but the ladder of the server is quite many, Linux network Manager does not support the bulk 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 address, but this is also very inconvenient.

As a qualified developer, of course, I would think of using the program to generate the configuration in bulk, I chose to use Python.

Find the location of the configuration file

To create a bulk configuration, you first have to know where the configuration file, such as your own ladder VPN address contains example words, so it is easy to find.

Copy 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 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 at it:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 [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

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

Connection.id this need is unique.

Connection.uuid is just a UUID to build one.

Connection.permissions to add your username.

Address of the Vpn.gateway VPN server

Vpn.user the account name of the VPN service

Ipv4.dns, you can configure it as you prefer.

Now that you know, start working.

Prepare configuration information and templates

First of all, let's get the material ready:

?

1 2 3 4 5 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 in the configuration needs to dynamically generate the

?

1 2 3 >>> 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.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 tpl.cfg [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

To generate a VPN connection configuration file

The only thing left is to traverse the VPN server information and generate the template

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 def add_connection (TPL, conn_info): filename = Os.path.join (Cfg_dir, conn_info[' id ']) print ' Creating file: ', filename ou t = 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 V PN_SERVERS:conn_info.update (Uuid=str (UUID.UUID1 ()) add_connection (TPL, Conn_info)

I have tested that although the file name of the VPN profile is all right, if you modify the connection's information in NetworkManager, NetworkManager automatically renames the profile to Connection name (that is, the ID in the configuration file). So when you create a file, it's better to keep the file name 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 through sudo, so we just need to control the chmod.

?

1 Os.chmod (filename, 0600)

The complete script

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

Enjoy the fruits

Modify the relevant user name in the tpl.cfg as your own, and then execute the following command.

?

1 2 3 4 5 6 7 8 9 $ 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:)

Related Article

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.