Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently convert long int to dotted quad IP in bash [duplicate]

Tags:

bash

mysql

I have a lot of IP addresses in a large mysql DB stored as long int's. I need an efficient/quick way to convert them back to an IP within a BASH shell script ( or have mysql return the results as an IP?? ).

Note: specifically don't want to call perl, awk, or other 'language'.

like image 896
Mark Avatar asked Jul 11 '10 08:07

Mark


4 Answers

Since you asked for Bash:

INET_NTOA() { 
    local IFS=. num quad ip e
    num=$1
    for e in 3 2 1
    do
        (( quad = 256 ** e))
        (( ip[3-e] = num / quad ))
        (( num = num % quad ))
    done
    ip[3]=$num
    echo "${ip[*]}"
}

INET_ATON ()
{
    local IFS=. ip num e
    ip=($1)
    for e in 3 2 1
    do
        (( num += ip[3-e] * 256 ** e ))
    done
    (( num += ip[3] ))
    echo "$num"
}

Examples:

$ INET_ATON 10.2.1.255
167903743
$ INET_NTOA 167903743
10.2.1.255

Here is a version that will work in any of the Bourne-derived shells I tried including dash, ksh, several versions of Bash, BusyBox ash, zsh (with -y) and even the Heirloom Bourne Shell.

INET_NTOA() {
    num=$1
    ip=
    for e in 3 2 1
    do
        quad=`echo "256 ^ $e" | bc`
        if [ -n "$ip" ]
        then
            ip=$ip.
        fi
        ip=$ip`echo "$num / $quad" | bc`
        num=`echo "$num % $quad" | bc`
    done
    ip=$ip.$num
    echo "$ip"
}

INET_ATON ()
{
    num=0
    e=3
    saveIFS=$IFS
    IFS=.
    set -- $1
    IFS=$saveIFS
    for ip in "$@"
    do
        num=`echo "$num + $ip * 256 ^ $e" | bc`
        e=`echo "$e - 1" | bc`
    done
    echo "$num"
}
like image 57
Dennis Williamson Avatar answered Nov 03 '22 05:11

Dennis Williamson


See the INET_NTOA function, you can use that to convert the number to an IP on the MySQL server.

like image 26
Lukáš Lalinský Avatar answered Nov 03 '22 07:11

Lukáš Lalinský


INET_ATON()
  {
  local IFS=. ipStr

  ipStr=($1)
  echo $(($(($(($(($(($((${ipStr[0]} * 256)) + ${ipStr[1]})) * 256)) + ${ipStr[2]})) * 256)) + ${ipStr[3]}))
  }

INET_NTOA()
  {
  echo "$(($1 / 16777216)).$(($(($1 % 16777216)) / 65536)).$(($(($1 % 65536)) / 256)).$(($1 % 256))"
  }

subnetRange()
  {
  local addr=$(INET_ATON "$1") mask=$(INET_ATON "$2")

  echo "$(INET_NTOA $(($addr & $mask))) $(INET_NTOA $(($addr | $mask ^ 4294967295)))"
  }


like image 3
Daniel Podolsky Avatar answered Nov 03 '22 07:11

Daniel Podolsky


Suppose you have a field called ip_addr in a table called hosts

Then

select INET_NTOA(ip_addr) from hosts;

Would do the query and return dotted quad IPs in the result set.

like image 1
Jesse Dhillon Avatar answered Nov 03 '22 06:11

Jesse Dhillon