2004 发表于 2018-8-29 09:04:15

shell脚本获得本机ip地址

#!/bin/bash  
#author:
  
#date:2015-6-13
  
#filename:ip.sh
  
#############################
  
ifconfig_cmd=`which ifconfig`
  
$ifconfig_cmd > ifconfig.me
  
###################################
  
function obtain_ip_information(){
  
if grep -q 'bond' ifconfig.me
  
then
  
bond_name=`cat ifconfig.me | grep bond | grep -v grep | awk '{print $1}'`
  
current_ip=`"${ifconfig_cmd}" "${bond_name}" | grep Bcast | awk '{print $2}' | awk -F ":" '{print $2}'`
  
current_netmask="255.255.255.0"
  
current_gateway=`echo ${current_ip} | cut -f 1-3 -d "." | sed 's/$/\.254/g'`
  
echo ${current_ip}
  
echo ${current_netmask}
  
echo ${current_gateway}
  
elif grep -q 'eth' ifconfig.me
  
then
  
current_ip=`${ifconfig_cmd} eth0 | grep Bcast | awk '{print $2}' | awk -F ":" '{print $2}'`
  
current_netmask="255.255.255.0"
  
current_gateway=`echo ${current_ip} | cut -f 1-3 -d "." | sed 's/$/\.254/g'`
  
echo ${current_ip}
  
echo ${current_netmask}
  
echo ${current_gateway}
  
else
  
echo '网卡非bond,非eth0,请手工检查'
  
exit 3
  
fi
  
}
  

  
obtain_ip_information


页: [1]
查看完整版本: shell脚本获得本机ip地址