45

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.

3 Answers 3

43

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

0
1

You need both -t and a shell to keep the session running. When your script returns there's no shell to keep control of the session, so the server terminates the connection. You need to start tmux or a shell so the server doesn't terminate the connection when your commands stop running. But that introduces another complexity: how to show the results of your commands on the terminal.

The most naive solution I've found is to run a the commands and call a shell at the end (/bin/bash or which your host have available). This allows the results of the previous scripts to be still be read and drops you into an interactive shell. This has the limitations that you can't configure your shell session, since it would return immediately it runs whatever command you tell it to run at which point the server would terminate the session.

-1

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.

2
  • 1
    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. Apr 25, 2013 at 15:50
  • Sorry to necro-post on a +7 y. old post, but did you find any solution to do this? I am tired of starting Android Studio (and other programs) on remote PC manually too!
    – MahNas92
    Nov 24, 2020 at 10:11

You must log in to answer this question.

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