2019-12-03 20:27:13 +00:00
|
|
|
from servicebase import ServiceBase
|
2019-12-13 23:21:01 +00:00
|
|
|
import paramiko
|
|
|
|
import re
|
2019-12-03 20:27:13 +00:00
|
|
|
|
|
|
|
class ServiceDelegate(ServiceBase) :
|
2019-12-13 23:21:01 +00:00
|
|
|
|
|
|
|
connections = None
|
|
|
|
|
2019-12-04 19:00:50 +00:00
|
|
|
def get_arguments(cls) :
|
2019-12-03 20:27:13 +00:00
|
|
|
"""Returns an array of information used to construct an argumentparser argument."""
|
2019-12-16 21:43:48 +00:00
|
|
|
return ['-r', '--vpn','store_true',"Return VPN information about the subject (openvpn)"]
|
2019-12-03 20:27:13 +00:00
|
|
|
|
2019-12-13 23:21:01 +00:00
|
|
|
def startup(self) :
|
|
|
|
for requirement in ['hosts','username','key'] :
|
|
|
|
if requirement not in self.config or (requirement in self.config and self.config[requirement] is ''):
|
|
|
|
self.error.append('Missing required config option ' + requirement)
|
|
|
|
return
|
|
|
|
self.hosts = self.config['hosts']
|
|
|
|
self.debug("Logging into OpenVPN servers...",1)
|
|
|
|
connections = []
|
|
|
|
for host in self.config['hosts'] :
|
|
|
|
try :
|
|
|
|
sshclient = paramiko.SSHClient()
|
|
|
|
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
sshclient.connect(host,22,username=self.config['username'],password=self.config['key'])
|
|
|
|
connections.append(sshclient)
|
|
|
|
except Exception as exception :
|
|
|
|
self.error.append("Unable to ssh into " + host + ': ' + str(exception))
|
|
|
|
self.connections = connections
|
|
|
|
|
|
|
|
|
|
|
|
def shutdown(self) :
|
|
|
|
for connection in self.connections :
|
|
|
|
connection.close()
|
|
|
|
|
2019-12-16 20:47:10 +00:00
|
|
|
def perform_lookup(self,subject) :
|
|
|
|
search_command = "sudo -S cat /etc/openvpn/openvpn-*p.log | grep 'primary virtual IP for' | grep '" + subject + "' | tail -n 1"
|
2019-12-13 23:21:01 +00:00
|
|
|
for connection in self.connections :
|
|
|
|
try:
|
|
|
|
stdin,stdout,stderr=connection.exec_command(search_command)
|
|
|
|
stdin.write(self.config['key'] + '\n')
|
|
|
|
stdin.flush()
|
|
|
|
result = stdout.readlines()
|
|
|
|
if len(result) > 0 :
|
|
|
|
self.debug('Retrieved line from ssh session: \n' + result[0],2)
|
|
|
|
result = result[0]
|
|
|
|
else :
|
|
|
|
result = ""
|
|
|
|
items = {'nat_address': r'.*\ ([0-9a-fA-F\.\:]*)$', 'source_address': r'.*/([0-9a-fA-F\.\:]*)\ .*', 'user_name': r'.*us=[0-9]*\ ([a-zA-Z0-9\.]*)/.*', 'timestamp': r'(.*)\ us=.*'}
|
|
|
|
return_dictionary = {}
|
|
|
|
for item in items.keys() :
|
|
|
|
matches = re.match(items[item],result)
|
|
|
|
if matches and len(matches.groups()) and matches[1] :
|
|
|
|
return_dictionary.update({item: matches[1]})
|
|
|
|
if return_dictionary is not {} :
|
|
|
|
return return_dictionary
|
|
|
|
except Exception as exception :
|
|
|
|
self.error.append("Unable to get results from ssh: " + str(exception))
|
2019-12-03 20:27:13 +00:00
|
|
|
pass
|
2019-12-13 23:21:01 +00:00
|
|
|
|