Question: Is there any efficient way to check and report in a log file or on the console may be... when ever the VPN is disconnected?

```
import time
print time.asctime( time.localtime(time.time()) )

```

Can print the time but I do not know what is the code to recursively find whether the VPN is active or not. Pinging it in a while(1) would be a stupid way to check if the connection is active or not. Any way to achieve this?

Answer: 
This solution is system dependent, I do know that it works on Linux because I've done something similar, but not sure about Windows though. I don't know if you want a solution not involving ping, but I think this is a good solution.

```
import logging, os, time

PING_HOST='10.10.10.10'  # some host on the other side of the VPN

while True:
    retcode = os.system('ping -c 1 %s' % PING_HOST)

    if retcode:  
       # perform action for lost connection
       logging.warn('Lost visibility with %s' % PING_HOST)

    time.sleep(10)  # sleep 10 seconds

```

This works because ping returns a return code of `0` for success. All other return codes signify an error.
In case the IP of vpn changes, you can check if a tunnel has been established at all.

```
import psutil
import logging, os, time
import subprocess
import sys

procname = "yourprocess_name"

while True:
    cmdout = subprocess.Popen(["ifconfig | grep tun"],stdout = subprocess.PIPE, shell=True).communicate()[0]
    print "cmdout: "+str(cmdout)
    time.sleep(2)
    #-----
    if "tun" in cmdout:
        print "seems to be ok"
    if not "tun" in cmdout: 
        # perform action for lost connection
        print "killing "+str(procname)
        for proc in psutil.process_iter():
            # check whether the process name matches
            print "Listing procname: "+str(proc.name())
            if proc.name() == procname:
                proc.kill()
                sys.exit()

```