Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to run a python script on remote machine without sending it?

Tags:

python

ssh

I can run a shell script on remote machine with ssh. For example:

ssh -l foo 192.168.0.1 "`cat my_script.sh`"

Now I want to run a python script without sending .py file. Is there any way?

like image 696
ibrahim Avatar asked Oct 01 '12 11:10

ibrahim


People also ask

How do I run a python script remotely?

If you want to run an entire script (such as a bash or even a python application) on another server from your local machine, you can make use of the SCP module to upload your script, then simply execute it using the same technique we used above with Paramiko.

How do I SSH into a python script?

SSH is widely used by network administrators for managing systems and applications remotely, allowing them to log in to another computer over a network, execute commands and move files from one computer to another. In python SSH is implemented by using the python library called fabric.


1 Answers

This will put the contents of my_script.py on your computer into an echo command that is performed on the remote computer and passed into python.

ssh -l foo 192.168.0.1 "echo '`cat my_script.py`' | python"

If you want to add command line args it should be as simple as putting them after the python command like so:

ssh -l foo 192.168.0.1 "echo '`cat my_script.py`' | python -testSwitch -arg 0"

Make sure that the command line args are inside the double quotes of the command you are sending to the remote host.

like image 57
Sean Dawson Avatar answered Oct 20 '22 03:10

Sean Dawson