Reputation: 23
I want to extract IP from Hostname or FQDN using a shell script. If I Ping using the hostname it gives me the IP in the output but how do I extract that from the output to use in my shell script.
Upvotes: 0
Views: 7306
Reputation: 23
This worked for me:
IP1=$(ping -c 1 "$IP" | grep PING | awk -F'(' '{print $2}'| awk -F')' '{print $1}') &> /dev/null
IP being the FQDN
Upvotes: 0
Reputation: 171
ip=`nslookup <fqdn> | grep -m2 Address | tail -n1 | cut -d : -f 2`
eg.
$ ip=`nslookup stackoverflow.com | grep -m2 Address | tail -n1 | cut -d : -f 2` && echo $ip
151.101.1.69
Upvotes: 0