Samstag, 4. April 2015

Lan- & Global IP

As you probably know, a computer has at least 2 different IPs, at least when its connected to the internet.

Getting the internal (lan) IP adress, is no issue, we simply invoke ifconf and search the broadcast lines providing an IP adress.

For the external IP adress, one needs to parse a website which provides this information.
In this example i'm using this page: http://www.unix.com/what-is-my-ip.php

As first we load the page into a DATA variable, and prepare some of the html tags, as php will be interpreted and transformed to html.
How to get the actual value, depends upon the html you parse, and the hoster.

#!/usr/bin/env bash
# File:     myip.sh
# Description:    Simply prints internal and external IP using http://www.unix.com/what-is-my-ip.php
# GNU General Public License (GPL) 2015 by Simon Arjuna Erat (sea) (erat.simon@gmail.com)
# ------------------------------------------------------
#
#    Variables
#
    URL=http://www.unix.com/what-is-my-ip.php
    DATA=$(curl -s $URL) > /dev/zero
    str="DNS Lookup For"
#
#    Action & Display
#
    printf "%s\t%s\n" \
        "Internal" \
        "$(ifconfig | \
            grep -i broadcast | grep ^[[:space:]] | \
            awk '{ print $2}')"
    printf "%s\t%s\n" \
        "External" \
        "$(echo "$DATA" | \
            sed s,"$str","\n\n$str",g | sed s,"<"," ",g | \
            grep "$str" | awk '{print $4}')"

Keine Kommentare: