編寫Python指令碼大量設定VPN的教程

來源:互聯網
上載者:User
緣起

大家都知道,最近的網路不怎麼和諧,速度慢不說,VPN 還總斷,好在雲梯 提供了挺多的伺服器可以切換, 但云梯的伺服器又挺多,Linux 的 Network Manager 又不支援大量新增配置,甚至設定檔都不能複製建立, 每個伺服器的配置都得手動加,非常麻煩。

當然,也可以每次切換時開啟配置,光改地址,但是這也非常不方便。

作為一個合格的開發人員,當然會想到用程式批量組建組態,我選擇使用 Python。
尋找設定檔的位置

要大量建立配置,首先得知道設定檔在哪裡,比如自己的雲梯 VPN 地址中包含 example 字樣,這樣找起來就方便了。

複製代碼 代碼如下:

grep 'example' ~/.config -r
grep 'example' /etc/ -r

於是輕鬆的定位到了設定檔的位置

複製代碼 代碼如下:

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

瞭解設定檔結構

拿一個設定檔出來看看:

[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

顯然,有這麼幾個部分需要動態產生的

  • connection.id 這個需要是唯一的
  • connection.uuid 就是 uuid 產生一個就好了
  • connection.permissions 要添加上你的使用者名稱嘛
  • vpn.gateway VPN 伺服器的地址
  • vpn.user VPN 服務的帳戶名稱
  • ipv4.dns 按你喜好配置就好

既然瞭解了,就開工吧
準備配置資訊及模板

首先,讓我們準備好材料:

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 需要動態產生了

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

至於 connection.permissions、vpn.user 和 ipv4.dns 直接寫在配置模板中即可。

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

產生 VPN 串連設定檔

剩下的事,就只有遍曆 VPN 伺服器資訊,產生模板了

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)

我測試過,雖然 VPN 設定檔的檔案名稱怎麼寫都行,但是如果在 NetworkManager 中修改了該串連的資訊,NetworkManager 會自動將該設定檔重命為 Connection Name (也就是設定檔中 id),所以在建立檔案時,還是保持檔案名稱與 id 一致才好。

還有一個注意點是,串連設定檔必須屬於 root:root 並且使用權限設定為 600, 因為我們需要通過 sudo 執行指令碼,所以這裡只需要控制 chmod 就行了。

os.chmod(filename, 0600)

完整的指令碼

https://gist.github.com/greatghoul/9066705
享受成果

修改 tpl.cfg 中相關的使用者名稱為自己的,然後執行下面的命令。

$ 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

開始使用雲梯吧 :)

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.