Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- #
- # PyVPNC - a Python interface to the vpnc vpn client.
- #
- # Copyright (C) 2010 Christian Dersch <pyvpnc@lupinix.net>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License along
- # with this program; if not, write to the Free Software Foundation, Inc.,
- # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- #
- # The module vpnc provides an interface to vpnc and its utilities for
- # Python.
- import subprocess
- from subprocess import PIPE
- # First we'll define and implement some functions for the vpnc utilities
- # pcf2vpnc and cisco-decrypt to have an interface fot them.
- def pcf2vpnc(pcf, conf):
- command = ["pcf2vpnc", pcf, conf]
- retcode = subprocess.call(command)
- if retcode == 0:
- return 0
- else:
- return 1
- def cisco_decrypt(key):
- command = "cisco-decrypt" + key
- output = subprocess.getstatusoutput(command)
- retcode = output[0]
- if retcode == 0:
- return output[1]
- else:
- return None
- # The class VpncManager controls vpnc. It provides functions to
- class VpncManager(object):
- def __init__(self, conf):
- self._conf = conf
- self._pid = None
- def Connect(self):
- command = ["vpnc", self._conf]
- p = subprocess.Popen(command)
- self._pid = p.pid
- def Disconnect(self):
- command = ["vpnc-disconnect"]
- p = subprocess.Popen(command)
- self._pid = None
- def GetPid(self):
- return self._pid
- def Status(self):
- if self._pid != None:
- return 1
- else:
- return 0
- class VpncConf(object):
- def __init__(self):
- self._gateway = None
- self._id = None
- self._secret = None
- self._username = None
- self._password = None
- def SetGateway(self, gateway):
- self._gateway = gateway
- def SetId(self, id):
- self._id = id
- def SetSecret(self, secret):
- self._secret = secret
- def SetUsername(self, username):
- self._username = username
- def SetPassword(self, password):
- self._password = password
- def GetGateway(self):
- return self._gateway
- def GetId(self):
- return self._id
- def GetSecret(self):
- return self._secret
- def GetUsername(self):
- return self._username
- def GetPassword(self):
- return self._password
- def ReadConfFromFilePart(self, path, wanted):
- command = ["cat", path]
- p1 = subprocess.Popen(command, stdout=PIPE)
- command = ["grep", wanted]
- p2 = subprocess.Popen(command, stdin=p1.stdout, stdout=PIPE)
- command = ["cut", "-d", " ", "-f", "3"]
- p3 = subprocess.Popen(command, stdin=p2.stdout, stdout=PIPE)
- command = ["tr", "-d", "\n"]
- p4 = subprocess.Popen(command, stdin=p3.stdout, stdout=PIPE)
- return bytes.decode(p4.communicate()[0])
- def ReadConfFromFile(self, path):
- self._gateway = self.ReadConfFromFilePart(path, "IPSec gateway")
- self._id = self.ReadConfFromFilePart(path, "IPSec ID")
- self._secret = self.ReadConfFromFilePart(path, "IPSec secret")
- self._username = self.ReadConfFromFilePart(path, "Xauth username")
- self._password = self.ReadConfFromFilePart(path, "Xauth password")
- def WriteConf(self):
- s = "# Generated by PyVPNC - Python interface for vpnc\n\n"
- s += "IPSec gateway " + self._gateway + "\n"
- s += "IPSec ID " + self._id + "\n"
- s += "IPSec secret " + self._secret + "\n\n"
- s += "Xauth username " + self._username + "\n"
- s += "Xauth password " + self._password + "\n"
- return s
- def WriteConfToFile(self, path):
- f = open(path, "w")
- f.write(self.WriteConf())
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement