40

I am looking to script something in batch which will need to run remote SSH commands on Linux. I would want the output returned so I can either display it on the screen or log it.

I tried putty.exe -ssh user@host -pw password -m command_run, but it doesn't return anything on my screen.

How can I get output?

2
  • 1
    Vote to reopen: Scripting is still programming and this question is 10 years old and attracted a lot of views. Commented Sep 14 at 11:02
  • 1
    @jeb Except the "scripting" just comes down to what the correct putty command line is (general use of a sysadmin tool, not the scripting itself). It's also a duplicate of quite some other questions and I don't see how its age or amount of views are relevant to re-open votes. But maybe you're right and your comment just fails to make that point. Commented Oct 17 at 5:41

3 Answers 3

48

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

Reference: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m

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

Sign up to request clarification or add additional context in comments.

Comments

5

You can also use Bash on Ubuntu on Windows directly. E.g.,

bash -c "ssh -t user@computer 'cd /; sudo my-command'"

Per Martin Prikryl's comment below:

The -t enables terminal emulation. Whether you need the terminal emulation for sudo depends on configuration (and by default you do no need it, while many distributions override the default). On the contrary, many other commands need terminal emulation.

Comments

3

As an alternative option you could install OpenSSH http://www.mls-software.com/opensshd.html and then simply ssh user@host -pw password -m command_run

Edit: After a response from user2687375 when installing, select client only. Once this is done you should be able to initiate SSH from command.

Then you can create an ssh batch script such as

ECHO OFF
CLS
:MENU
ECHO.
ECHO ........................
ECHO SSH servers
ECHO ........................
ECHO.
ECHO 1 - Web Server 1
ECHO 2 - Web Server 2
ECHO E - EXIT
ECHO.

SET /P M=Type 1 - 2 then press ENTER:
IF %M%==1 GOTO WEB1
IF %M%==2 GOTO WEB2
IF %M%==E GOTO EOF

REM ------------------------------
REM SSH Server details
REM ------------------------------

:WEB1
CLS
call ssh [email protected]
cmd /k

:WEB2
CLS
call ssh [email protected]
cmd /k

3 Comments

because You are suggesting ssh server, and OP want a way to execute remote command from bat script. Also, after installing program ou suggested, my antivir went crazy.
Awesome how would I do password?
I'd use -ssh user@host -pw password -m c:\path\command.txt as suggested above

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.