Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

To access a windows machine remotely I have to tunnel through a ubuntu server of mine. To setup the tunnel I have

ssh -l root -C -L 9999:windowsmachine:23389 myserver

Then I have to open another console and run

rdesktop -u user -password -f localhost:9999

How can I combine these in a single command or script?

share|improve this question

(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.

share|improve this answer
You can also use the "-f" and "-N" flags (background, and no remote command execution), then you can run ssh first and rdesktop afterwards without a need for the sleep command. – Derek Pressnall Jan 22 '13 at 1:15
@Derek I like your answer much better than my own. :) You should probably submit it as an answer and get the credit, it's the more efficient solution. – Andrew B Jan 22 '13 at 1:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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