Page 1 of 1

running multiple ssh commands in a batch file

Posted: 19 Apr 2018 21:05
by aristosv
Hi,

This works as a batch file.

Code: Select all

plink.exe -ssh user@<ip address> -P 22 -pw password ( pwd ; ls ;)
It executes the 2 commands and shows me the results. But what if I have to run 100 commands. I'll have to arrange them vertically. So I tried various things but I can't figure it out.

The following don't work, and I tried many other variations that I don't list here. Help?

Code: Select all

plink.exe -ssh user@<ip address> -P 22 -pw password (
pwd ;
ls ;
)

plink.exe -ssh user@<ip address> -P 22 -pw password {
pwd ;
ls ;
}

plink.exe -ssh user@<ip address> -P 22 -pw password [
pwd \
ls \
]

Re: running multiple ssh commands in a batch file

Posted: 19 Apr 2018 21:30
by Squashman
When you were reading the documentation for plink did you stumble upon the -m option?

Re: running multiple ssh commands in a batch file

Posted: 19 Apr 2018 21:59
by aristosv
Yes. I don't want to use a separate file for the commands. I want to have them in the batch file.

Re: running multiple ssh commands in a batch file

Posted: 19 Apr 2018 22:29
by siberia-man
You can try hybrid script of batch+bash

Code: Select all

: << '____CMD____'
@echo off
ssh "%~f0" %*
goto :eof
____CMD____

pwd
ls
or use the script cmdize.bat from viewtopic.php?f=3&t=5543&p=37780#p37780 which will produce the same code as above.

If neither of them work, you can try another two methods I suggested on this Russian forum here http://forum.script-coding.com/viewtopic.php?id=11535

Re: running multiple ssh commands in a batch file

Posted: 20 Apr 2018 01:48
by aristosv
I'm no batch file expert so I find it difficult to understand how this code works.

Code: Select all

: << '____CMD____'
@echo off
ssh "%~f0" %*
goto :eof
____CMD____

pwd
ls
Can you explain how it would work?

Re: running multiple ssh commands in a batch file

Posted: 20 Apr 2018 02:06
by SIMMS7400
I don't understand the reluctance to use the -m switch?

Why don't you manage the 100 commands in batch, but just spool to a commands file instead to make it simpler?

For instance:

Code: Select all

set SCRIPT_FILE=%TEMP%\commands.txt
echo cd /path > %SCRIPT_FILE%
echo /bin/bash > %SCRIPT_FILE%
..
..
..
..
..
..
etc
putty.exe username@example.com -pw password -t -m %SCRIPT_FILE%

Re: running multiple ssh commands in a batch file

Posted: 20 Apr 2018 02:06
by siberia-man
difficult to understand how this code works
This is tricky way to combine two scripts written in two different languages. We call it here "hybrid". Let me explain it step by step

Code: Select all

: << '____CMD____'
@echo off
ssh "%~f0" %*
goto :eof
____CMD____

pwd
ls
The major trick is that this script will be processed twice by the cmd.exe and bash/ssh. The script is constructed so the most of the commands are valid from perspective of batch and bash.

The first processor is cmd.exe. It parses the file and executes the first 4 lines of the file:
A.1. the first character is colon is interpreted as a label and does nothing
A.2. turn off echoing in batch
A.3. the real invocation of bash or ssh passing the same arguments
A.4. stop executing the script and the further parsing of this file

The second processor is bash or ssh invoked on the step A.3 above:
B.1. the first line is the empty command (the colon is interpreted as empty command) taking HEREDOC (the multiple line argument) which beginning and end are marked with the ____CMD____ string. This allows us to avoid the interpretation of batch commands.
B.2 So the rest of the file is assumed as the valid bash script.

As the conclusion. The first 5 lines are so called prolog or glue between two different worlds (batch and bash). What you need is to leave it "as is" and put the bash commands below it. One thing you could do is to replace "ssh" with the command you need "plink -m".

Could you let us know which one of three suggested options does work for you?

Re: running multiple ssh commands in a batch file

Posted: 20 Apr 2018 02:49
by aristosv
I had to find the line continuation character (^) and put it at the end of each line. That's it.

Code: Select all

plink.exe -ssh user@<ip> -P 22 -pw pass ( ^
ls; ^
pwd ^
)