0

For some reason I have a master script that required below coding:

vi test_run.sh    
#!/bin/sh
ssh -tt 192.168.1.20

To execute the command I run:

cat test_run.sh | sh -

I'm able to connect to the remote server but I can't issue any command there, e.g ls follow by Enter, cause the screen to hang.

  • Why are you doing cat foo | sh - instead of just foo? – Sparhawk Jan 25 at 3:40
  • 2
    @Sparhawk Probably because they haven't chmod +x test_run.sh. Still, sh test_run.sh would have been better than the cat through a pipe. – Kusalananda Jan 25 at 6:41
  • does it return something if you type ssh 192.168.1.20 'echo yes' ? if no i might be an ssh problem, if yes if might then be an MTU problem between your machine and 192.168.1.20. – dominix Jan 25 at 6:54
2

ssh will inherit the standard input stream of the shell that executes it.

The standard input of the shell that executes ssh is being used to read the shell script (from the pipe from cat), and is therefore not available for interactively issuing commands to ssh. This is why you are trying with -tt to make the ssh session interactive (ssh would complain otherwise, saying Pseudo-terminal will not be allocated because stdin is not a terminal), but it doesn't work because, as mentioned, the stream that is used supply it with commands from the user is being used by the shell to read the actual script.

Instead, run the script as usual:

sh test_run.sh

This would leave the standard input stream available for you to issue commands interactively to the remote shell session, and your invocation of ssh would also not need -tt at all.

If your script was a bit longer:

#!/bin/sh

ssh -tt 192.168.1.20
hostname
echo "DONE"

and if you ran it as

cat test_run.sh | sh -

you would notice that the machine that executes both hostname and the echo is actually the remote machine. This is because the standard input of the shell (connected to the stream read from cat, containing the script) will be passed on to ssh. This has the effect of passing the commands in the script on to the shell started on the remote system (as if you typed them into the remote shell).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.