-2

I have a Python script I am trying to run in a Docker container to send a file that is on this container to an SFTP server.

I tried the following :

import paramiko

ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session = ssh.connect(hostname="X", port=X, username='X', password="X")

stdin,stdout,stderr = ssh.exec_command('sftp -P X ldzdl@hostname', get_pty=True)

I also tried with paramiko transport method but didn't work from remote (docker container) to remote SFTP.

But I have the following error : paramiko.ssh_exception.AuthenticationException: Authentication failed.

How can I do this ? I don't know if my method is okay or if there is other better way to solve it (send data from container to an SFTP server).

David Maze
  • 130,717
  • 29
  • 175
  • 215
lbened
  • 65
  • 6
  • 1
    Did you try to manually send a file from the command line to the remote SFTP server from the container? Did it work? – D3M0N1K Feb 11 '23 at 09:34
  • Hello @D3M0N1K it works. I did sftp -P port_number user@hostname:data. Next it prompted me the password I have to entered manually. Next I did the command put file and it worked. How can I do it in Python to avoid entering manually the password and do these two commands ? – lbened Feb 11 '23 at 10:17
  • Yes I tried it locally too, it doesn't work. Same error. Maybe I can do it with subprocess or bash script ? I believe in subprocess I can't give the password to avoid entering manually. – lbened Feb 11 '23 at 11:03
  • 1
    If you cannot do locally, why do you complicate your question with Docker? We need [mcve]. You might have this problem: [Paramiko authentication fails with "Agreed upon 'rsa-sha2-512' pubkey algorithm" (and "unsupported public key algorithm: rsa-sha2-512" in sshd log)](https://stackoverflow.com/q/70565357/850848). – Martin Prikryl Feb 11 '23 at 11:28
  • Thank you @Martin Prikryl . Is is possible to do sftp -P port_number user@hostname:data command and enter dynamically password in subprocess ? – lbened Feb 11 '23 at 12:31
  • So are you basically asking how to upload a file from local machine to an SFTP server using Paramiko? No Docker, no jump hosts (what your code looks like you were attempting)? There are many question about upload with Paramiko already here. They all show that you should use `SFTPClient.put`. So why are you trying `exec_command`? – Martin Prikryl Feb 11 '23 at 13:18

1 Answers1

0

The argument given to the exec_command function is not the command you would normally run on the local (client) host's shell, but rather attempted to be ran on the remote (server) host. While it is not likely to get an AuthenticationException by attempting to run remote commands, as you did not post a full traceback in the question - it is hard to tell for sure.
I suggest checking the following code:

import paramiko

ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session = ssh.connect(hostname="X", port=X, username='X', password="X")
### so far, same code as in the question ###

print("Auth OK")
sftp_connection = ssh.open_sftp()

sftp_connection.put("local/file/path", "remote/file/path")

If you see the "Auth OK" print - then you should be good to go, just replace the file path arguments of the sftp_connection.put() method with actual local and remote file paths.
Otherwise - there is an actual authentication issue which should be resolved.

micromoses
  • 6,747
  • 2
  • 20
  • 29
  • It prints "Auth OK" but I have : OSError: Failure for sftp_connection.put. When I print session it is "None" – lbened Feb 11 '23 at 13:05
  • 1
    `session` should be `None`, as `ssh.connect()` should not return anything (that code was simply copied from the question as-is). An `OSError` is actually good news - it just means that `local/file/path` doesn't exist on your client. You should replace the arguments of `put()`, the first would be your local file path, and the second would be the path you want to upload to - on the server. – micromoses Feb 11 '23 at 13:12
  • 2
    @lbened How can you get *"Auth OK"*, if the authentication code is the same as in your question that gets `AuthenticationException`? Sorry, but the information you provide are totally unclear. – Martin Prikryl Feb 11 '23 at 13:20
  • @micromoses It works thank you ! I tried it locally, will it be the same on my container ? – lbened Feb 11 '23 at 13:33
  • 1
    I cannot say if it will run from a container, as every container is different. There may be authentication issues, filename issues, permissions, connectivity, platform interoperability.. the list of things that can go wrong is practically infinite. I'm glad I was able to help from a coding perspective, but @MartinPrikryl is on point - for anyone to be able to help, you first need to try; and if things don't work - try explaining them as detailed as you possibly can. Best of luck :) – micromoses Feb 11 '23 at 14:09