4

I want putty to run a single command after login when its start up and then get control of the console.

Let's say every time I need to goto a specific directory to run some command there.
The command I execute will vary time to time but I need to go to that specific directory every time : lets say the directory is a/b/c/

So I need to run a

cd /a/b/c

command after every time i login.

currently I am using :

C:\Users\Administrator\Desktop\putty.exe [myhost] -l [user] -pw [password] 

As the Target value In putty.exe shortcut file. Which working fine.

But now I want to execute a

cd /a/b/c

command after login automatically, Is it possible?

I have tried as suggested Here . But its execute the command and then exit putty. I don't want the putty to exit after the command has been executed.

Saif
  • 225

3 Answers3

5

You need to write your command in a file first. Then you can use -m option of putty.exe:

putty.exe -ssh user@192.168.56.1 -P 22 -pw password -m C:\fileWithCommand

It depends on the system's OS which you want to access via SSH whether it is possible to write even a batch of commands in this file.

Above method can execute whole scripts but closes the session when terminated. If you want to have the output, download plink.exe:

plink.exe -ssh user@192.168.56.1 -P 22 -pw password echo test

This command prints test.

-P 22 is not necessary in the case your port is 22.

h0ch5tr4355
  • 1,003
2

For those who are having issue that putty exits after execution when we use -m, we need to give "-t" option at the end so that the session remains interactive.

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

So on windows, basically we can create a shortcut and in its property put the whole statement as above. In cmd.txt put the command to execute. You may have to give absolute path of putty.exe (example: C:\Program Files\PuTTY\putty.exe)

0

If -t option was not enough and putty terminal still closes try adding this to your cmd.txt file at the end

...starting code you want to run (for example: cd "../../web/")
/bin/bash