2019-12-03 20:27:13 +00:00
|
|
|
from servicebase import ServiceBase
|
2019-12-13 23:21:01 +00:00
|
|
|
import paramiko
|
2020-05-26 18:26:01 +00:00
|
|
|
import datetime
|
2019-12-13 23:21:01 +00:00
|
|
|
import re
|
2019-12-03 20:27:13 +00:00
|
|
|
|
|
|
|
class ServiceDelegate(ServiceBase) :
|
2019-12-13 23:21:01 +00:00
|
|
|
|
2020-05-26 18:26:01 +00:00
|
|
|
_connections = None
|
2019-12-13 23:21:01 +00:00
|
|
|
|
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."""
|
2020-05-22 18:28:13 +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'] :
|
2020-07-14 21:36:46 +00:00
|
|
|
if requirement not in self._config or (requirement in self._config and self._config[requirement] == ''):
|
2020-05-26 18:26:01 +00:00
|
|
|
self._error.append('Missing required config option ' + requirement)
|
|
|
|
rself._error
|
|
|
|
self.hosts = self._config['hosts']
|
2020-05-22 18:28:13 +00:00
|
|
|
self.debug('Logging into OpenVPN servers...',1)
|
|
|
|
connections = {}
|
2020-05-26 18:26:01 +00:00
|
|
|
for host in self._config['hosts'] :
|
2019-12-13 23:21:01 +00:00
|
|
|
try :
|
|
|
|
sshclient = paramiko.SSHClient()
|
|
|
|
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
2020-05-26 18:26:01 +00:00
|
|
|
sshclient.connect(host,22,username=self._config['username'],password=self._config['key'])
|
2020-05-22 18:28:13 +00:00
|
|
|
connections.update({host:sshclient})
|
2019-12-13 23:21:01 +00:00
|
|
|
except Exception as exception :
|
2020-05-26 18:26:01 +00:00
|
|
|
self._error.append('Unable to ssh into ' + host + ': ' + str(exception))
|
|
|
|
self._connections = connections
|
2019-12-13 23:21:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def shutdown(self) :
|
2020-05-26 18:26:01 +00:00
|
|
|
for connection in self._connections.values() :
|
2019-12-13 23:21:01 +00:00
|
|
|
connection.close()
|
|
|
|
|
2020-05-22 02:34:56 +00:00
|
|
|
def lookup(self,subject) :
|
2020-07-20 22:55:56 +00:00
|
|
|
search_command = 'sudo -S cat /var/log/openvpn/openvpn.log | grep \'primary virtual IP for\' | grep -i \'' + subject + '\' | tail -n 1'
|
2020-05-26 18:26:01 +00:00
|
|
|
final_dictionary = {}
|
|
|
|
for host,connection in self._connections.items() :
|
2019-12-13 23:21:01 +00:00
|
|
|
try:
|
|
|
|
stdin,stdout,stderr=connection.exec_command(search_command)
|
2020-05-26 18:26:01 +00:00
|
|
|
stdin.write(self._config['key'] + '\n')
|
2019-12-13 23:21:01 +00:00
|
|
|
stdin.flush()
|
|
|
|
result = stdout.readlines()
|
|
|
|
if len(result) > 0 :
|
2020-05-26 18:26:01 +00:00
|
|
|
self.debug('Retrieved line from ssh session: \n' + result[0].strip(),2)
|
2019-12-13 23:21:01 +00:00
|
|
|
result = result[0]
|
|
|
|
else :
|
2020-05-22 18:28:13 +00:00
|
|
|
result = ''
|
|
|
|
items = {'nat_address': r'.*\ ([0-9a-fA-F\.\:]*)$', 'source_address': r'.*/([0-9a-fA-F\.]*)\:.*', 'user_name': r'.*primary\ virtual\ IP\ for\ ([a-zA-Z0-9\.]*)/.*', 'timestamp': r'^([a-zA-Z]{3}\ +[0-9]{1,2}\ [0-9:]*)\ .*'}
|
2019-12-13 23:21:01 +00:00
|
|
|
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]})
|
2020-05-22 18:28:13 +00:00
|
|
|
if len(return_dictionary) > 0 :
|
|
|
|
return_dictionary.update({'host':host})
|
2020-05-26 18:26:01 +00:00
|
|
|
|
|
|
|
epoch = None
|
|
|
|
if 'timestamp' in final_dictionary.keys() and 'timestamp' in return_dictionary.keys() :
|
|
|
|
epoch = int(datetime.datetime.strptime(return_dictionary['timestamp'] + ' ' + str(datetime.datetime.now().year), '%b %d %H:%M:%S %Y').strftime('%s'))
|
|
|
|
if len(return_dictionary) > 0 :
|
|
|
|
if epoch :
|
|
|
|
if epoch > int(datetime.datetime.strptime(final_dictionary['timestamp'] + ' ' + str(datetime.datetime.now().year), '%b %d %H:%M:%S %Y').strftime('%s')) :
|
|
|
|
final_dictionary = {}
|
|
|
|
final_dictionary.update(return_dictionary)
|
|
|
|
else :
|
|
|
|
final_dictionary = return_dictionary
|
2020-05-22 02:34:56 +00:00
|
|
|
else :
|
2020-05-26 18:26:01 +00:00
|
|
|
self.debug('No results for ' + subject + ' on host ' + host,2)
|
2019-12-13 23:21:01 +00:00
|
|
|
except Exception as exception :
|
2020-05-26 18:26:01 +00:00
|
|
|
raise exception
|
|
|
|
self._error.append('Unable to get results from ssh: ' + str(exception))
|
|
|
|
if len(final_dictionary) > 0 :
|
|
|
|
return final_dictionary
|
|
|
|
else :
|
|
|
|
return None
|