Search code examples
putty

Use PuTTY's -m parameter to run script on login


In the putty manual outlines how to use the program with command line parameters. I have this setup with a shortcut on my toolbar to automatically log me into my server.

"C:\Program Files\PuTTY\putty.exe" username@server -pw .... -C

However, I'd like to run a simple script to automatically put me in a specific directory once I log in. I saw there's the -m command to give "a local file name, and it will read a command from that file".

With a file at the destination I've tried the following options:

"C:\Program Files\PuTTY\putty.exe" username@server -pw .... -C -m "C:\local file\script.txt"

"C:\Program Files\PuTTY\putty.exe" username@server -pw .... -C -m "C:\local file\script.sh"

"C:\Program Files\PuTTY\putty.exe" username@server -pw .... -C -m "/server/file/with/755/permissions/script.sh"

And nothing worked.

I'm trying to find an example of this parameter and use but I can't find one. How can I properly use this parameter.


Solution

  • Quoting the docs,

    The -m option performs a similar function to the ‘Remote command’ box in the SSH panel of the PuTTY configuration box (see section 4.19.1). However, the -m option expects to be given a local file name, and it will read a command from that file.

    And quoting section 4.19.1,

    In SSH, you don't have to run a general shell session on the server. Instead, you can choose to run a single specific command (such as a mail user agent, for example). If you want to do this, enter the command in the ‘Remote command’ box.

    Note that most servers will close the session after executing the command.

    Note the "instead". This is not a command to run in the shell before passing control to you. This is a command to run instead of a shell.

    You can't just put cd /wherever and expect to end up in a shell in directory /wherever. You might be able to write a command that will run a shell in the directory you want and give you control, though I'm not sure how to get all the details correct.