| 2 | |
| 3 | == Python 3 Script to Keep VPN Link Up == |
| 4 | |
| 5 | {{{ |
| 6 | #monitor-local.py |
| 7 | |
| 8 | #Windows Python 3 Script to keep vpn up |
| 9 | |
| 10 | #Please Note you MUST set the VPN environment Variables in windows global config |
| 11 | #this script picks up on these to start rasdial service |
| 12 | # |
| 13 | #Also - Important |
| 14 | #You need to create a vpn connection in administrator first |
| 15 | #Configure it any way you need |
| 16 | #This will create a phone book file that needs to be in the system area |
| 17 | # |
| 18 | #CrayFishUK, my experience for all Windows XP/Vista/7/2008/8/2012/10 This will connect VPN as a SYSTEM user when system starts. |
| 19 | |
| 20 | #You will need to create a VPN connection |
| 21 | #Save or not save credential it is not important. |
| 22 | #Find a rasphone.pbk in %APPDATA%\Microsoft\Network\Connections\Pbk_hiddenPbk |
| 23 | #More nice if you will copy it to C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk or attach to exist rasphone.pbk (it is text files) |
| 24 | #Now you need to create scheduler task... |
| 25 | |
| 26 | #Requires lib3.py |
| 27 | |
| 28 | #Scheduler task: |
| 29 | #Setup as follows |
| 30 | #Use the SYSTEM ACCOUNT |
| 31 | #Make sure it restarts this scrip every 5 minutes and at startup |
| 32 | |
| 33 | #Start after boot |
| 34 | #Program to start has credential to connect VPN |
| 35 | |
| 36 | #The idea is to have windows run this script in the background as a system user |
| 37 | #this prevent the vpn from dropping everytime someone loggs in ?? (its a windows thing) |
| 38 | |
| 39 | #So do the setup under administrator and go from there. |
| 40 | |
| 41 | #any issue email paul@scom.ca and i will try to help! |
| 42 | |
| 43 | |
| 44 | |
| 45 | import os, sys, struct, select, time |
| 46 | import logging, logging.handlers |
| 47 | import win32serviceutil |
| 48 | import servicemanager,win32evtlogutil,win32evtlog |
| 49 | import win32event |
| 50 | import win32service,win32api |
| 51 | import py2exe |
| 52 | |
| 53 | |
| 54 | from lib3 import * |
| 55 | |
| 56 | |
| 57 | |
| 58 | class send_event : |
| 59 | def __init__(self, message = 'Info', level = 'Info' ): |
| 60 | |
| 61 | #base_dir = os.path.dirname(sys.argv[0]) |
| 62 | #print len(base_dir) |
| 63 | #print base_dir |
| 64 | |
| 65 | #dllname = os.path.join(base_dir, "win32service.pyd") |
| 66 | #print dllname |
| 67 | |
| 68 | ntl = logging.handlers.NTEventLogHandler(message, dllname="win32service.pyd") |
| 69 | logger = logging.getLogger("") |
| 70 | logger.setLevel(logging.DEBUG) |
| 71 | logger.addHandler(ntl) |
| 72 | |
| 73 | |
| 74 | if level == 'Error' or level == 'error' : |
| 75 | logger.error(message) |
| 76 | |
| 77 | if level == 'Warn' or level == 'warn' : |
| 78 | logger.warn(message) |
| 79 | |
| 80 | if level == 'Info' or level == 'info' : |
| 81 | logger.info(message) |
| 82 | |
| 83 | print ('Event Sent %s' %message) |
| 84 | |
| 85 | #Start - get the vpn variables |
| 86 | |
| 87 | LOCAL_IP = os.getenv('VPNIP') |
| 88 | RAS_CONNECT = os.getenv('VPN') |
| 89 | RAS_USERNAME = os.getenv('VPNUSER') |
| 90 | RAS_PASSWORD = os.getenv('VPNPASS') |
| 91 | |
| 92 | |
| 93 | print (LOCAL_IP,RAS_CONNECT,RAS_USERNAME,RAS_PASSWORD) |
| 94 | |
| 95 | #try : |
| 96 | |
| 97 | send_event ('Checking VPN Connection Heartbeat', 'Info') |
| 98 | |
| 99 | a=commands('c:/windows/system32/ping -n 1 %s' %LOCAL_IP) |
| 100 | print ('Ping returned : %s' %a.output) |
| 101 | |
| 102 | |
| 103 | if 'Packets: Sent = 1, Received = 0, Lost = 1 (100% loss)' in str(a.output) : |
| 104 | print ('Resetting Connection') |
| 105 | send_event ('Resetting (Stopping) Connection', 'warn') |
| 106 | command = ('C:\\Windows\\SysWOW64\\rasdial %s /disconnect' %RAS_CONNECT) |
| 107 | a = commands(command) |
| 108 | print ('Reconect Status %s' %a.output ) |
| 109 | #Now reset the rasdial service |
| 110 | command = ('c:\\windows\\system32\\sc queryex rasman') |
| 111 | a = commands(command) |
| 112 | print ('Rasdial Service Status %s' %a.output ) |
| 113 | #go find the pid line |
| 114 | b = str(a.output) #.replace ('\n','') |
| 115 | #print (b) |
| 116 | b = b.split('\n') |
| 117 | #print (b) |
| 118 | for n in range (0,len(b)) : |
| 119 | d = b[n] |
| 120 | if 'PID' in d: |
| 121 | #print (d) |
| 122 | c = d.split(': ')[1] |
| 123 | #print (c) |
| 124 | break |
| 125 | |
| 126 | if c == '0' : #is RAS running ? |
| 127 | print ('Skipping Taskkill on RAS Service - Not Running ...') |
| 128 | else : # Yes kill it |
| 129 | command = ('c:\\windows\\system32\\taskkill /f /pid %s' %c) |
| 130 | a = commands(command) |
| 131 | print ('RAS Service Stop Status %s' %a.output ) |
| 132 | |
| 133 | send_event ('Pausing for Re-Connect ...', 'Info') |
| 134 | time.sleep(5) |
| 135 | send_event ('Re-Connecting ....', 'Info') |
| 136 | command = ('C:\\Windows\\SysWOW64\\rasdial %s %s %s' %(RAS_CONNECT,RAS_USERNAME,RAS_PASSWORD)) |
| 137 | print ('Running : %s' %command) |
| 138 | a = commands(command) |
| 139 | print ('Reconect Status %s' %a.output ) |
| 140 | |
| 141 | else : |
| 142 | print ('Connection Up ...') |
| 143 | |
| 144 | #except Exception as e :#Exception for main error? |
| 145 | # exc_type, exc_obj, exc_tb = sys.exc_info() |
| 146 | # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 147 | # e = str(e) + '\n\n' + str(exc_type) + '\n' + str(fname) + '\n' + str(exc_tb.tb_lineno) |
| 148 | # send_event (e, 'error') |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | }}} |
| 159 | |
| 160 | == Task Scheduler / Setup Advice for windows == |
| 161 | |