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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章