shell脚本中获取Linux系统ip地址的常见方式
分享几个获取本机服务器IP地址的脚本~
利用awk命令获取IP
[root@backup~]# ifconfig ens33 | awk NR==2{print $2} 192.168.154.5
利用grep命令获取IP
[root@backup~]#ifconfig ens33 | egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}" | head -n 1 192.168.154.5
利用sed命令获取IP
[root@backup~]#ifconfig ens33 | sed -n /inet /p | sed s/inet ([0-9.]+).*/1/ | tr -d 192.168.154.5
获取所有网卡IP
[root@backup~]#cat ip.sh #!/bin/bash # Author: cn-Linuxer ifs=(`ifconfig | grep "^e" | awk -F: {print $1}`) for i in `echo ${ifs[@]}`;do echo -e "${i} `ifconfig ${i} | awk NR==2{print $2}`" done [root@backup~]#sh ip.sh ens33 192.168.154.5 ens35 192.168.156.5
Linux学习指南 有收获,点个在看分享几个获取本机服务器IP地址的脚本~ 利用awk命令获取IP [root@backup~]# ifconfig ens33 | awk NR==2{print $2} 192.168.154.5 利用grep命令获取IP [root@backup~]#ifconfig ens33 | egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}" | head -n 1 192.168.154.5 利用sed命令获取IP [root@backup~]#ifconfig ens33 | sed -n /inet /p | sed s/inet ([0-9.]+).*/1/ | tr -d 192.168.154.5 获取所有网卡IP [root@backup~]#cat ip.sh #!/bin/bash # Author: cn-Linuxer ifs=(`ifconfig | grep "^e" | awk -F: {print $1}`) for i in `echo ${ifs[@]}`;do echo -e "${i} `ifconfig ${i} | awk NR==2{print $2}`" done [root@backup~]#sh ip.sh ens33 192.168.154.5 ens35 192.168.156.5 Linux学习指南 有收获,点个在看
下一篇:
QT:信号发送了,槽函数没有响应