You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ff/ssh.py

41 lines
2.1 KiB
Python

#from __future__ import with_statement
from fabric import Connection
from util import find_data_file
from util import setup_child
from util import fprint
from invoke import exceptions
import sys
settings = None
def sftp_send_data(config, filename, filetype):
setup_child()
fprint("Connecting over SSH to " + config['sftp']['host'], settings)
c = Connection(host=config['sftp']['host'], user=config['sftp']['user'], port=config['sftp']['port'], connect_kwargs={"key_filename": find_data_file(config['sftp']['keyfile']),})
fprint("Sending data over SFTP: " + filename, settings)
fprint(c.put(find_data_file(filename), remote=config['sftp']['filepath'][filetype]), settings)
fprint("Data sent over SFTP successfully", settings)
#command = 'ls ' + config['sftp']['filepath'][filetype]
#fprint(c.run(command))
def check_for_file(config, filename, location):
setup_child()
fprint("Connecting over SSH to " + config['sftp']['host'], settings)
c = Connection(host=config['sftp']['host'], user=config['sftp']['user'], port=config['sftp']['port'], connect_kwargs={"key_filename": find_data_file(config['sftp']['keyfile']),})
fprint("Checking for existence of file " + config['sftp']['filepath'][location] + "/" + filename, settings)
try:
res = c.run("ls -l " + config['sftp']['filepath'][location] + "/" + filename, hide=True)
fprint("File " + filename + " exists!", settings)
return c.run("cat " + config['sftp']['filepath'][location] + "/" + filename, hide=True)
except exceptions.UnexpectedExit:
return False
def run_ssh(config, command, location):
setup_child()
fprint("Connecting over SSH to " + config['sftp']['host'])
c = Connection(host=config['sftp']['host'], user=config['sftp']['user'], port=config['sftp']['port'], connect_kwargs={"key_filename": find_data_file(config['sftp']['keyfile']),})
fprint("cd to " + config['sftp']['filepath'][location], settings)
with c.cd(config['sftp']['filepath'][location]):
fprint("Running ssh command: " + command, settings)
res = c.run(command, hide=True, asynchronous=True)
return res