博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

获得linux的IP地址信息

Posted on 2011-07-28 21:22  三块石头  阅读(781)  评论(0)    收藏  举报

这段shell脚本主要用来获得Linux的IP地址信息,包括IP、网关、子网掩码、广播地址以及DNS,参考了Eucalyptus中的eucalyptus-ipaddr.conf文件。

default_route=$(ip route show)
default_interface
=$(echo $default_route | sed -e 's/^.*dev \([^ ]*\).*$/\1/' | head -n 1)
address
=$(ip addr show label $default_interface scope global | awk '$1 == "inet" { print $2,$4}')


#ip address
ip=$(echo $address | awk '{print $1 }')
ip
=${ip%%/*}


#broadcast
broadcast=$(echo $address | awk '{print $2 }')

#mask address
mask=$(route -n |grep 'U[ \t]' | head -n 1 | awk '{print $3}')


#gateway address
gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}')

#dns
dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')


echo ip:
$ip,mask:$mask,broadcast:$broadcast,gateway:$gateway,dns:$dns