Linux 下获取通讯IP

#!/bin/sh
# filename: get_net.sh

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[ 	]' | head -n 1 | awk '{print $3}')
  
#gateway address
gateway=$(route -n | grep 'UG[ 	]' | 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

shell脚本获取linux下的活动网络信息,包括ip、broadcast、netmask、gateway以及dns的信息。

原文地址:https://www.cnblogs.com/iclk/p/6927120.html