1

I'm using Linux to log into a server using ssh. Once I'm logged in, I would like to execute a local Python script which uses netcat to run some program on the remote machine (it is therefore required to be logged in to access it).

Locally I can type ./script.py on bash to run the script however if I do it while being connected to the remote server that doesn't work. This is due to the fact that the terminal now is in a directory on the remote machine and no longer in a local one.

So how do I execute my local script against the remote machine while being logged in without copying it over first?

1 Answer 1

2

You can do it easily using pipes:
my sample program to test: (py.py)

import socket print socket.gethostname()

running remotely:

ssh remoteserver "python" < py.py

the output was:

remoteserver name

If your user's path not contains python, you must use full path

ssh remoteserver "/usr/bin/python" < py.py

in my environment.

Sign up to request clarification or add additional context in comments.

3 Comments

Is this solution specific to python or possible to use this in other language as well (ofcourse in their syntax) ?
any program what can handle input redirection (pipes) can run by this method - I used perl, php, bash shell by this way before.
This is really awesome trick. I did not know about it earlier. Also my question was pretty lame. I did not think through before asking as i was admiring your solution. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.