Created
August 7, 2013 16:29
-
-
Save anonymous/6175680 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import subprocess | |
import time | |
import datetime | |
# CONFIG ##################### | |
# Network-Manager Name of VPN-Profile | |
# see <nmcli con> | |
# http://askubuntu.com/questions/57339/connect-disconnect-from-vpn-from-the-command-line | |
# http://forums.fedoraforum.org/showthread.php?t=276493 | |
NM_VPN_ID = 'IPredator' | |
# how often the vpn-tunnel shall be tried to reconnect and the growing delay between retries | |
VPN_RECONNECT_MAX_RETRIES=10 | |
VPN_RECONNECT_BASE_DELAY=30 | |
# bitching NetworkManagerCLI: ** (process:2874): WARNING **: Could not deactivate connection '/org/freedesktop/NetworkManager/ActiveConnection/1': Not authorized to deactivate connections | |
# visudo -f /etc/sudoers.d/network-manager | |
# kai ALL=NOPASSWD: /usr/bin/nmcli | |
# Name of Torrent-Process | |
# see <ps axu> | |
TORRENT_PROC_PATH = '/usr/bin/transmission-gtk' | |
TORRENT_PROC_NAME='transmission-gtk' | |
# shorter/more fuzzy name to check if process is running using <pgrep> | |
TORRENT_PROC_CHECK='transmission' | |
# ping to see whether Internet over VPN-tunnel is alive or stalled | |
# https://developers.google.com/speed/public-dns/ | |
CHECK_HOST_ALIVE = '8.8.8.8' | |
OUTPUT_FILE="check-vpn.log" | |
logFile = open(OUTPUT_FILE, "a") | |
# APP ######################## | |
def main(): | |
if isAlive(): | |
log("internet-connection is alive, quitting.") | |
sys.exit(0) | |
else: | |
isTun = isTunneled() | |
isTor = isTorrenting() | |
if not isTun: | |
log("Internet-connection is stalled but there is no active VPN-tunnel present, quitting.") | |
elif not isTor: | |
log("Internet-connection is stalled but there is no active Torrent running, quitting.") | |
else: | |
stopTorrent() | |
stopTunnel() | |
time.sleep(10) | |
reconnectTunnel() | |
if isTunneled(): | |
log("VPN-tunnel successfully reestablished") | |
startTorrent() | |
sys.exit(0) | |
else: | |
log("failed to reestablish VPN-tunnel") | |
sys.exit(1) | |
sys.exit(0) | |
# LIB ######################## | |
def isAlive(): | |
#return False | |
log("checking for stalled connection, sending pings ...") | |
rc1 = exek("ping -q -c 1 %s > /dev/null" % CHECK_HOST_ALIVE) | |
time.sleep(3) | |
rc2 = exek("ping -q -c 1 %s > /dev/null" % CHECK_HOST_ALIVE) | |
time.sleep(3) | |
rc3 = exek("ping -q -c 1 %s > /dev/null" % CHECK_HOST_ALIVE) | |
return (rc1 == 0 or rc2 == 0 or rc3 == 0) | |
def isTunneled(): | |
log("checking network-manager status") | |
# full vpn-state just for the log | |
exek("sudo nmcli con status id %s | grep -e VPN-STATE" % NM_VPN_ID) | |
rc = exek("sudo nmcli con status id %s | grep -e VPN-STATE:.*5 > /dev/null" % NM_VPN_ID) | |
return (rc == 0) | |
def isTorrenting(): | |
rc = exek("pgrep -l %s" % TORRENT_PROC_CHECK) | |
return (rc == 0) | |
def stopTunnel(): | |
log("sending network-manager tunnel down") | |
rc = exek("sudo nmcli con down id %s" % NM_VPN_ID) | |
return (rc == 0) | |
def startTunnel(): | |
log("sending network-manager tunnel up") | |
rc = exek("sudo nmcli con up id %s" % NM_VPN_ID) | |
return (rc == 0) | |
def reconnectTunnel(retries = 1): | |
startTunnel() | |
time.sleep(10) | |
if not isTunneled(): | |
if retries <= VPN_RECONNECT_MAX_RETRIES: | |
wait = VPN_RECONNECT_BASE_DELAY * retries | |
log("could not reestablish VPN-tunnel, trying again in %d secs" % wait) | |
time.sleep( wait ) | |
return reconnectTunnel(retries + 1) | |
else: | |
log("could not reestablish VPN-tunnel , aborting after %d retries." % retries) | |
return False | |
else: | |
return True | |
def stopTorrent(): | |
log("sending QUIT to torrent-process") | |
rc = exek("killall --signal QUIT %s" % TORRENT_PROC_NAME) | |
log("RC:%s" % rc) | |
time.sleep(10) | |
log("sending safety-KILL to torrent-process") | |
rc = exek("killall --signal KILL %s" % TORRENT_PROC_NAME) | |
log("RC:%s" % rc) | |
def startTorrent(): | |
log("starting torrent-process ...") | |
pid = subprocess.Popen([TORRENT_PROC_PATH], shell=False, stdin=None, stdout=None, stderr=None).pid | |
log("successfully started with PID %s" % pid) | |
return True | |
def log(s): | |
print s | |
logFile.write( s + "\n" ) | |
logFile.flush() | |
# http://docs.python.org/2/library/subprocess.html | |
def exek(cmd): | |
rc = subprocess.call(cmd, stdout=logFile, stderr=subprocess.STDOUT, shell=True) | |
return rc | |
log("=== START ===") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment