gora
gora

Reputation: 421

Python: Save output files on another computer through ssh

I want to save output files (binary, matplotlib, etc.) of a Python program in a different computer over ssh. So far, I have saved files in the same computer that runs the program and to do that I have a line in my Python code filename = '/OutputFiles/myOutput.txt'. How do I change this line so that I can save the output in a different computer through ssh? It can be assumed that the ssh login password for the remote computer is in my keyring.

Upvotes: 0

Views: 1561

Answers (2)

whereswalden
whereswalden

Reputation: 4959

(New answer because OP specified they wanted to write multiple files)

You want to look into the paramiko module. It might look something like this:

import paramiko
connection = paramiko.SSHClient()
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connection.connect(10.10.10.10, username=myuser, password=mypass)
ftp = connection.open_sftp()

for i in range(10):
  results = do_stuff()
  f = ftp.open(i+'.txt', 'w+')
  f.write(results)
  f.close()

ftp.close()
connection.close()

Upvotes: 1

whereswalden
whereswalden

Reputation: 4959

The easiest way to do this would be to write your output to standard out and pipe it to ssh (assuming you're on a Mac, Linux, or other *nix-based machine). For example:

python myProgram | ssh user@host 'cat > outfile.txt'

Upvotes: 0

Related Questions