Automating Running Command on Linux from Windows Using Putty

Automating running command on Linux from Windows using PuTTY

There could be security issues with common methods for auto-login.
One of the most easiest ways is documented below:

  • Running Putty from the Windows Command Line

And as for the part the executes the command
In putty UI, Connection>SSH> there's a field for remote command.

4.17 The SSH panel

The SSH panel allows you to configure
options that only apply to SSH

4.17.1 Executing a specific command on the server

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.

in short, your answers might just as well be similar to the text below:

  • let Putty run command in remote server

Automating command/script execution using PuTTY

PuTTY has the -m switch, that you can use to provide a path to a file with a list of commands to execute:

putty.exe -m c:\local\path\commands.txt

Where the commands.txt will, in your case, contain a path to your shell script, like:


Though for automation, your better use the Plink command-line connection tool, instead of the GUI PuTTY application, as you have already found out. The Plink is a part of PuTTY package, so everyone who has PuTTY should have Plink too.

The Plink (plink.exe) has the same command-line arguments as PuTTY. And in addition to those, you can specify your command directly on its command like:

plink.exe /home/user/

or using its standard input

plink.exe < c:\local\path\command.txt

(of course, you will use redirection mechanism of your language, instead of the <).

Note that providing a command using the -m switch or directly on command-line implies a non-interactive mode, while using the standard input uses an interactive mode by default. So the results or behavior may differ. Use the -t and -T switches to force the interactive and the non-interactive mode, respectively.

How to automate running Linux commands via `ssh` from Windows 7?

I found it myself without using Cygwin - I'm stuck with installing/reinstalling it.

The answer is using Plink from PuTTY.

rem Note
rem ----
rem 01 Update `plink` to point to `plink.exe` on `your PC`
rem 02 Update `pkey` to point to `private key` of `remote server`
rem (the end)

set plink="Path\To\PuTTY\plink.exe"
set user=<your user>
set server=<remote server>
set pkey="Path\To\YourPrivateSSHKey.ppk"
set pass=<your pass>

set run=%plink% -v -pw %pass% -i %pkey% %user%@%server%

rem sample run command
set cmd=svn update /var/www/MyWeb

%run% %cmd%
rem (the end)

Hope that helps

How to open a session in PuTTY and execute a command without using a file containing the command?

Not with PuTTY. But for automation, you should not use PuTTY anyway. That's a GUI application for an interactive use.

For automation, use Plink (PuTTY command-line connection tool). The Plink supports the same set of command-line arguments as PuTTY, but can also accept a command on its command line:

plink -ssh command

how to automate some linux commands

Yes. Once you know it, you'll find that scripting in Linux is a delight compared to Windows. Imagine the difference between (horrid) command prompt and (less horrid) PowerShell, then multiply that difference by ten, and that's how much nicer it is to script in Linux.

The term is "shell scripting", because the command processor (equivalent of Windows command prompt) is called the 'shell'. Just to make life a bit more fruity, there are various shells available for Linux. The most common are variants of sh such as bash and ash. Some crazy people use csh or tcsh or others, though, which have annoyingly different syntax.

Creating a script

To create a 'shell script', i.e. a batch file, just create a text file with this line at the top:


(or whatever the name of your shell is). Typically, the filename may end in .sh or there may be no special file ending at all.

In the remaining lines of the file just list the commands you would like to run, as if you were typing them in at a normal Linux terminal.

Then assign 'execute' permissions to that file. From the command line, you would use chmod u+x That means add eXexecute permissions for the current User.

The real fun comes when you start to learn all the different things you can do in the shell. For example there are neat tricks like this:

my-first-command && my-second-command   # only runs my-second-command if my-first-command succeeded

or this:

my-background-command &   # runs my-background-command in the background, so that you can get on with other things

There are also lots of terrific little Linux/UNIX programs which make it easy to connect other programs together - for example grep, uniq, xargs.

Your specific example

Having explained how great all that is, though, I don't think that's actually what you need to do.

The shell script part of your example boils down to:

rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv

I get the impression you want to log in from Windows automatically to run these commands.

I believe that in PuTTY, if it's connecting via SSH, you can give it a command to run. So here's what I'd do.

  1. Generate ssh keys on your Windows machine using PuTTY. (I can't remember how to do this - I think there's PuTTYGen or similar).
  2. Copy the public key that results into a file called authorized_keys within the .ssh directory of the psoftXXX user. You can literally copy and paste it; that's probably easier than doing anything fancy. Be aware that this directory and/or file may not exist, in which case you'll need to create them; if the file exists already then be sure to append the new key to the end of the file instead of overwriting it.
  3. Now try to connect again using PuTTY and ssh. It should automatically log in as the psoftXXX user.
  4. Finally, in the PuTTY settings you can probably specify the command-line given above. You might well need to specify it like this:

    /bin/bash -c "rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv"

Note that there's one stage I haven't automated, which is pressing 1 at the menu. That's because I suspect this menu is implemented by giving you a special default login shell which isn't /bin/bash but is instead /something/somewhere/which/shows/a/menu. I hope that if you specify an alternative command in PuTTY, that setting will be totally ignored and instead you'll get your script to run.

You might need to play around a bit. Good luck!

Batch file to auto login in PuTTY and and run a specific command

You can use below command :

C:\Program Files\PuTTY\putty.exe" -ssh msnoc@ -pw mypassword -P pwd -M "C:\Program Files\PuTTY\script.txt

where script.txt contain your commands:

sh isdn st | i Bs

Best way to script remote SSH commands in Batch (Windows)

The -m switch of PuTTY takes a path to a script file as an argument, not a command.


So you have to save your command (command_run) to a plain text file (e.g. c:\path\command.txt) and pass that to PuTTY:

putty.exe -ssh user@host -pw password -m c:\path\command.txt

Though note that you should use Plink (a command-line connection tool from PuTTY suite). It's a console application, so you can redirect its output to a file (what you cannot do with PuTTY).

A command-line syntax is identical, an output redirection added:

plink.exe -ssh user@host -pw password -m c:\path\command.txt > output.txt

See Using the command-line connection tool Plink.

And with Plink, you can actually provide the command directly on its command-line:

plink.exe -ssh user@host -pw password command > output.txt

Similar questions:

Automating running command on Linux from Windows using PuTTY

Executing command in Plink from a batch file

Related Topics

Leave a reply