Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
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
add comment

2 Answers

up vote 3 down vote accepted

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
share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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