linux - How to extract IP address from Amazon hostname?

09
2013-08
  • Valter Henrique

    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?

  • Answers
  • mpy

    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
    
  • Dennis Williamson

    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.


  • Related Question

    backup - Are whole VM images backed up on Amazon EC2/S3?
  • John

    I've been trying to get my head around Amazon Web Services as a VPS provider. My understanding is a EC2 instance running Windows is basically a Windows VM, very similar to renting a VPS from a more traditional hosting provider.

    I don't want to have complex backups, either to administer or to restore - if my restore involves installing SVN, MySQL, Jira, etc on a new box before I can even try to restore the backup then it's not great to me.

    What I really want is a service which backs up my entire VM... if the PC running the VPS dies then the VM image is installed on a new PC and off we go again.

    With Amazon being all about flexibility and elasticity, I wondered if they have this service? I can't figure it out from reading their docs.


  • Related Answers
  • Tomas Markauskas

    If you use EBS volumes then you don't can create backups via snapshots which is very easy to do (just a single API call or via the aws console). You can also use an EBS-based windows AMI so the whole system would be stored on EBS.