Advertisement
Guest User

save_vpn.py

a guest
Jan 18th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. import os
  2. import time
  3. import socket
  4. import fcntl
  5. import struct
  6. import optparse
  7.  
  8. def get_ip_address(ifname):
  9.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  10.     return socket.inet_ntoa(fcntl.ioctl(
  11.         s.fileno(),
  12.         0x8915,
  13.         struct.pack('256s', ifname[:15])
  14.     )[20:24])
  15.  
  16.  
  17.  
  18. def ciclo(interface,interface_down,time_check):
  19.     ip=get_ip_address(interface)
  20.     my_ip=ip
  21.     start = raw_input("Press enter when you are connect to vpn service\n")
  22.     try:
  23.         while ip == my_ip:
  24.             ip = get_ip_address(interface)
  25.                     time.sleep(time_check)
  26.         except IOError:
  27.             os.system("ifconfig %s down" % interface_down)
  28.             print "You're vpn failed, %s go down" % interface_down
  29.         os.system("ifconfig %s down" % interface_down)
  30.  
  31.  
  32. def main():
  33.     parser=optparse.OptionParser("python %prog "+ "-i <interface> -d <interface down> -t <time>\nRoot user to work!")
  34.     parser.add_option("-i", dest="interface", type="string", help="interface (eth*,tun*,wlan* (tun* for openvpn")
  35.     parser.add_option("-t", dest="time",type="int",help="time to check (default=3)")
  36.     parser.add_option("-d", dest="interface_down", type="string",help="interface must go down if vpn fail (eth*,wlan*)")
  37.     (options,args)=parser.parse_args()
  38.     interface=options.interface
  39.     time_check=options.time
  40.     interface_down=options.interface_down
  41.     if time_check==None:
  42.         time=3
  43.     if interface==None:
  44.         print "Must specify interface\n"
  45.         exit(0)
  46.     if interface_down==None:
  47.         print "Must specify interface that must go down if vpn fail\n"
  48.         exit(0)
  49.     ciclo(interface,interface_down,time_check)
  50.    
  51.  
  52. if __name__=="__main__":
  53.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement