Skip to content

Instantly share code, notes, and snippets.

@isoteemu
Last active April 28, 2016 10:01
Show Gist options
  • Save isoteemu/731abcbe2d1bec98cadb079795c14915 to your computer and use it in GitHub Desktop.
Save isoteemu/731abcbe2d1bec98cadb079795c14915 to your computer and use it in GitHub Desktop.
Automatic vpnbook password updating script for KWallet and NetworkManager
#!/usr/bin/env python
'''
Automatic vpnbook password updating script for KWallet and NetworkManager
'''
url = 'http://www.vpnbook.com/freevpn'
## To find out your NetworkManager uuid, run: nmcli con list
uuid = '2bf2b05e-1b6f-4cd3-abe3-d07fb15c54d0'
import re
import sys
import time
from subprocess import call, check_output
import requests
from PyQt4 import QtGui
from PyKDE4.kdeui import KWallet
try:
# Can be ignored, its a wrapper for requests
from teemu import firefox
headers = {}
except:
firefox_version = re.search('[0-9\.]+$', check_output(['firefox', '--version'])).group(0)
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/'+firefox_version}
firefox = requests
data = firefox.get(url, headers=headers).text
vpn_password = re.search('<li>Password:\s*<[^>]+>(.+)</[^>]+></li>', data).group(1)
vpn_password = re.sub(r'[^a-zA-Z0-9]', '', vpn_password)
if len(vpn_password):
print "Updating vpn password to "+vpn_password
app = QtGui.QApplication([])
wallet = KWallet.Wallet.openWallet(KWallet.Wallet.LocalWallet(), 0)
for i in range(1, 5):
if wallet.isOpen() == False:
time.sleep(i)
continue
wallet.setFolder('Network Management')
wallet.writeMap('{'+uuid+'};vpn', {'VpnSecrets': 'password%SEP%' + vpn_password })
break
else:
print "Could not update kwallet password"
sys.exit(call(['nmcli', 'con', 'up', 'uuid', uuid]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment