I've a script that ssh's onto other vms and runs a command.
Working code is as follows:
declare -a envname=("hostname1" "hostname2")
# Looping through array
for i in "${envname[@]}"
do
# loging on to environments, running a df command
ssh "$i" "df -h"
done
This works perfectly, but the code will get messy as more hostnames are added to the list. As such, I want a .txt file to replace the array. The textfile looks like this:
hostname1
hostname2
hostname3
Here's my attempt with a .txt file:
sed '/^[ \t\r\n]*$/d' hosts.txt | while read hosts;
do
echo "$hosts"
ssh "$hosts" "df -h"
done
The "echo$hosts" prints each hostname which looks fine, but I receive the error on the next line of code:
"name or service not knownname "hostname""
I assume this is some sort of whitespace/linespace issue as the hostnames look fine in the previous command. The sed '/^[ \t\r\n]*$/d
line was my attempt to remove whitespace, but this isn't working.