shell脚本中获取Linux系统ip地址的常见方式

分享几个获取本机服务器IP地址的脚本~

利用awk命令获取IP


1
[root@backup~]# ifconfig ens33 | awk 'NR==2{print $2}'192.168.154.5

  利用grep命令获取IP


1
[root@backup~]#ifconfig ens33 | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -n 1192.168.154.5

  利用sed命令获取IP


1
[root@backup~]#ifconfig ens33 | sed -n '/inet /p' | sed 's/inet \([0-9.]\+\).*/\1/' | tr -d ' '192.168.154.5

  获取所有网卡IP


1
[root@backup~]#cat ip.sh#!/bin/bash# Author: cn-Linuxerifs=(`ifconfig | grep "^e" | awk -F: '{print $1}'`)for i in `echo ${ifs[@]}`;doecho -e "${i}\n\t`ifconfig ${i} | awk 'NR==2{print $2}'`"done[root@backup~]#sh ip.shens33        192.168.154.5ens35        192.168.156.5