Search code examples
bashscriptinglogfileshellksh

Check IP address off a list using ksh/bash


I have a list (text file) with the following data:

app1 example1.google.com   
app2 example2.google.com  
dev1 device1.google.com  
cell1 iphone1.google.com

I want to check the ip address of the URLs/hostnames and update the text file with the gathered ip. Example:

app1 example1.google.com 192.168.1.10  
app2 example2.google.com 192.168.1.55  
dev1 device1.google.com 192.168.1.53  
cell1 iphone1.google.com 192.168.1.199

Solution

  • You can use dig to get the IP (but the domains must exist). Not tested for IPv6.

    #! /bin/bash
    while read name url ; do
        ip=$(dig -4 $url | grep '^[^;]' | grep -o '\([0-9]*[.:]\)\+[0-9.:]*$')
        printf '%s %s %s\n' "$name" "$url" "$ip"
    done < data.txt