Compile a Python script to configure VPN in batches and a python script vpn tutorial

Source: Internet
Author: User

Compile a Python script to configure VPN in batches and a python script vpn tutorial

Origin

As we all know, the recent network is not harmonious, the speed is slow, and the VPN is still disconnected. Fortunately, yunti provides a lot of servers for switching, but there are quite a lot of yunti servers, in Linux, Network Manager does not support batch configuration addition, and even configuration files cannot be copied and created. The configuration of each server must be manually added, which is very troublesome.

Of course, you can also enable the configuration and change the address each time you switch, but this is also very inconvenient.

As a qualified developer, of course, I would think of using programs to 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 located. For example, the VPN address of your cloud ladder contains the name example, which makes it easy to find out.

Copy codeThe Code is as follows: grep 'example '~ /. Config-r
Grep 'example '/etc/-r

Therefore, you can easily locate the configuration file.

Copy codeThe Code is as follows: grep:/etc/NetworkManager/system-connections/yunti.ppt p. a: Permission denied
Grep:/etc/NetworkManager/system-connections/yunti.ppt p. B: Permission denied
Grep:/etc/NetworkManager/system-connections/yunti.ppt p. c: Permission denied

Understand the configuration file structure

Let's take a look at the configuration file:

[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 several parts that need to be dynamically generated

  • Connection. id, which must be unique
  • Connection. uuid is what uuid generates.
  • Connection. permissions should I add your user name?
  • The address of the vpn. gateway VPN Server.
  • Account name of vpn. user VPN Service
  • Ipv4.dns configuration as you like

Now that you know about it, let's get started.
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' },]

Uuid needs to be dynamically generated during configuration

>>> import uuid>>> str(uuid.uuid1())'0621ba62-888a-11e3-805c-44334c786649'

For connection. permissions, vpn. user, and ipv4.dns, write them 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

Generate 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)

I tested that although the name of the VPN configuration file can be written, if the connection information is modified in NetworkManager, networkManager automatically resets the configuration file to the Connection Name (that is, the id in the configuration file), so it is better to keep the file Name consistent with the id when creating the file.

Note that the connection configuration file must belong to root: root and the permission is set to 600. Because we need to execute the script through sudo, we only need to control chmod here.

os.chmod(filename, 0600)

Complete script

Https://gist.github.com/greatghoul/9066705
Enjoy results

Modify the user name in tpl. cfg as your own, 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

Get started with yunti :)

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.