How to extract IP address from Amazon hostname?

0

I really don't know why, but Amazon Web Services provided me with some weird hostnames for my machines. Now I need to list all them with knife to use in a bash script. I'm saying that the IPs are weird because none place you can resolve the hostname, which is useless though after all.

In my script, I would like to extract only the IP from the hostname. For example, extract 10.114.152.134 from the following hostname:

ip-10-114-152-134.valter.henrique.com

How can I do that?

Valter Silva

Posted 2013-05-11T08:17:10.617

Reputation: 1 331

2To clarify, ip-10-114-152-134.valter.henrique.com is the "weird hostname" Amazon gives you, and you want to extract the IP address from it? – Daniel Beck – 2013-05-11T08:25:22.397

Couldn't you just ping the hostname? – slhck – 2013-05-11T08:44:06.567

1@slhck If you're talking about getting the IP from a hostname via a DNS lookup, that's more for nslookup (or dig or host). – Bob – 2013-05-11T09:20:03.220

Answers

3

This snippet surely can be improved, but it should do the job:

hostname=ip-10-114-152-134.valter.henrique.com
hostip=$(echo ${hostname%%.*} | sed -e 's/ip-//' -e 's/-/./g')

${hostname%%.*} removes everything after (and including) the first .; sed then removes the starting ip- and replaces then the dashes with dots.


You can also use only one sed command:

echo ip-10-114-152-134.valter.henrique.com | sed 's/ip-\(.*\)-\(.*\)-\(.*\)-\(.*\)\.valter\.henrique\.com/\1.\2.\3.\4/'

The regex in the first brackets (you need to escape these: \(.*\) ) gets assigned to \1 and so on.


Here is the last variant, using only bash functions:

IFSsave="$IFS"; IFS=-                            # save IFS prior modifying it
hostip=""
hostname=ip-10-114-152-134.valter.henrique.com   # initial values
hostname=${hostname#*-}                          # remove the "ip-" part
hostname=${hostname%%.*}                         # remove the ".valter.henrique.com" part
for i in $hostname; do                           # loop over 10-114-152-134, splitted at "-" ($IFS)
   hostip="${hostip}${i}."                       # append number. to $hostip
done
hostip=${hostip%.}                               # remove trailing dot
echo $hostip                                     # print resulting IP
IFS="$IFSsave"                                   # restore IFS

mpy

Posted 2013-05-11T08:17:10.617

Reputation: 20 866

3

This is less robust than mpy's Bash-only variation since it assumes that the IP address portion always falls in the same position, but it's a little shorter and a little faster.

hostname=ip-10-114-152-134.valter.henrique.com
IFSsave=$IFS
IFS=.-
parts=($hostname)
hostip="${parts[*]:1:4}"
IFS=$IFSsave
echo "$hostip"

Using * as an array subscript inside quotes causes the first character of IFS to be inserted between each element of the array. Setting IFS to two characters causes the split to be done at either of them.

By the way, that domain name resolves like this for me:

$ host ip-10-114-152-134.valter.henrique.com
ip-10-114-152-134.valter.henrique.com has address 208.91.197.27

The address 10.114.152.134 is a private IP which is not routable over the internet. It's probably only reachable within the AWS network.

Paused until further notice.

Posted 2013-05-11T08:17:10.617

Reputation: 86 075

That's elegant - especially that parts[*] trick. But what is the parts=($d) doing, I mean, what is $d or should it read $hostname? – mpy – 2013-05-11T11:29:01.693

@mpy: It's a typo. I'll fix it. Thanks. I also realized that IFS only needs to be set once. – Paused until further notice. – 2013-05-11T11:32:30.420