Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import paramiko
except ImportError:
print "SFTP: You will need to install the paramiko library to have sftp support."
raise
url = urlfunctions.url_split(url)
if not url.port:
url.port = 22
self._transport = paramiko.Transport((url.hostname, url.port))
if not url.username:
url.username = getpass.getuser()
if not url.password:
url.password = getpass.getpass(
"SFTP: Please enter the password for %s@%s:" % (url.username, url.hostname)
)
self._transport.connect(username=url.username, password=url.password)
self._connection = paramiko.SFTPClient.from_transport(self._transport)
def get_results(is_all):
""" Get results from server.
"""
# Starting with clean slate
cleanup()
# Read private keys
key = paramiko.RSAKey.from_private_key_file(KEY_DIR)
# Open a transport
transport = paramiko.Transport(('acropolis.uchicago.edu', 22))
transport.connect(username='eisenhauer', pkey=key)
# Initialize SFTP connection
sftp = paramiko.SFTPClient.from_transport(transport)
# Get files
sftp.chdir(CLIENT_DIR)
# Get candidate directories that contain results on the server.
results = []
for candidate in sftp.listdir('.'):
# Determine status of directory or file
if isdir(candidate, sftp):
if 'module' in candidate:
continue
results += [candidate]
# Get all material from the results directories.
for result in results:
os.mkdir(result), os.chdir(result), sftp.chdir(result)
def get_ftp_connection(self):
transport = paramiko.Transport(self.host)
transport.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(transport)
return sftp
# Create a sql dump
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password)
stdin, stdout, stderr = client.exec_command('scl enable postgresql92 "./dump_sidewalk_tables.sh %s"' % sql_dump_filename)
stdout.read()
client.close()
print "Created the database dump %s on the remote server" % sql_dump_filename
# Download the file
print "Opening a sftp port"
port = 22
transport = paramiko.Transport((hostname, port))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
print "Downloading the database dump %s to local path %s" % (remote_filename, sql_dump_filename)
sftp.get(remote_filename, sql_dump_filename)
sftp.close()
transport.close()
print "Download complete"
def sftp_upload_file(host,user,password,ssh_port,server_path, local_path):
try:
t = paramiko.Transport((host, ssh_port))
t.connect(username=user, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(local_path, server_path)
t.close()
except Exception, e:
print e
def _make_sftp(self):
transport = self.client.get_transport()
transport.open_session()
return paramiko.SFTPClient.from_transport(transport)
def sftp_ctx():
"""
Context manager that provides an SFTP client object
(an SFTP session across an open SSH Transport)
"""
transport = paramiko.Transport((current_app.config['SFTP_HOSTNAME'],
int(current_app.config['SFTP_PORT'])))
transport.connect(username=current_app.config['SFTP_USERNAME'],
pkey=paramiko.RSAKey(filename=current_app.config['SFTP_RSA_KEY_FILE']))
sftp = paramiko.SFTPClient.from_transport(transport)
try:
yield sftp
except Exception as e:
raise paramiko.SFTPError("Exception occurred with SFTP: {}".format(e))
finally:
sftp.close()
transport.close()
def make_sftp(self):
"""Make SFTP client from open transport"""
transport = self.client.get_transport()
transport.open_session()
return paramiko.SFTPClient.from_transport(transport)
def get_results(is_all):
""" Get results from server.
"""
# Starting with clean slate
cleanup()
# Read private keys
key = paramiko.RSAKey.from_private_key_file(KEY_DIR)
# Open a transport
transport = paramiko.Transport(('acropolis.uchicago.edu', 22))
transport.connect(username='eisenhauer', pkey=key)
# Initialize SFTP connection
sftp = paramiko.SFTPClient.from_transport(transport)
# Get files
sftp.chdir(CLIENT_DIR)
# Get results directory, this is always downloaded.
get_directory('rslts', sftp)
# If requested, also download all intermediate results as well.
if is_all:
for candidate in sftp.listdir('.'):
# Only directories are even potentially interesting.
if not isdir(candidate, sftp):
continue
# Check if directory name can be transformed into a float.
try:
float(candidate)
host_server = self.fqdn_to_shortname(app.host_server)
self.logger.info("Bootstrapping {hostname} on server: {host_server} port: {port}".format(
hostname=app.hostname,
host_server=host_server,
port=app.ssh_port))
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host_server, port=app.ssh_port,
username='root', password='newroot')
transport = paramiko.Transport((host_server, app.ssh_port))
transport.connect(username = 'root', password = 'newroot')
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put('bootstrap.sh', '/root/bootstrap.sh')
sftp.put('start.sh', '/root/start.sh')
ssh.exec_command("chmod +x /root/bootstrap.sh")
ssh.exec_command("chmod +x /root/start.sh")
stdin, stdout, stderr = ssh.exec_command("bash /root/start.sh")
self.logger.debug(''.join(stdout.readlines()))
ssh.close()
except SSHException:
self.logger.error("Failed to log into server. Shutting it down and cleaning up the mess.")
self.delete_container(app.host_server, app.container_id)