Reputation: 63
I'm using Paramiko to connect to a server via SSH, run a command to generate a report, then download the report to my local computer. Everything seems to work fine without an error, but the resulting file is blank on my local computer. I'm using Python 2.7 and the latest version of Paramiko. The file that I'm trying to download is a .csv . I've verified that the file contains data server-side.
The code I am using is below:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=user_name, key_filename=key_file)
except:
print 'error connecting'
try:
stdin, stdout, stderr = ssh.exec_command(report_cmd)
except:
print 'error generating report'
try:
sftp = ssh.open_sftp()
sftp.get(source_str, dest_str)
except:
print 'failed to DL file' + str(sys.exc_info())
ssh.close()
Upvotes: 2
Views: 2143
Reputation: 69041
You are trying to download the file before the ssh.exec_command
has finished.
You need to wait; the best way would be to read from stdout
and/or stderr
until EOF
.
I think those reads will be blocking, but since you have nothing else to do until they finish anyway that should be fine. And I beleive EOF
will look like an empty string: ""
.
Upvotes: 1