问题:网络端口输出命令如下:,想知道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"
阅读(3006) | 评论(1) | 转发(0) |