11

I'm trying to build a Windows batch file, which starts putty/plink with port forwarding, and nothing else. The Windows part is ready so far:

start plink.exe -ssh -i key.ppk -L 1234:localhost:80 sampleUser@192.168.0.1

As I don't want to allow to execute other commands after authentication, I'm using ForceCommand with a Match User declaration:

Match User sampleUser
    ForceCommand echo 'Success! Close this window to log out.'

Problem is, running my batch file starts putty correctly, but it closes immediately after executing echoing my specified text.

My idea is to use something like this:

ForceCommand echo 'Success! Close this window to log out.' && waitTillControlC

This way, putty/SSH should keep the connection alive and doesn't quit my port forwarding.

I've thought of commands like yes, ping or read, but they

  • are spamming my terminal window
  • are actually doing stuff / generating unnecessary CPU load
  • could close unexpectedly, if someone presses enter

Is there a command which will do nothing, forever, till somebody terminates it with Ctrl+C or closes the SSH connection by closing the putty window?

What should I use for waitTillControlC?

stuXnet
  • 265

5 Answers5

14

I don't get why all the other replies want to use a loop here, sleep is able to wait long enough for any practical purpose and will use no clock cycles doing it.

For example it will try here to sleep for a dubious one hundred years and will likely do it enough to comply with the request anyway:

echo "Something"; sleep 36500d

Alternatively, this should block too until you type the Enter key:

echo "Something"; read foo
jlliagre
  • 14,369
9

Try the -N option instead. From the plink documentation:

-N        don't start a shell/command (SSH-2 only)

This is the same behavior as the ssh option -N, as described in the man page:

-N      Do not execute a remote command.  This is useful for just for-
         warding ports (protocol version 2 only).
Moshe Katz
  • 3,488
chepner
  • 7,041
8

This should sleep forever without consuming any (noticeable) amount of CPU power.

echo "Something" && while true; do sleep 9999; done

I'm also not sure whether you can give a command like in the ForceCommand clause. You may need to put the command in a shell script.

#!/usr/bin/env bash
echo "Success! Close this window to log out." && while true; do sleep 9999; done

This script should of course be in a place and have permissions such that no ordinary user on the server can write to it.

Match User sampleUser
    ForceCommand /usr/bin/waitforever

Edit

I've found a command which seems more elegant:

echo "Something" && tail -f /dev/null

tail -f waits for a file or stream to return bytes. (Otherwise useful for watching logs in realtime.) /dev/null will never return bytes. And so the command will sleep forever.

nitro2k01
  • 2,461
0

You can simply try the following:

echo "your commands" && while :; do sleep 1; done
user2783132
  • 1,967
0

You're trying to solve this from the wrong end. You don't want the shell not to exit, you want putty to leave the window open.

http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter4.html#config-closeonexit - describes exactly what you need to change. If you want to use the "only on clean exit" option, just add exit 1 at the end of the ForceCommand.

Edit: I misunderstood; you only want to maintain the forwarded ports, you don't want to keep the output of the ForceCommand. In that case, doesn't http://tartarus.org/~simon/putty-snapshots/htmldoc/Chapter4.html#config-ssh-noshell do what you want?

Gabe
  • 2,170