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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With