Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
1,441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # PyVPNC - a Python interface to the vpnc vpn client.
  4. #
  5. # Copyright (C) 2010  Christian Dersch <pyvpnc@lupinix.net>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. # The module vpnc provides an interface to vpnc and its utilities for
  22. # Python.
  23.  
  24. import subprocess
  25. from subprocess import PIPE
  26.  
  27. # First we'll define and implement some functions for the vpnc utilities
  28. # pcf2vpnc and cisco-decrypt to have an interface fot them.
  29. def pcf2vpnc(pcf, conf):
  30.     command = ["pcf2vpnc", pcf, conf]
  31.     retcode = subprocess.call(command)
  32.     if retcode == 0:
  33.         return 0
  34.     else:
  35.         return 1
  36.  
  37. def cisco_decrypt(key):
  38.     command = "cisco-decrypt" + key
  39.     output = subprocess.getstatusoutput(command)
  40.     retcode = output[0]
  41.     if retcode == 0:
  42.         return output[1]
  43.     else:
  44.         return None
  45.  
  46.  
  47. # The class VpncManager controls vpnc. It provides functions to
  48. class VpncManager(object):
  49.     def __init__(self, conf):
  50.         self._conf = conf
  51.         self._pid = None
  52.  
  53.     def Connect(self):
  54.         command = ["vpnc", self._conf]
  55.         p = subprocess.Popen(command)
  56.         self._pid = p.pid
  57.  
  58.     def Disconnect(self):
  59.         command = ["vpnc-disconnect"]
  60.         p = subprocess.Popen(command)
  61.         self._pid = None
  62.        
  63.     def GetPid(self):
  64.         return self._pid
  65.  
  66.     def Status(self):
  67.         if self._pid != None:
  68.             return 1
  69.         else:
  70.             return 0
  71.  
  72.  
  73. class VpncConf(object):
  74.     def __init__(self):
  75.         self._gateway = None
  76.         self._id = None
  77.         self._secret = None
  78.         self._username = None
  79.         self._password = None
  80.        
  81.     def SetGateway(self, gateway):
  82.         self._gateway = gateway
  83.        
  84.     def SetId(self, id):
  85.         self._id = id
  86.        
  87.     def SetSecret(self, secret):
  88.         self._secret = secret
  89.        
  90.     def SetUsername(self, username):
  91.         self._username = username
  92.        
  93.     def SetPassword(self, password):
  94.         self._password = password
  95.        
  96.     def GetGateway(self):
  97.         return self._gateway
  98.        
  99.     def GetId(self):
  100.         return self._id
  101.        
  102.     def GetSecret(self):
  103.         return self._secret
  104.        
  105.     def GetUsername(self):
  106.         return self._username
  107.        
  108.     def GetPassword(self):
  109.         return self._password
  110.  
  111.     def ReadConfFromFilePart(self, path, wanted):
  112.         command = ["cat", path]
  113.         p1 = subprocess.Popen(command, stdout=PIPE)
  114.         command = ["grep", wanted]
  115.         p2 = subprocess.Popen(command, stdin=p1.stdout, stdout=PIPE)
  116.         command = ["cut", "-d", " ", "-f", "3"]
  117.         p3 = subprocess.Popen(command, stdin=p2.stdout, stdout=PIPE)
  118.         command = ["tr", "-d", "\n"]
  119.         p4 = subprocess.Popen(command, stdin=p3.stdout, stdout=PIPE)
  120.         return bytes.decode(p4.communicate()[0])
  121.        
  122.     def ReadConfFromFile(self, path):
  123.         self._gateway = self.ReadConfFromFilePart(path, "IPSec gateway")
  124.         self._id = self.ReadConfFromFilePart(path, "IPSec ID")
  125.         self._secret = self.ReadConfFromFilePart(path, "IPSec secret")
  126.         self._username = self.ReadConfFromFilePart(path, "Xauth username")
  127.         self._password = self.ReadConfFromFilePart(path, "Xauth password")
  128.        
  129.     def WriteConf(self):
  130.         s = "# Generated by PyVPNC - Python interface for vpnc\n\n"
  131.         s += "IPSec gateway " + self._gateway + "\n"
  132.         s += "IPSec ID " + self._id + "\n"
  133.         s += "IPSec secret " + self._secret + "\n\n"
  134.         s += "Xauth username " + self._username + "\n"
  135.         s += "Xauth password " + self._password + "\n"
  136.         return s
  137.        
  138.     def WriteConfToFile(self, path):
  139.         f = open(path, "w")
  140.         f.write(self.WriteConf())
  141.         f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement