running multiple ssh commands in a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

running multiple ssh commands in a batch file

#1 Post by aristosv » 19 Apr 2018 21:05

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 \
]

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: running multiple ssh commands in a batch file

#2 Post by Squashman » 19 Apr 2018 21:30

When you were reading the documentation for plink did you stumble upon the -m option?

aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Re: running multiple ssh commands in a batch file

#3 Post by aristosv » 19 Apr 2018 21:59

Yes. I don't want to use a separate file for the commands. I want to have them in the batch file.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: running multiple ssh commands in a batch file

#4 Post by siberia-man » 19 Apr 2018 22:29

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

aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Re: running multiple ssh commands in a batch file

#5 Post by aristosv » 20 Apr 2018 01:48

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?

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: running multiple ssh commands in a batch file

#6 Post by SIMMS7400 » 20 Apr 2018 02:06

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%

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: running multiple ssh commands in a batch file

#7 Post by siberia-man » 20 Apr 2018 02:06

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?

aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

Re: running multiple ssh commands in a batch file

#8 Post by aristosv » 20 Apr 2018 02:49

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 ^
)

Post Reply