|
楼主 |
发表于 2003-5-10 19:50:18
|
显示全部楼层
我想出来个用perl处理的方法,请指教;)- javalee//home/javalee/lx>cat ipvt
- #!/bin/ksh
- #将十进制IP地址转换成二进制的脚本
- #begin
- (($# !=4)) && { print "\aExample:ipvt 192 168 0 1[Enter]" ; exit 1; }
- if [[ $1<224 && $2<256 ]]
- then
- integer -i10 var1=$1
- integer -i10 var2=$2
- integer -i10 var3=$3
- integer -i10 var4=$4
- typeset -i2 var1
- typeset -i2 var2
- typeset -i2 var3
- typeset -i2 var4
- varlen1=$(print $var1|sed 's/2#//')
- integer len1=${#varlen1}
- ((n1=8-$len1))
- v1=$(print $(perl -e "print 0 x $n1")$varlen1)
- varlen2=$(print $var2|sed 's/2#//')
- integer len2=${#varlen2}
- ((n2=8-$len2))
- v2=$(print $(perl -e "print 0 x $n2")$varlen2)
- varlen3=$(print $var3|sed 's/2#//')
- integer len3=${#varlen3}
- ((n3=8-$len3))
- v3=$(print $(perl -e "print 0 x $n3")$varlen3)
- varlen4=$(print $var4|sed 's/2#//')
- integer len4=${#varlen4}
- ((n4=8-$len4))
- v4=$(print $(perl -e "print 0 x $n4")$varlen4)
- print -n "IP Address:[$1.$2.$3.$4] "
- print "[$v1.$v2.$v3.$v4]"
- else
- print "Wrong number!\aInput again..."
- fi
- #over
复制代码 是不是很臃肿,我也觉得,不知道哪位兄弟替我瘦瘦身?;)
测试我当前上线的IP:
javalee//home/javalee/lx>ipvt 211 150 182 28
IP Address:[211.150.182.28] [11010011.10010110.10110110.00011100]
javalee//home/javalee/lx>
|
|