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 from the following hostname:


How can I do that?

  • 2
    To 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 May 11 '13 at 8:25
  • Couldn't you just ping the hostname? – slhck May 11 '13 at 8:44
  • 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 May 11 '13 at 9:20

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

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
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
hostip=${hostip%.}                               # remove trailing dot
echo $hostip                                     # print resulting IP
IFS="$IFSsave"                                   # restore IFS

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.

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

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

  • 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 May 11 '13 at 11:29
  • @mpy: It's a typo. I'll fix it. Thanks. I also realized that IFS only needs to be set once. – Dennis Williamson May 11 '13 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.