求8位16进制mask为点分十进制mask的脚本

3096阅读 1评论2010-03-27 long1225
分类:

问题:网络端口输出命令如下:,想知道netmask为多少,要求输出 255.255.255.0 这种格式的结果
 
 
$ ifconfig eth0
eth0: flags=1004843 mtu 1500 index 2
        inet 192.168.0.101 netmask ffffff00 broadcast 192.168.0.255
 
代码:
 
#!/bin/sh
## USAGE: mask network_interface 
## for example mask eth0
# fetch the hex_netmask
hex_netmask=`ifconfig $0|sed -n 2p|sed -n 's/^  *inet.\{17,25\}\([0-9a-f]\{8\}\) broadcast [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$/\1/gp'`

# Convert hex to be uppercase for bc to accept it.
#
IN=`echo hex_netmask | tr "[:lower:]" "[:upper:]" `
#
# Begin converting each octet
#
IN1=`echo $IN| sed 's/^\(..\).*/ibase=16;\1/'|bc`
IN2=`echo $IN| sed 's/^..\(..\).*/ibase=16;\1/'|bc`
IN3=`echo $IN| sed 's/^....\(..\).*/ibase=16;\1/'|bc`
IN4=`echo $IN| sed 's/^......\(..\)/ibase=16;\1/'|bc`
#
# Begin gathering info on the resulting IP.
#
echo "IP Address = $IN1.$IN2.$IN3.$IN4"
上一篇:sed与awk 65页
下一篇:2012果然是真的,看中国建造方舟的地点

文章评论