0

I have a little problem with executing putty(from command line) with -m argument. Please help me with this, or suggest a better way if you know one :)

The command(from "Run"):

putty -ssh user@machine -pw password -m C:\test.txt

"test.txt" command:

PROMPT_COMMAND='echo -ne "\033]0;'$( ifconfig | head -2 | tail -1 | awk '{print $2}')'\007"'

The problem:
It opens the session and then closes straight away

What does it suppose to do: Open ssh session from command line with defined title

Thanks.

igor
  • 473

2 Answers2

1

I believe you tried to do something similar as in this answer. But the thing is, the sample commands provided there are to be executed from the file on the server.

They cannot be passed as command(s) to execute using -m switch. This switch of PuTTY is to mimic the behavior of Linux command-line SSH client:

ssh user@foo command

This way ssh logs a user on the foo server, runs command there, and just exits. Looks like with -m PuTTY is doing exactly the same thing.

My suggestion

is to prepare a script on the remote server and configure PuTTY in a different way. First of all, you need to create a file with the commands you would like to run after logging in, let's call it .title

cat > ~/.title

Then just type, or copy and paste whatever you need there, press Ctrl+D afterwards. For now let's just test setting the title:

echo -ne "\033]0;PuTTY\007"

You can even try the above directly in terminal. If the window title of PuTTY is not changed, chances are your distribution already set default PROMPT_COMMAND variable. You can confirm that by executing the following commands in terminal

echo $PROMPT_COMMAND
unset PROMPT_COMMAND
echo -ne "\033]0;PuTTY test\007"

The key is to unset current value of PROMPT_COMMAND variable. Then the last command should set the window title to PuTTY test.

My solution

that works for me is to prepare a session in PuTTY, with "Remote command" box in GUI set to:

/bin/bash --rcfile ~/.title

Just remember to provide the name of this session (for example: SessionName) and press "Save" button on the first page of PuTTY settings, near "Saved Sessions" list :)

Then you can start PuTTY and tell it to load this session

putty -load SessionName -l user -pw password

Yes, it's sad, but there is no command-line equivalent of "Remote command" box for a saved session. That's why one has to go over all these hurdles :(

TL;DR version

Assuming that you do all these steps correctly, everything should work fine:

  • create ~/.title script on the remote machine
  • prepare a session with Remote command that runs Bash which will execute ~/.title
  • tell PuTTY to load prepared session upon start, specifying user and password

Pro tip

Instead of specifying passwords in clear text on command line (and possibly in scripts), you should learn to use SSH keys and run 'Pageant' (PuTTY authentication agent) in Windows tray.

0

I haven't got an ssh to test it on right now but http://www.derkeiler.com/Newsgroups/comp.security.ssh/2012-04/msg00005.html "If you just want the window to stay open for you too look at, put sleep 10 at the end of your script for a 10 second wait. If you want to use the shell, use exec /bin/bash at the end. Shai"

two other links that offer some insight.

PuTTY: Run a remote command after login & keep the shell running

How do I choose what shows up in PuTTY title bar from Linux?

Added by barlop

the following isn't using putty, it uses openssh in cygwin The a.a file could contain ls or commands to change the prompt. It's local.

$ ssh 127.0.0.1 "cat | bash" < a.a

related , see answer I just posted to - https://unix.stackexchange.com/questions/87405/how-can-i-execute-local-script-on-remote-machine-and-include-arguments/208952#208952

barlop
  • 25,198