3

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 user_at_B@192.168.IP_of_B -pw userBpasswd
ssh -L user_at_C@192.168.IP_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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3013157
  • 109
  • 1
  • 9
  • You appear to be using `start` with `plink.exe`, which means that the command is started and immediately runs the next line, `ssh ...`, _(effectively both lines are being ran at the same time)_. I'm sure that is not what you want it to do, you'll either need to run one after the other, i.e. using `/Wait` or joining them as one command. The answer you've already received, chooses the latter. – Compo Jul 18 '19 at 07:18
  • @Compo `ssh` to work as OP intended, must be executed on the server, not locally. So it's was not my *choice*, imo it must be that way. – Martin Prikryl Jul 18 '19 at 07:32

1 Answers1

2

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

start plink.exe -t user_at_B@192.168.IP_of_B -pw userBpasswd ssh -L user_at_C@192.168.IP_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?
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • for `start plink.exe user_at_B@192.168.IP_of_B -pw userBpasswd ssh user_at_C@192.168.IP_of_C` I get "pseudo-terminal will not be allocated because stdin is not a terminal" error. P.S. -L is for port forwarding, which I use. – user3013157 Jul 22 '19 at 06:45
  • Why do you think, it's an error? That's for information only, isn't it? + I know what `-L` does. That's why I know that it needs an argument, right? – Martin Prikryl Jul 22 '19 at 06:50
  • yes yes :) so how do I make this terminal alocation message sorted? – user3013157 Jul 22 '19 at 06:53
  • Depends, on what you mean by *"sorted"*. If you want to get rid of the message, you can try adding `-T` switch to `ssh`. – Martin Prikryl Jul 22 '19 at 06:55
  • I mean `plink.exe userB@IPofB ssh userC@IPofC` does not work, stdin message is displayed and cmd Terminal closes :) oh and this message is shown too: "Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password,keyboard-interactive)." I am guessing this is related to ssh password request. – user3013157 Jul 22 '19 at 07:02
  • OK, so add `-t` switch to Plink (and do NOT add the `-T` to `ssh`) – Martin Prikryl Jul 22 '19 at 07:15
  • yes, got it working, thanks @Martin `plink.exe -t B@IP_B ssh C@IP_C` – user3013157 Jul 22 '19 at 07:27