0

I have a script that I am trying to run. The purpose of this script is to run single or multiple commands on list of servers. When running it, the script is not able to parse the hosts file and gives an error:

ssh: Could not resolve host name nodename nor servname provided, or not known

while read host;
do
echo server: $host
sshpass -p 'password'   ssh -o "StrictHostKeyChecking no" admin@$host  'command'
done < /path/to/hosts.txt

hosts.txt contains list of IP addresses of the hosts that need to connect in the following format:

server 1
server 2
server 3

I have tried putting the server IPs in "" and '' and ; and ,, but they all give the same error.

Can someone explain what I am doing wrong here?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Bi0n1C
  • 1
  • Make sure your system can resolve the hostnames. No ssh problem. For your next problem: Add option `-n` to your ssh command. – Cyrus Oct 07 '19 at 16:24
  • i can log into each hosts via ssh from my system and can resolve to hostname perfectly fine, but when i enter list of server IPs, the script is only able to do the first ip and run the command on the first IP on the list but does not parse the rest of the list. – Bi0n1C Oct 07 '19 at 16:29
  • 2
    You appear to have posted a redacted pseudo-code version of your script along with dummy data and censored output. This makes it hard to debug. Can you please create a new script that doesn't have any code or data you need to hide, then run it and post the complete, unmodified script and all its output? – that other guy Oct 07 '19 at 16:30
  • Check hosts.txt for special characters (DOS/Windows line breaks): `cat -A hosts.txt` – Cyrus Oct 07 '19 at 16:37
  • @Cyrus no special characters are in the hosts file. for some reason, it reads the first host and executes the command on the first host and then stops reading the rest of the hosts. – Bi0n1C Oct 07 '19 at 16:57
  • What is with your `ssh: Could not resolve host name nodename nor servname provided, or not known` problem? – Cyrus Oct 07 '19 at 17:00
  • so when i make a list of IP's only on each line in hosts.txt and run the script, it gives me that error. but when i put the hostname and then ip on each line, it reads the first server and runs the command and does not run the next server but it doesnt give me the above error. – Bi0n1C Oct 07 '19 at 17:07
  • Please follow [that other guy](https://stackoverflow.com/questions/58273400/shell-script-not-able-to-parse-host-list-and-host-unknown?noredirect=1#comment102913994_58273400)'s instructions. – Cyrus Oct 07 '19 at 17:09
  • Part of the problem may be due to `ssh` reading stdin, thus bogarting the file list. See ["Shell script while read line loop stops after the first line"](https://stackoverflow.com/questions/13800225/shell-script-while-read-line-loop-stops-after-the-first-line) and [BashFAQ #89: "I'm reading a file line by line and running ssh or ffmpeg, only the first line gets processed!"](https://mywiki.wooledge.org/BashFAQ/089). – Gordon Davisson Oct 07 '19 at 17:51
  • @GordonDavisson: or see my first comment. – Cyrus Oct 07 '19 at 18:57
  • @Cyrus and @Gordon Davisson thank you `-n` resolved the issue. such a simple fix i am surprised i didnt see that post here before despite of lot of searching. guess i wasnt wording it the right way. Thank all for the input. – Bi0n1C Oct 07 '19 at 22:23

2 Answers2

0

Please check if $host variable is getting parsed properly. Also, in your hosts.txt, there are two entries in each row. What does it signify?

I have created a sample script to parse space-separated arguments

Input file: hosts.txt

enter image description here

Script output:

enter image description here

#!bin/sh
while IFS=' ', read hostname ipaddress;
do
echo server: $ipaddress
done < hosts.txt
  • I tried your script and it works to echo the results and then i added what i actually wanted to do with the script with the command for multiple servers and for some reason, it reads the first host and executes the command on the first host and then stops reading the rest of the hosts. – Bi0n1C Oct 07 '19 at 16:57
  • i tried doing this and its working but not when i tell it to parse the list ```hosts="ip1 ip2 ip3 ip4" for HOSTNAME in ${hosts}; do``` – Bi0n1C Oct 07 '19 at 16:59
0

@Cyrus and @Gordon Davisson thank you -n resolved the issue. such a simple fix i am surprised i didnt see that post here before despite of lot of searching. guess i wasnt wording it the right way. Thank all for the input.

Bi0n1C
  • 1