0

I'm searching high and low for a simple script to just loop through a series of servers. I don't want to run additional commands automatically, but that's all I'm finding. I don't need to while. I just want to have it SSH to the server and allow me to do work on it, then when I exit it goes to the next one. I've done this before but it was on an old debian box and now I'm using ubuntu

#!/bin/bash for IP in `cat /home/bb/server.txt` do echo $IP ssh -p 2202 $IP done 

I've tried modifications of other scripts people post such as

#!/bin/bash servers="/home/bb/server.txt" while IFS= read -r server do echo "connecting ... $server" ssh -p 2202 "$server" done < "$servers" 

but this just spins through them all and doesn't let me work inside any of the servers.

Edit1: the first loop just echos the file location and not the server so its not opening the file to read the hostnames. additionally it comes back with Could not resolve hostname cat, which confirms it is not reading it.

Edit2: I am trying to avoid installing new software as that is frowned upon on these servers.

Edit3: As for my end goal I'll try to be more detailed. I have work to be done on a list of servers. The work is different on each, but they all have stuff that needs to be done. Instead of going off a list of hostnames (58 of them) and potentially missing one, having a script connect me to the next one is more convenient. I just need the script to ssh me to one server, then when I'm done and exit out via exit or ctrl+d it ssh to the next one

2
  • 1
    The first loop looks just fine. What exactly happens when you run it? The second loop doesn't work because you're trying to read your list of servers from stdin, but that's also where your ssh session is trying to read interactive commands. Commented Jul 5, 2023 at 21:53
  • Maybe a better question is to see what your end goal is. Are you trying to run commands on a remote server through ssh? It looks like you want some orchestration tool or something like: parallel-ssh.org. Maybe try ansible? It will save you a lot of time and head aches. Saltstack is another orchestration platform which will facilitate this. I know what you want and maybe you think passing -t to the ssh command will solve the issue but this is not the right approach imho. Commented Jul 6, 2023 at 6:39

2 Answers 2

2

As @larsks pointed out, read will mask the terminal by taking file descriptor 0. You can override this with a small change to the script.

#!/bin/bash servers=/home/bb/server.txt exec 3<"$servers" while IFS= read -u 3 -r server do echo "connecting ... $server" ssh -p 2022 "$server" done 
2
  • OMG that did it! It works and works exactly how I need it to work! Thank you! Commented Jul 6, 2023 at 14:27
  • You're welcome :-) Commented Jul 6, 2023 at 14:59
1

The first loop looks correct, assuming all your servers listen on port 2202 instead of 22.

Depending on your use case, I would recommend you look at tmux-cssh which allows you to work on multiple servers simultaneously. If you are going to be typing the same commands on all of them, it may fit your use case.

It looks like this when connecting to four servers...

tmux-cssh screenshot with four servers

1
  • Unfortunately I'm not typing the same commands on all of them. Though that might be fairly interesting to use for other projects. Thank you. Commented Jul 6, 2023 at 14:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.