0

I want a bash script to start multiple SSH connections to multiple linux servers in multiple new bash windows, not for further processing, just to connect with a simple command instead of having to type the full ssh-lines every time I restart the bash (using WSL2/WSLg).

Opening new terminals and executing commands in them is no issue at all. Just running ssh (openSSH) is a problem in all three cases, since all throw the same error: 'ad port ' 12345

Executing it manually in bash I do invoke ssh like this: ssh user@ip.adress.de -p 12345

So I assume, like with every command I can just script it in following way:

#!/bin/bash
ssh usera@ip.adress.de -p 12345
ssh userb@ip.adress.eu -p 67890
ssh userc@ip.adress.uk -p 54321

Is this a script related issue, or might this be a WSL problem?

EDIT: I want to point out that my focus right now is mainly on the 'ad port ' error that I get. I have no idea what this is and I can't find anything useful about it on the Inet.

EDIT2: This is only an oversimplification of the code, since I am not allowed to share any details of the whole script. I know, that this code will just sequentially execute the ssh's but that's for testing only anyways. I just want to see the three connections being established in order (the logins are handled by RSA keypairs).

The whole script was written using NANO or VIM on a Proxmox->Debian server (virtual Debian running on Proxmox). So the CRLF line endings are sadly not the issue. Thanks for pointing this out though!

The issue wasn't solved directly but it could be adressed with another approach - using tmux - as suggested by @NotTheDr01ds !

4
  • Are the three ports correct for the three servers? Try also using ssh -f.
    – harrymc
    Apr 1 at 11:03
  • These run sequentially rather than in parallel, and they don't open separate windows. If you want these to be interactive, you need to add something to the front to open the commands in separate windows.
    – user10489
    Apr 1 at 11:37
  • @harrymc Yes, all ports are correct. I said that manually invoking ssh is no problem. Just the script does not work well with these commands. Apr 3 at 7:59
  • @user10489 I know that this does not open separate windows. My code fragment up there is a massive oversimplification of what the script actually does. There is much more to it, for example all three of the ssh's are executed in a loop which opens separate shell instances for them and makes sure the necessary encryption is handled. I am not allowed to share any details about the script itself, so I just posted the critical part of it - the ssh commands themselfs. The adresses and ports are obfuscated as well (obviously). Apr 3 at 8:02

1 Answer 1

2

This is most likely caused by creating or editing the file in a Windows editor that saved it with DOS/Windows CRLF line endings. I get the same error if I create your script using notepad.exe. Notepad can now save in Unix/Linux format, but it doesn't by default for new files unless you change that option manually.

In general, always edit files inside WSL using Linux tools.

in multiple new bash windows

Your script doesn't attempt to address this yet. The current script, as mentioned in the comments, is going to run these sequentially in a single bash instance (the subshell of the script's shebang line itself).

If you want to launch each into a separate "window", you'll need some tool that provides that functionality. The wsl.exe command itself isn't a great candidate for this, since by default, it's going to execute a new instance inside the current terminal. It's possible to use PowerShell (even from within WSL) with the Start-Process cmdlet to launch a new WSL window/terminal:

From WSL:

powershell.exe -c Start-Process wsl.exe

That could be combined with wsl.exe's -e/--execute argument to launch the ssh commands:

#!/bin/bash
powershell.exe -c 'Start-Process "wsl.exe" -ArgumentList "ssh usera@ip.adress.de -p 12345"'
powershell.exe -c 'Start-Process "wsl.exe" -ArgumentList "ssh userb@ip.adress.eu -p 67890"'
powershell.exe -c 'Start-Process "wsl.exe" -ArgumentList "ssh userc@ip.adress.uk -p 54321"'

But I'd recommend using Tmux for this. It's installed by default in the WSL Ubuntu distribution:

#!/bin/bash
tmux new-session sh -c "ssh usera@ip.adress.de -p 12345; exec bash" \; new-window sh -c "ssh userb@ip.adress.eu -p 67890; exec bash" \; new-window sh -c "ssh userc@ip.adress.uk -p 54321; exec bash"

That will run your ssh commands in 3 different Tmux windows, and then start a bash shell in each so that you can view the results. If you don't know Tmux already, you'll need to learn a few hotkeys in order to be able to switch windows, etc.

There are, of course, other ways of doing this as well.

4
  • Since the script is going to be deployed as a part of a whole software package inside a docker container, I will concider adding tmux to the list! Apr 3 at 8:21
  • This is only an oversimplification of the code, since I am not allowed to share any details of the whole script. I know, that this code will just sequentially execute the ssh's but that's for testing only anyways. I just want to see the three connections being established in sequential order (the logins are handled by RSA keypairs). The whole script was written using NANO or VIM on a Proxmox->Debian server (virtual Debian running on Proxmox). So the CRLF line endings are sadly not the issue. Using tmux solves the issue though. Thanks for pointing this out! Apr 3 at 8:27
  • 1
    @73CHN0M4NC3R Hmm - Running it through tmux fixed the 'ad port' message? Good to hear that it's working for you now, but I'm not sure that it was Tmux specifically that fixed that problem. Taking a guess here, but I think there was still some bad character in the script, even if it wasn't from a Windows tool -- Maybe copy/pasted in from something? It sounds like in the process of editing the file to add in Tmux, it got rid of the offending character, but not sure, of course. Apr 3 at 18:00
  • @NotTheDro1ds I sadly can't exactly say what my colleagues did there on that script, since I've never did bash scripting before (and seeing these hundreds of lines of - for me as a Python/Java amateur dev - barely readable code kinda intimidated me tbh :) But it is working now and I added tmux to our docker container. It could come in handy for a couple of other things. Again, thanks for the tip ^^ Apr 4 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.