Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111925
  • 博文数量: 24
  • 博客积分: 368
  • 博客等级: 准尉
  • 技术积分: 272
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-13 22:17
文章分类

全部博文(24)

文章存档

2011年(2)

2010年(22)

分类:

2010-03-27 22:58:37

问题:网络端口输出命令如下:,想知道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"
阅读(2969) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

安何2010-04-02 15:54:52

技术贴,没人踩,俺来踩