Bezz
Bezz

Reputation: 118

Run python script over SSH

I am trying to run a python script that will connect to SSH and continue to execute code on that server. I can get the connection to the server with my first lines:

cmd = 'ssh username@host'     
os.system(cmd)

But any code following will wait until I am disconnected from SSH to execute. How do I continue to execute code all within one script?

Ideally, I will connect to the SSH server create a few files and copy them to my computer to do stuff with them in the same script.

I have tried Paramiko is there a way around that? Thanks.

Upvotes: 2

Views: 2690

Answers (2)

gavinb
gavinb

Reputation: 20018

As others have commented, use Fabric. It provides a high level interface for remote control over ssh and they have already done all the hard work of managing authentication, keys, error handling and so on. It will be much easier than trying to run things via a system call and managing it all yourself.

More details:

Upvotes: 2

Dave
Dave

Reputation: 612

Pipe the script to the remote bash.

cat script.sh | ssh user@server /bin/bash

Upvotes: 1

Related Questions