1

I have scripts in several languages which I pipe to ssh to have them invoked on other hosts. For shell scripts I'm able to do this:

ssh < /path/to/script.sh

For scripts in other languages however, I find myself having to do this:

ssh python - < /path/to/script.py

Locally I can run the scripts without specifying the interpreter, due to their shebang lines, and executable bits.

How can I have ssh execute scripts regardless of language by invoking the appropriate interpreter from the shebang? Is there something I can have ssh invoke regardless of scripts language to do this?

Update0

The ssh invocation is part of yet another script which I am developing, this is strictly specific to coding and not server management.

Zypher
  • 37,405
  • 5
  • 53
  • 95
Matt Joiner
  • 191
  • 1
  • 8

2 Answers2

3

Could you invoke a command that reads it's input into a temporary file and executes that? Something like

ssh user@host '{ F=$(mktemp /tmp/scripts.XXXXXX); cat > $F ; chmod +x $F; $F; rm $F }' < foo.py
  • curious as to why you have the whole command in `{}`? – Matt Joiner Jul 20 '11 at 01:07
  • Overcorrection? :-) –  Jul 20 '11 at 01:08
  • No comprendo, please explain :P – Matt Joiner Jul 20 '11 at 03:26
  • It doesn't seem to be required in this case (though this might be a bash-ism), but I'm used to wrapping groups that need descriptor redirection in `{}`. Like people who use too many commas, correct the spelling of already correct words (thereby misspelling them), etc. :-) –  Jul 20 '11 at 04:06
3

You can't. The shebang line is recognized by the OS when it's exec-ing a file. When you're piping it over ssh, you're just sending commands to the shell (/bin/sh) or whatever other command you specified should be run.

The only way to have the remote server recognize the shebang is to copy the files to the remote server and set the execute bit.

You could fake it by writing a wrapper around ssh that looks for a shebang line in standard input to determine what command to run, or by doing some trickery to make non-sh scripts run under sh, like the classic tcl idiom

# \
exec tclsh "$0" "$@"

(I don't know the equivalent for other languages)

evil otto
  • 151
  • 2