List the IP address of list of servers

Hi

I have a file containing server names and i can ssh to all these servers without password.

Could any body suggest me how to list out IP address of all the servers?

Now i am manually doing this, like ssh to each server and run "ifcong -a" command and copy the ipaddress to a excel sheet.

can anybody advice a shell script to get servername and ipaddress side by side?

example output

server1    x.x.x.x
server2    x.x.x.x
server3    x.x.x.x
server4    x.x.x.x
-
-
-

like above....

Thanks
siva

man nslookup
1 Like

So you have list of hostname?
Try something like

while read SERVER
do
 ssh -q $SERVER "$COMMAND" | <further processing of the output if required> >> output.txt
done < hostname.lst

If you have specific usernames you want to login to, add them to hostname.lst

user1 server1
user2 server2

And use it like,

while read USERNAME SERVER
do
 ssh -q ${USERNAME}@$SERVER "$COMMAND" | <further processing of the output if required> >> output.txt
done < hostname.lst
for SERVER in `cat server-list`
do
ping -c 1 $SERVER | awk '{ print $2"\t"$3 }' | head -n 1 | tr -d '()'
done

Since the host names are resolving already for ssh, there is no need to remotely connect or ping the systems (that will only disclose currently alive servers, at best).
A query with nslookup as mentioned in post #2 or with dig, would be sufficient.

Here's an example using dig
Place all the server names in hostsfile.txt

dig -f hostsfile.txt +noall +answer | awk '{print $1, $NF}'
1 Like

Try the following:-

  • man host
  • man ping
  • man nslookup
  • view /etc/hosts

If using the hostname gets you connected, why do you need the IP address? I'd always steer people away from these as if the network requires changes (physical move, company merger etc.) you don't want to have to recode it all over the place if a DNS server can be a central reference where a single change will implement for all.

Robin