Dear Mike,
Thanks for your help, but I am new in paramiko,would you please help me
more?
May I connect to server by password insted of private key? How?
I used this code:
....
t.auth_password(username, password, event)
....
but I received this error :
paramiko.SSHException : No existing session
I think my problem is in authentication, Do you think I have to use pycrypto
module or something like that??
If not would you please tell me how should I precreate the key and ensure
the server
side has it in its authorized_keys? How do I share that as I mentioned it
before I am using two different platforms?
Thanks.
Mike said:
here is a snippet that works, you need to replace the default data or
create a database etc.. and the fields to store it
you also need to generate and share your public key to the receiving
server to avoid password prompt
# Standard library imports
import os
import sys
import time
import traceback
import paramiko
# my defaultdata is stored in a sqllite table called merchant.lib
# you can just assign values to defaultdata or recreate a database of
your own
from merchant.lib import defaultdata
# set up default values
hostname = defaultdata.defaults['hostname']
username = defaultdata.defaults['username']
password = defaultdata.defaults['password']
# paramiko required data
port = defaultdata.defaults['port']
fname = defaultdata.defaults['fname']
pathupload = defaultdata.defaults['pathupload']
pathdownload = defaultdata.defaults['pathdownload']
input_dir = defaultdata.defaults['input_dir']
output_dir = defaultdata.defaults['output_dir']
curdir = os.path.abspath(os.curdir)
# load ssh pkey you need to precreate the key and ensure the server
side
# has it in its authorized_keys
pkey_path = os.path.expanduser('~/.ssh/id_dsa')
pkey = paramiko.DSSKey.from_private_key_file(pkey_path)
paramiko.util.log_to_file('transfer.log')
logger = paramiko.util.get_logger('paramiko')
try:
# set up connection
t = paramiko.Transport((hostname, port))
t.connect(username=username, pkey=pkey)
sftp = paramiko.SFTPClient.from_transport(t)
# upload files
files = [file for file in os.listdir(output_dir)
if file.endswith('.txt')]
logger.info("UPLOAD FILES %s" % files)
os.chdir(output_dir)
for file in files:
sftp.put(file, os.path.join(pathupload, file))
os.rename(file, file + '.old')
# download files
response_files = [file for file in sftp.listdir(pathdownload)
if file.startswith('DISCOVER_RESPONSE_')]
logger.info("RESPOSE FILES: %s" % response_files)
os.chdir(os.path.join(curdir, input_dir))
for file in response_files:
file_path = os.path.join(pathdownload, file)
sftp.get(file_path, file)
sftp.remove(file_path)
# close connection
t.close()