Tutorial on configuring VPN in batches using Python scripts

Source: Internet
Author: User

Tutorial on configuring VPN in batches using Python scripts

This article describes how to compile a Python script to configure a VPN in batches. Setting up a VPN is a very popular behavior in China (well...). For more information, see

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 the Code as follows:

Grep 'example '~ /. Config-r

Grep 'example '/etc/-r

Therefore, you can easily locate the configuration file.

Copy the Code 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

[Connection]

Id=yunti.ppt p. tw1

Uuid = 063db9b5-5915-4f3e-8bb4-2fe58abf5be5

Type = vpn

Permissions = user: greatghoul :;

Autoconnect = false

 

[Vpn]

Service-typepolicorg.freeyuntop.networkmanager.ppt p

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 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:

?

1

2

3

4

5

VPN_SERVERS = [

{'Id': 'yunti.ppt p. A', 'Gateway': 'A .example.com '},

{'Id': 'yunti.ppt p. B ', 'Gateway':' B .example.com '},

{'Id': 'yunti.ppt p. C', 'Gateway': 'C .example.com '},

]

Uuid needs to be dynamically generated during configuration

?

1

2

3

>>> Import uuid

>>> Str (uuid. uuid1 ())

'0621ba62-888a-11e3-805c-44334c786649'

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

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

Generate a VPN connection configuration file

The rest is to traverse the VPN Server Information and generate a 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 (pai_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', pai_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.

?

1

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.

?

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.ppt p.

Removing file:/etc/NetworkManager/system-connections/yunti.ppt p. B

Removing file:/etc/NetworkManager/system-connections/yunti.ppt p. c

Creating yunti connection files under/etc/NetworkManager/system-connections

Creating file:/etc/NetworkManager/system-connections/yunti.ppt p.

Creating file:/etc/NetworkManager/system-connections/yunti.ppt p. B

Creating file:/etc/NetworkManager/system-connections/yunti.ppt p. c

Get started with yunti :)

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.