IP的字符串和整型格式转换
时间:2009-04-30 来源:tessykandy
#!/bin/sh usage="Usage `basename $0` [option] [values]\n -s: 167772161 => 10.0.0.1\n -l: 10.0.0.1 => 167772161\n -h or -? : for help\n\n For example: convert 10.0.0.1 to 167772161,input:\n ./`basename $0` -l 10.0.0.1\n" LongToStr(){ a=`echo $1|cut -d\. -f1` b=`echo $1|cut -d\. -f2` c=`echo $1|cut -d\. -f3` d=`echo $1|cut -d\. -f4` itip=`expr $a \* 256 \* 256 \* 256 + $b \* 256 \* 256 + $c \* 256 + $d ` echo $1 "======>" $itip } StrToLong() { itip=$1 a=$(($itip/256/256/256)) b=$(($itip/256/256-$a*256)) c=$(($itip/256-$a*256*256-$b*256)); d=$(($itip-a*256*256*256-$b*256*256-$c*256)) echo $1 "=======>" $a"."$b"."$c"."$d } if [ $# != 2 ];then echo -e $usage exit fi if [ "$1" == "-l" ];then LongToStr $2 elif [ "$1" == "-s" ];then StrToLong $2 else echo -e $usage exit 1 fi |
相关阅读 更多 +