On Sunday 24 May 2015 08:33 PM, Ritesh Raj Sarraf wrote:
> On Friday 22 May 2015 04:31 PM, Ritesh Raj Sarraf wrote:
>> And I just revised the patch (attached).
> Actually, please use the newer one attached.
>
Some minor import cleanups.

-- 
Ritesh Raj Sarraf
RESEARCHUT - http://www.researchut.com
"Necessity is the mother of invention."

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2009  Ritesh Raj Sarraf <r...@researchut.com>
#
# Based on paramiko's example file
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#

import getpass
import os
import socket
import sys

import paramiko


def upload(fqdn, login, incoming, files_to_upload, debug, compress, progress):


    ssh = paramiko.SSHClient()

    # Load system host keys, if any
    ssh.load_system_host_keys()

    try:
	host_keys = ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
    except IOError:
	print '*** Unable to open host keys file'

    try:
        ssh.connect(fqdn)
    except paramiko.SSHException:
        print "\nWARNING: Host %s not in known_hosts" % (fqdn)
	print "WARNING: Continue?\n"

        user_input = raw_input('Type \'YES\' to add %s to known_hosts: ' % fqdn)

        if user_input == 'YES':
            # Add the host to SSH known_hosts
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy() )

            # And connect again.
            ssh.connect(fqdn)
        else:
            # get username
            username = login
            port = 22
            if username == '' or username == '*':
	        default_username = getpass.getuser()
	        username = raw_input('Username [%s]: ' % default_username)
                password = getpass.getpass('Password for %s@%s: ' % (default_username, fqdn))
	    if len(username) == 0:
                username = default_username

	    # It is the user's responsibility to be sure of the host they are connecting to
	    print "\nWARNING: Cannot trust authenticity of host %s " % (fqdn)

	    response = raw_input('Press \'YES\' again to connect to host %s: ' % fqdn)
	    if response == 'YES':
		ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy() )
	    else:
		print "Aborting....\n"
		sys.exit(1)
            # Here, connect with user provided username/password
            ssh.connect(fqdn, port, username, password)

    # Let's establish an SFTP connection now
    sftp = ssh.open_sftp()

    # Chdir to incoming path
    sftp.chdir(incoming)

    # Now put the file up
    for file in files_to_upload:
        remote_file = os.path.basename(file)

	print "Transferring file %s to %s" % (remote_file, fqdn)
        #TODO: Find out how to make it do resume
	ret = sftp.put(file, remote_file)
        print "%s of size %d tranferred to %s" % (remote_file, ret.st_size, fqdn)

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to