Linux shell中获取IP地址
Linux shell中获取IP地址的方法:
1.hostname -I
2.ifconfig ens33 | grep inet | awk -F "[: ]+" '{print $3}' | head -n 1
3.ifconfig -a | grep inet| grep -v inet6|grep -v 127.0.0.1 |awk '{print $2}'
4.ip a| grep inet |grep -v inet6|grep -v 127.0.0.1 |egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"|head -n 1
5.ifconfig -a |grep inet|grep -v inet6|grep -v 127.0.0.1|egrep -o "inet [^ ]*"|grep -o "[0-9.]*"
这5种方法都能获取到IP地址,相对来说,hostname -I的命令是最简单的。
其他的方法都是结合grep、egrep、awk、head等其他命令结合使用的。Linux系统里面有很多的命令,常用的脚本里面比如:grep、sed、awk等,可以结合head、tail、sort、uniq、tac等命令,读取Linux系统里面的IP信息和其他数据。

computer