(Disclaimer: See comments for a more efficient solution.)
This is tricky for a number of reasons:
ssh needs to run in the foreground to hold the connection open. This makes it challenging to do this within a single instance of a shell, because the second command needs to execute in parallel to it being foreground.
- You can't test for the return status of the
ssh command, because the connection will have closed by then. This means the resktop command will fire regardless of whether ssh authentication succeeded.
- For the
rdesktop command to not be interactive, it needs to supply the password in the command line. Storing the password in the script is a bad idea.
Try something like this:
# Request password, no echo
read -s RDP_PW
# Insert password validation here (i.e. make sure we got input)
# Execute sleep+rdesktop in a background subshell, redirect STDOUT to /dev/null.
( sleep 5; rdesktop -u user -p$RDP_PW -f localhost:9999 ) >/dev/null &
# Run the ssh command. We have 5 seconds establish the tunnel.
ssh -l root -C -L 9999:windowsmachine:23389 myserver
Adjust the sleep statement as needed. You may want to remove the /dev/null redirection until you're sure this works. You also may want to redirect STDERR.
A better approach relies on the terminal program you're using within X, or whether or not you're familiar with a terminal multiplexer. If your terminal program provides a way for you to invoke a command in a new tab, prefix
ssh with the appropriate command for doing that. If you go the multiplexer route, use the syntax for invoking
ssh in a new multiplexer window. Dealing with passwords prompts from
ssh in both cases is up to you.