10

When I do putty -ssh user@host -pw password echo "hi"

I get a network error

But if I do putty -ssh user@host -pw password

An SSH session is successfully opened in a putty terminal. But I just want to run a single command. How do I do this in Putty?

030
  • 5,901
  • 13
  • 68
  • 110
wp-overwatch.com
  • 199
  • 1
  • 1
  • 7
  • 1
    You are trying to do things which are easier to accomplish on non-Windows operating systems. Consider whether you can use something other than Windows as your workstation OS. – Michael Hampton Dec 07 '16 at 08:56
  • Why do you want to use putty like this? It is much easier to use the terminal (both in linux and osx) to do this – Orphans Dec 07 '16 at 14:54
  • 1
    @MichaelHampton It's for an automated script that would be running on developer's window machines. – wp-overwatch.com Dec 07 '16 at 16:18

4 Answers4

19

You might want to use plink (which is shipped with putty) instead of putty for this.

Quote from the documentation:

Plink is a command-line connection tool similar to UNIX `ssh'. It is mostly used for automated operations, such as making CVS access a repository on a remote server.

For your example:

plink -ssh user@host -pw password echo "hi"
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
3

Per the putty manual, there is only -m which can be used to give it a file containing one or more commands to be executed in sequence.

ETL
  • 6,513
  • 1
  • 28
  • 48
  • `-m` does not execute a script. It reads a list of commands to execute remotely from a local file. This might be equivalent with simple scripts that only execute command after command, but when you add loops or operations on the output of commands this will stop working. Quote from [the documention](https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter3.html#S3.8.3.6): `With some servers [...] you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers.` – Gerald Schneider Dec 07 '16 at 09:12
  • @GeraldSchneider `script: computing: an automated series of instructions carried out in a specific order.` But yes... it's probably clearer to say _multiple commands in sequence_ – ETL Dec 07 '16 at 14:50
3

As @GeraldSchneider suggests, PLINK is more suitable, but if you really wanted to use PuTTY this is how you could do it:

First, you need to set a few things up as follows:

  1. Connect to the remote box via PuTTY, as in your example.
  2. While on the remote box, create a script to echo "hi". Name it hello.sh.
  3. Right-click the PuTTY window caption and select 'Change Settings...".
  4. Type the name my-saved-session and click Save then Cancel.
  5. Exit the session. Open the PuTTY Configuration panel again.
  6. Select your saved my-saved-session session and click Load.
  7. On the left of the 'Category' tree view, click 'SSH'.
  8. In the 'Remote command' box type /hello.sh -o.
  9. Click back on 'Session' and click Save.

Now you're good to run the command:

putty -ssh user@host -pw password -load my-saved-session

Cor
  • 51
  • 3
  • item 8: maybe _~/hello.sh -o_ instead of _/hello.sh -o_ : Need to check where hello.sh was saved. – mguima Sep 10 '21 at 12:21
0

If you have pterm (which usually ships with putty) AND you have a local ssh command line program, you can do the following

pterm -e "ssh [user]@[host] /path/to/your/program"

where /path/to/your/program could also be bash -c "some; shell; code"