Commit 718ec0c2 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Add ssh username to config file

- Add paramiko for SSH handling
- Add connection code to create SFTP connection to remote host
parent 46ea15e0
......@@ -37,4 +37,7 @@ dbhost = localhost
dbport = 3306
# Location of the gpg binary. This should be the full path and binary name.
gpg_binary = /usr/bin/gpg
\ No newline at end of file
gpg_binary = /usr/bin/gpg
# Username for SFTP process
sftp_username = skynet
\ No newline at end of file
......@@ -28,6 +28,7 @@ import os, os.path
import re
import MySQLdb
from netaddr import IPAddress
import paramiko
# Global Variables
verbose = False
......@@ -112,15 +113,20 @@ def main(argv):
# Build configs
configlist = build_configs(dbhandle, server['id'])
server_conn = connect_server(server['ip'], server['key_type'],
server['ssh_key'])
# SCP configs over (if there are configs)
if (len(configlist) > 0):
push_configs(server, server['ssh_key'])
push_configs(server_conn, configlist)
# Get results from server
get_results(dbhandle, str(server['id']), server['ssh_key'])
get_results(dbhandle, server_conn, str(server['id']))
# Process results, update database, store results
server_conn.close()
# Process results, update database, store results
# Sleep until the beginning of the next minute
sleeptime = 60 - datetime.utcnow().second
logger.info('Sleeping until next loop iteration - {0} seconds'
......@@ -147,6 +153,7 @@ def loadconfig():
cfg['dbport'] = config.get('skynet', 'dbport')
cfg['dbuser'] = config.get('skynet', 'dbuser')
cfg['dbpass'] = config.get('skynet', 'dbpass')
cfg['sftp_user'] = config.get('skynet', 'sftp_username')
for k, v in cfg.items():
logger.debug('{0} = {1}'.format(k, v))
......@@ -154,10 +161,10 @@ def loadconfig():
def get_servers(dbhandle):
logger.info('Loading server list')
sql = '''SELECT DISTINCT c.id, c.server_ip, c.ssh_key, c.gpg_key FROM cloud
AS c, timers AS ti, target AS t, spawn AS s WHERE (ti.cloud_id =
c.id AND ti.spawn_id = s.id AND ti.target_id = t.id) AND
(c.last_contacted <= c.last_modified OR c.last_contacted <=
sql = '''SELECT DISTINCT c.id, c.server_ip, c.key_type, c.ssh_key, c.gpg_key
FROM cloud AS c, timers AS ti, target AS t, spawn AS s WHERE
(ti.cloud_id = c.id AND ti.spawn_id = s.id AND ti.target_id = t.id)
AND (c.last_contacted <= c.last_modified OR c.last_contacted <=
ti.last_modified OR c.last_contacted <= t.last_modified OR
c.last_contacted <= s.last_modified OR c.last_contacted <=
DATE_SUB(NOW(), INTERVAL c.contact_frequency MINUTE))'''
......@@ -175,10 +182,31 @@ def get_servers(dbhandle):
for row in rows:
logger.debug('Database Retrieval : ' + str(row))
serverlist.append({ 'id' : row[0], 'ip' : str(IPAddress(row[1])),
'gpg_key' : row[2], 'ssh_key' : row[3]})
'key_type' : row[2], 'ssh_key' : row[3],
'gpg_key' : row[4]})
return serverlist
def connect_server(server_ip, key_type, ssh_key):
logger.info('Creating SSH connection to server {0}'.format(server_ip))
if (key_type == 'RSA'):
server_key = paramiko.RSAKey.from_private_key_file(ssh_key)
else:
server_key = paramiko.DSSKey.from_private_key_file(ssh_key)
try:
transport = paramiko.Transport((server_ip))
transport.start_client()
transport.auth_publickey(cfg['sftp_user'], server_key)
sftp = paramiko.SFTPClient.from_transport(transport)
except:
e = sys.exc_info()[0]
logger.exception('BROKED = {0}'.format(e))
return sftp
def build_configs(dbhandle, server_id):
logger.info('Building server configuration')
......@@ -210,10 +238,10 @@ def build_configs(dbhandle, server_id):
return configlist
def push_configs(server, key):
def push_configs(server_conn, configlist):
logger.info('Pushing server configuration')
def get_results(dbhandle, server_id, key):
def get_results(dbhandle, server_conn, server_id):
logger.info('Retrieving results from server')
logger.info('Updating last_contacted for server {0}'.format(server_id))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment