Score:0

simple ssh loop without running extra commands

ms flag

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

pt flag
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.
Valentin Bajrami avatar
br flag
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: https://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.
Score:2
cn flag

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
MilkOverflow avatar
ms flag
OMG that did it! It works and works exactly how I need it to work! Thank you!
Gerrit avatar
cn flag
You're welcome :-)
Score:1
cn flag

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

MilkOverflow avatar
ms flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.