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:
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:
ssh -tt 192.168.1.20
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).