half baked, but works. no progress bar.

Ritesh
-- 
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 base64
import getpass
import os
import socket
import sys
import traceback

import paramiko


# setup logging
#paramiko.util.log_to_file('demo_sftp.log')


def upload(fqdn, login, incoming, files_to_upload, debug, compress, progress):
    # get username
    username = login
    hostname = fqdn
    port = 22
    if username == '':
	default_username = getpass.getuser()
	username = raw_input('Username [%s]: ' % default_username)
	if len(username) == 0:
	    username = default_username
    password = getpass.getpass('Password for %...@%s: ' % (username, hostname))


    # get host key, if we know one
    hostkeytype = None
    hostkey = None
    try:
	host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
    except IOError:
	try:
	    # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/
	    host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts'))
	except IOError:
	    print '*** Unable to open host keys file'
	    host_keys = {}

    if host_keys.has_key(hostname):
	hostkeytype = host_keys[hostname].keys()[0]
	hostkey = host_keys[hostname][hostkeytype]
	print 'Using host key of type %s' % hostkeytype


    # now, connect and use paramiko Transport to negotiate SSH2 across the connection
    try:
	t = paramiko.Transport((hostname, port))
	t.connect(username=username, password=password, hostkey=hostkey)
	sftp = paramiko.SFTPClient.from_transport(t)
	sftp.chdir(incoming)

	# BETTER: use the get() and put() methods
	for file in files_to_upload:
                remote_file = os.path.basename(file)
                print file, remote_file
	        sftp.put(file, remote_file)
	#sftp.put('demo_sftp.py', 'demo_sftp_folder/demo_sftp.py')
	#sftp.get('demo_sftp_folder/README', 'README_demo_sftp')

	t.close()

    except Exception, e:
	print '*** Caught exception: %s: %s' % (e.__class__, e)
	traceback.print_exc()
	try:
	    t.close()
	except:
	    pass
	sys.exit(1)

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to