Skip to content

Instantly share code, notes, and snippets.

@gdestuynder
Last active August 29, 2015 14:03
Show Gist options
  • Save gdestuynder/f5faa56d400df864ba1f to your computer and use it in GitHub Desktop.
Save gdestuynder/f5faa56d400df864ba1f to your computer and use it in GitHub Desktop.
super basic openvpn mgmt client
#!/usr/bin/env python
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import socket
import sys
class vpnmgmt():
def __init__(self, socket_path):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.connect(socket_path)
def connect(self, p):
self.sock.connect(p)
# get rid of the welcome msg
self.sock.recv(1024)
def send(self, command):
self.sock.send(command+'\r\n')
# warning this limits the max reply size to 16k
return self.sock.recv(16384)
def success(self, msg):
# checks if OpenVPN returned SUCCESS or ERROR or else.
if msg.startswith('SUCCESS'):
return True
elif msg.startswith('INFO'):
return True
else:
return False
def getpid(self):
return self.send('pid').split('\r')[0]
def getstatus(self):
data = self.send('status').split('\r\n')
read_start = False
users = {}
for line in data:
if line.startswith('Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since'):
read_start = True
continue
if line.startswith('ROUTING TABLE') or line == '':
break
if read_start:
u = line.split(',')
users[u[0]] = u[1:]
return users
def kill(self, user):
ret = self.send('kill '+user)
return (self.success(ret), ret)
if __name__ == "__main__":
vpn = vpnmgmt(sys.argv[1])
ret = vpn.kill(sys.argv[2])
if ret[0]:
print(sys.argv[2]+' killed')
sys.exit(0)
else:
print(sys.argv[2]+' not connected')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment