-2

This is a very simple question today, for some reason when I start a batch file with the code:

ssh user@192.168.1.101

It loops infinitely, output:

enter image description here

Would really appreciate it if someone knows the answer to this, if not, I will also take some different software suggestions, my requirements are: It must be able to connect through SSH to a Linux server through Windows, It can not have the capability of storing passwords, It must be able to open multiple instances of SSH connections, and I must be able to input the password and commands in manually through a terminal.

Edit: Note I named my batch file SSH.bat

  • 6
    Do not name your batch `ssh.bat` – jeb Oct 08 '22 at 09:55
  • 2
    It is best to use in a batch file always the fully qualified file name of an executable which means in this case most likely `%SystemRoot%\System32\OpenSSH\ssh.exe` as in this case `cmd.exe` does not need to search for the file with name `ssh` in file system using the environment variables `PATH` and `PATHEXT` as explained in full details by my answer on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi Oct 08 '22 at 12:10
  • 2
    If the batch file should work on being processed by `%SystemRoot%\SysWOW64\cmd.exe` (32-bit version on 64-bit Windows), it would be necessary to use in the batch file the following lines: __1.__ `set SSHEXE=%SystemRoot%\System32\OpenSSH\ssh.exe` __2.__ `if not exist %SSHEXE% set SSHEXE=%SystemRoot%\Sysnative\OpenSSH\ssh.exe` __3.__ `%SSHEXE% user@192.168.1.101` The batch file will nevertheless not work on Windows computers on which OpenSSH is not installed at all by default. There would be additional lines needed to check that and inform the user of the batch file about missing SSH executable. – Mofi Oct 08 '22 at 12:14

2 Answers2

3

cmd executes in the following, relevant, order:

It checks working directory itself for a file with the name only (if no extension is specified). the system variable %pathext% displays the extensions and the order it will be searched.

If the file is not found locally, then it will move to the environment variables and try and find a command in %path%.

So, in your case, the ssh.bat file has called ssh.exe as ssh without extension and therefore found ssh.bat in the current directory and then keeps calling itself, as that is what you're effectively telling it to do.

two methods of fixing this; Name the file something other than system commands or simply fully use name, path and extensions in your batch file, so you can then have ssh.bat with C:\path\to\ssh.exe host:22 which will override checking local path as you implicitly specify the path to the executable.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

If a command loops infinitely, mostly it is caused by using the same name for the batch file than the name of the executable program. The batch file has precedence over the exe, so it starts itself

jeb
  • 78,592
  • 17
  • 171
  • 225