sftp problem!

S

sa6113

I want to use sftp from paramiko to copy a file from a windows machine to a
Linux in the network, I use this code :

host = "LinuxComputerName" (or its Ip)
port = 22
transport = paramiko.Transport((host, port))
password = "LinuxComputerPassword"
username = "LinuxComputerUserName"

transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
filepath = '/home/test.jpg'
localpath = '/home/test.jpg'
sftp.get(filepath, localpath)

sftp.close()
transport.close()

and after runing this code I get this error:
socket.error: (10061, 'Connection refused')

What is wrong with this code?
please help me.
 
L

Lawrence D'Oliveiro

sa6113 said:
host = "LinuxComputerName" (or its Ip)
port = 22

and after runing this code I get this error:
socket.error: (10061, 'Connection refused')

Did you try

sftp LinuxComputerName

just to confirm it's got a listener on port 22?
 
M

Mike Hjorleifsson

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()
 
S

sa6113

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top