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.

3
  • Assuming the remote system is unix-y and your shell is bash, add the cd command to your ~/.bash_profile Commented Oct 21, 2015 at 12:46
  • little complex for me.? can you explain please and where is the ~/.bash_profile ?
    – Saif
    Commented Oct 21, 2015 at 12:48
  • Before that, are my assumptions correct? Commented Oct 21, 2015 at 12:57

3 Answers 3

5

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

putty.exe -ssh [email protected] -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 [email protected] -P 22 -pw password echo test

This command prints test.

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

4
  • yes tried that. That exit putty after execution of that command . any alternative.?
    – Saif
    Commented Oct 21, 2015 at 12:16
  • Thanks for your nice effort. I ran your command from command prompt. yes it does print test. But i don't want just the output. I also want to use the command interface like regular, which means executing some command by my self after that command execute. I am doing it right ? or It is possible vai putty.? or Do i have to combine putty.exe and plink.exe in some way which i dont know.? thanks again.
    – Saif
    Commented Oct 21, 2015 at 12:45
  • if we wanted to send some arguments to the shell script that run inside the putty? @h0ch5tr4355 Commented Jun 19, 2020 at 13:34
  • 1
    For those who are having issue that putty exits after execution, we need to give "-t" option at the end so that the session remains interactive. Commented Aug 20, 2021 at 6:01
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
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Aug 3 at 10:09

You must log in to answer this question.

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