shell转换IP

老大写的一个shell转换IP的 很好用 ,分享下!

#!/bin/bash
hexip=""
hextoip() {
    hex=$1
ip1=`printf "%d." 0x${hex:6:2}`
ip2=`printf "%d." 0x${hex:4:2}`
    ip3=`printf "%d." 0x${hex:2:2}`
ip4=`printf "%d" 0x${hex:0:2}`
ip=$ip1$ip2$ip3$ip4
hexip=$ip
#return $ip
}


iptohex() {
    IFS=.
    for str in $1
    do
        printf "%02X" $str
    done
}


iptoaddr ()
{
#exec < $1
#while read a
for a in `cat $1`
do
#sring=`curl -s  "http://ip138.com/ips138.asp?ip=${a}&action=2" |grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}'`
#sring=$(wget -q "http://www.ip138.com/ips.asp?ip=${a}&action=2" -O -| iconv -f gbk -t utf-8 | grep "数据" | awk -F"ul" '{print $3}' | awk -F"" '{print $1$2}' | awk -F"" '{print $2"\n"$3}' | head -1)
hex=`printf "%X" ${a}`
#echo $a $hex
hextoip $hex
#hexip=$?
#echo $a $hexip
sring=$(wget -q "http://ip138.com/ips138.asp?ip=${hexip}&action=2" -O -| grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}')
echo $a $hexip $sring
done
}


case $1 in
-f)
        shift
        iptoaddr $1
;;
-i)
        shift
        #sring=`curl -s  "http://ip138.com/ips138.asp?ip=${1}&action=2" |grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}'`
#sring=$(wget -q "http://ip138.com/ips138.asp?ip=${1}&action=2" -O -| iconv -f gbk -t utf-8 | grep "数据" | awk -F"ul" '{print $3}' | awk -F"" '{print $1$2}' | awk -F"" '{print $2"\n"$3}' | head -1)
#sring=$(wget -q "http://ip138.com/ips138.asp?ip=${1}&action=2" -O -| grep "数据" | awk -F"ul" '{print $3}' | awk -F"" '{print $1$2}' | awk -F"" '{print $2"\n"$3}' | head -1)
sring=$(wget -q "http://ip138.com/ips138.asp?ip=${1}&action=2" -O -| grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}')
        echo $1 $sring
;;
-h)
        shift
hex=`echo "ibase=10;obase=16;$1"|bc`
echo $hex
        hextoip $hex
;;
-x)
        shift
        iptohex $1
;;
*)
        echo "[Help]
        $0 need -f or -i
        -f ------- argument is a file
        -i ------- argument is a IP
[For example]:
        $0 -f filename
        $0 -i ipadress
        "
;;
esac


你可能感兴趣的:(shell转换IP)