Hind Forsum
Hind Forsum

Reputation: 10497

How to request IPv4 address from a machine name?

In my linux cluster, I've got a bunch of hostnames, while my network management log shows ip addresses(they changes sometimes according to dns).

My question is: how to quickly find out the ipv4 of some hostname, without using "ping xxxxx" to get ip address and Ctrl+c to stop it. I wish to write a simple script like:

myCommand hostname1
myCommand hostname2
myCommand hostname3
myCommand hostname4
myCommand hostname5

Which will print hostname to ipaddress mapping for me(for hostname1-5)

So how to write this "myCommand" command or shell script? Thanks.

Upvotes: 0

Views: 584

Answers (2)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

Use host tool from bind-tools:

$ host -4 -t A stackoverflow.com
stackoverflow.com has address 151.101.129.69
stackoverflow.com has address 151.101.1.69
stackoverflow.com has address 151.101.193.69
stackoverflow.com has address 151.101.65.69

Or dig tool from the same package:

$ dig -4 -t A stackoverflow.com +short
151.101.129.69
151.101.1.69
151.101.193.69
151.101.65.69

Upvotes: 3

Harald Nordgren
Harald Nordgren

Reputation: 12381

#!/usr/bin/env bash

function get_ip {
    echo $1
    getent hosts $1 | awk '{ print $1 }' | sed 's/^/  /g'
    echo
}

Usage:

get_ip unix.stackexchange.com
get_ip hostname1
get_ip hostname2

Upvotes: 2

Related Questions