Une adresse IP est representée par 4 octets, séparées par un point. Chacun de ces octets représentent un nombre décimale, c'est la forme la plus répandue : 123.45.56.78. Nous allons voir ici qu'elle peut s'exprimer autrement.
0x01. PRELIMINAIRES
Nous allons écrire quelques petites fonctions qui seront toujours utiles.
0x02. INT2HEX
#!/usr/bin/env python import sys txt="" def usage(): print "\nUsage: %s <integer> [int2] [...]" for arg in sys.argv[1:]: try: res = hex( int(arg) ).replace("0x","") if( len(res) < 2 ): res = "0" + res print res, except: usage
0x03. HEX2INT
#!/usr/bin/env python import sys txt="" def usage(): print "\nUsage: %s <hex number> [hex2] [...]" for arg in sys.argv[1:]: try: res = int( '0x'+arg , 0 ) print res except: usage
0x04. CONVERSIONS
Nous allons maintenant entrer dans les fonctions de conversion d'adresse IP.
0x05. IP2HEX
#!/bin/sh if [ $# -lt 1 -o $# -gt 3 ]; then echo " Usage: $(basename $0) <192.168.1.2> [-0|-x|-r] default : c0.a8.01.02 -0 : 0xc0,0xa8,0x01,0x02 -x : \\xc0\\xa8\\x01\\x02 -r : c0a80102 " exit fi ip="$1" fmt="-d" ; [ $# -eq 2 ] && fmt="$2" o1=$(int2hex $(echo $ip|cut -d'.' -f1)|cut -d'x' -f2) o2=$(int2hex $(echo $ip|cut -d'.' -f2)|cut -d'x' -f2) o3=$(int2hex $(echo $ip|cut -d'.' -f3)|cut -d'x' -f2) o4=$(int2hex $(echo $ip|cut -d'.' -f4)|cut -d'x' -f2) [ ${#o1} -lt 2 ] && o1=0$o1 [ ${#o2} -lt 2 ] && o2=0$o2 [ ${#o3} -lt 2 ] && o3=0$o3 [ ${#o4} -lt 2 ] && o4=0$o4 if [ "$fmt" = "-0" ]; then hex_ip="0x$o1,0x$o2,0x$o3,0x$o4" elif [ "$fmt" = "-x" ]; then hex_ip="\\x$o1\\x$o2\\x$o3\\x$o4" elif [ "$fmt" = "-r" ]; then hex_ip="$o1$o2$o3$o4" else hex_ip="$o1.$o2.$o3.$o4" fi echo $hex_ip
# En sortie: # ---------- $ ip2hex 192.44.51.69 c0.2c.33.45 $ ip2hex 192.44.51.69 -0 0xc0,0x2c,0x33,0x45 $ ip2hex 192.44.51.69 -x \xc0\x2c\x33\x45 $ ip2hex 192.44.51.69 -r c02c3345
0x06. HEX2IP
#!/bin/sh if [ $# -ne 1 ]; then echo " Usage: $(basename $0) <c0.a8.1.2> Out .: 192.168.1.2 " exit fi ip="$1" o1=$(hex2int 0x$(echo $ip|cut -d'.' -f1)) o2=$(hex2int 0x$(echo $ip|cut -d'.' -f2)) o3=$(hex2int 0x$(echo $ip|cut -d'.' -f3)) o4=$(hex2int 0x$(echo $ip|cut -d'.' -f4)) int_ip="$o1.$o2.$o3.$o4" echo $int_ip
# En sortie: # ---------- $ hex2ip c0.a2.1.fe 192.162.1.254
0x07. IP2LONG
APP="ip2long" if [ $# -eq 1 -o $# -eq 2 ] ; then ip=$1 #ip1=$(echo $ip | cut -d'.' -f1) #ip2=$(echo $ip | cut -d'.' -f2) #ip3=$(echo $ip | cut -d'.' -f3) #ip4=$(echo $ip | cut -d'.' -f4) else echo " Usage: $APP <IP> Exemple: $APP 127.0.0.1 127.0.0.1 -> 2130706434 " exit 0 fi # Bash # ------------------------ #r1=$(($ip1*2**24)) #r2=$(($ip2*2**16)) #r3=$(($ip3*2**8)) #r4=$(($ip4*2)) #r=$((r1+r2+r3+r4)) # Perl # ------------------------ r=$(echo $ip | perl -ne 's/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1<<24|$2<<16|$3<<8|$4/e;print') # | grep -oE "(([0-9]){1,3}\.){3}([0-9]){1,3}") echo $r
# En sortie: # ---------- $ ip2long 177.84.14.3 2975075843
0x08. LONG2IP
#!/bin/bash APP="long2ip" if [ $# -eq 1 -o $# -eq 2 ] ; then int=$1 else echo " Usage: $APP <IP> [-v] Exemple: $APP 2130706434 2130706434 -> 127.0.0.1 -v : mise en forme " exit 0 fi V=0 [ $# -eq 2 ] && [ "$2" = "-v" ] && V=1 # Bash # ----------------------------------------------- #ip1=$(($int/2**24)) #ip2=$((($int - $ip1*2**24) / 2**16)) #ip3=$((($int - $ip1*2**24 - $ip2*2**16) / 2**8)) #ip4=$((($int - $ip1*2**24 - $ip2*2**16 - $ip3*2**8) / 2)) #r="$ip1.$ip2.$ip3.$ip4" # Perl # ----------------------------------------------- #r=$(echo $int | perl -ne 'print $_>>24 ,".",$_<<8>>24,".",$_<<16>>24,".",$_<<24>>24,"\n"') # Ping # ----------------------------------------------- #r=$(ping -r -m 0 -c 1 -W 0 $int 2>&1 |grep PING|cut -d'(' -f2|cut -d')' -f1) # Python # ----------------------------------------------- r=$(python -c "print str(($int & 0xFF000000) >> 24)+'.'+str(($int & 0x00FF0000) >> 16)+'.'+str(($int & 0x0000FF00) >> 8)+'.'+str(($int & 0x000000FF))") #echo -e "\e[1;29m $int\e[0m" '->' "\e[1;32m$ip1.$ip2.$ip3.$ip4\e[0m" [ $V -eq 1 ] && echo -e "\n\e[1;29m $int\e[0m\t" '>' "\t\e[1;32m$r\e[0m\n" [ $V -eq 0 ] && echo "$r"
# En sortie: # ---------- $ long2ip 2112687434 125.237.13.74
0x09. CHECK UP
ip2long $(long2ip 2975075843) # Doit retourner : 2975075843 long2ip $(ip2long 20.212.193.24) # Doit retourner : 20.212.193.24 ip2hex $(hex2ip c0.a2.01.fe) # Doit retourner : c0.a2.01.fe ip2hex $(long2ip $(ip2long $(hex2ip c0.a2.1.fe))) # Doit retourner : c0.a2.1.fe hex2ip $(ip2hex $(long2ip $(ip2long 123.54.99.1))) # Doit retourner : 123.54.99.1 ip2long $(hex2ip $(ip2hex $(long2ip 2975075843))) # Doit retourner : 2975075843
=> Écrit par : Nicolas, le 01 avril 2016