Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

Anyone done this before?

like image 629
leeman24 Avatar asked Jan 28 '15 16:01

leeman24


People also ask

Can I SSH through CMD?

You can start an SSH session in your command prompt by executing ssh user@machine and you will be prompted to enter your password. You can create a Windows Terminal profile that does this on startup by adding the commandline setting to a profile in your settings.

Can you run a script through SSH?

Try running ssh user@remote sh ./script. unx . This only works if the script is in the default (home) directory on the remote.

How run Linux commands remotely Windows?

Syntax for running commands on a remote Linux or Unix hostssh : The ssh (or other SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. USER-NAME : Remote host user name. REMOTE-HOST : Remote host ip-address or host name, such as fbsd.cyberciti.biz.


1 Answers

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

like image 104
Martin Prikryl Avatar answered Oct 04 '22 04:10

Martin Prikryl