Issue
I am trying to use Paramiko to get a log file from my Raspberry Pi:
import paramiko
paramiko.util.log_to_file("paramiko.log")
# Open a transport
host, port = RECV_IP_ADDRESS, 22
transport = paramiko.Transport((host, port))
# Auth
username, password = "pi", "raspberry"
transport.connect(None, username, password)
# Go!
sftp = paramiko.SFTPClient.from_transport(transport)
# Download
filepath = "/"
localpath = "/home/pi/Code/log.txt"
sftp.get(filepath, localpath)
# Close
if sftp: sftp.close()
if transport: transport.close()
However when I run this program I get a file not found error:
FileNotFoundError: [Errno 2] No such file or directory: '/home/pi/Code/log.txt'
Clearly the file is here
po@raspberrypi:~/Code $ pwd
/home/pi/Code
po@raspberrypi:~/Code $ ls
a.out log.txt test.c
po@raspberrypi:~/Code $ _
But the program can't seem to find it.
I had thought that perhaps the connection had died. I tried a suggestion found here:
Check if paramiko ssh connection is still alive
transport.send_ignore()
Did not yield an error.
I am new to Paramiko. Do I need to run something on the Pi in order for this to work? Is it not simply a wrapper for SFTP/SSH?
Solution
Your immediate problem is that you have the parameters of
SFTPClient.get
the other way around (you even have the variables named wrong, thelocalpath
should obviously beremotepath
). So Paramiko tries to create a local file with the path of your remote file. As/home/pi/Code
does not exist on your local (target) machine, you get localFileNotFoundError
error.Once you correct this, you will have another problem. The
localpath
path argument ofSFTPClient.get
needs to be path to the file, not only path to the target directory. See Python Paramiko, PermissionError: [Errno 13] Permission denied when get files from remote server.
So like this:
localpath = "/log.txt"
remotepath = "/home/pi/Code/log.txt"
sftp.get(remotepath, localpath)
Answered By - Martin Prikryl Answer Checked By - Clifford M. (WPSolving Volunteer)