Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

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

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

share|improve this question
up vote 11 down vote accepted

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

share|improve this answer
I tried that (adding /bin/bash to cmd.txt) and that does not work. As far as I understand, bash starts in non-interactive mode and, having no code to execute, exits. Is there a way to start an interactive shell that way? – Artyom V. Kireev Apr 25 '13 at 16:20
Meanwhile, I tried to start mc. :-) It didn't, and provided some output: Cannot get terminal settings: Invalid argument (22) TERM environment variable needs set. – Artyom V. Kireev Apr 25 '13 at 16:21
Works now! Thank you! – Artyom V. Kireev Apr 25 '13 at 16:47
Thanks for the guideline here, but I seem to have an issue trying to use putty -m "" -t to connect to a tinycorelinux vm (/bin/ash). I will get following error: (my script starts with echo ".." it will always show the first letter in my script after the illegal characters – Vincent De Smet Sep 24 '14 at 6:00
if I use configure a remote command in the putty profile, it works – Vincent De Smet Sep 24 '14 at 6:02

The tramp session you're starting in emacs and the shell session you run via putty are completely separate processes.

You're probably better off checking configuration options for tramp to see if there's a way to set a default directory per host within your emacs configuration. Maybe start with the tramp-remote-path configuration option as documented here.

If you need to use putty to establish the connection, you probably want to set up an ssh tunnel and have emacs/tramp connect through that.

share|improve this answer
Emacs & tramp are irrelevant here, I added that just for the background. That's not about editing file in emacs, that's about opening a shell to do something else with that file (e.g. run a script). I'm tired of "start putty, choose you host, then cd to the path you need", and want to automate that. – Artyom V. Kireev Apr 25 '13 at 15:50
Ok, thanks for the clarification. – Doug Harris Apr 25 '13 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .