Search code examples
batch-filesshcmdputtyplink

How to ssh hop through multiple hosts from a Windows batch file


How write a Windows batch file to be run from HostA for those connections below?

SSH (with local port forwarding): HostA => HostB => HostC.

HostA – HostB must use plink.exe (HostA runs Windows)
HostB – HostC must use ssh (HostB runs Linux).

I try this:

@echo off
chdir /d "C:\"
start plink.exe [email protected]_of_B -pw userBpasswd
ssh -L [email protected]_of_C

This batch script does not execute a second ssh command (from B to C), it just opens a new CMD window in which nothing happens.

Please disregard password storage in a batch file for plink.


Solution

  • You have to tell plink to execute the ssh, by passing the ssh command to plink commandline:

    start plink.exe -t [email protected]_of_B -pw userBpasswd ssh -L [email protected]_of_C
    

    When the command is provided on Plink command-line, Plink by default does not use a terminal emulation. To force the terminal emulation, the -t switch needs to be added (as shown above).


    Though:

    • Imo, your ssh command is wrong – With -L you need to specify an additional argument.
    • For SSH hop, you can also use a port forwarding, instead of executing ssh on the intermediate server.
      See How to create SSH tunnel using PuTTY in Windows?