How to run a remote command in PuTTY after login & keep the shell running

puttyssh

What I'm trying to do: start a PuTTY session from the command line, login to remote machine and cd to provided directory.

putty.exe -agent -ssh some.host

That will open a session & login with my default login name & private key.

echo cd /some/remote/path/ > c:/stuff/cmd.txt
putty.exe -agent -ssh some.host -m 'c:/stuff/cmd.txt'

That will open a session, login, execute a command (cd in this case) and exit.

How do I open a session, login, cd and keep the session open?

Background: I use emacs under windows and often edit files on remote Unix machines using tramp & plink. I want to make a hotkey that opens a PuTTY session for that remote machine and chdirs to the directory of that file. Not a big deal on emacs side, but I'm stuck with PuTTY.

Best Answer

What the -m does is, that it makes PuTTY instruct the SSH server to start that command(s) INSTEAD of a shell. So once your command finishes, so does the session.

If you want to run the shell after the cd command, you need to add it explicitly to your cmd.txt, like:

cd /my/path ; /bin/bash

Also the -m implies "nopty"/non-interactive mode. To use an interactive shell you need to override that using the -t switch.

putty.exe -ssh example.com -m "c:\path\cmd.txt" -t

Alternatively use KiTTY with its -cmd switch, that does what you want (and does not need a temporary file).

Related Question