如何显示接口的IP地址?

如果我想显示分配给eth1的IP地址,那么我怎样才能在Bash中做到这一点?

试试这个(Linux)

/sbin/ifconfig eth1 | grep 'inet addr:' | cut -d: -f2| cut -d' ' -f1 

或者这个(Linux)

 /sbin/ifconfig eth0 | awk -F ' *|:' '/inet addr/{print $4}' 

或者这个(* BSD)

 ifconfig bge0 | grep 'inet' | cut -d' ' -f2 

或者这个(Solaris 10)

 ifconfig e1000g0 | awk '/inet / {print $6}' 

显然要改变接口名称来匹配你想要获取信息的接口名称。

更好的方法:从命令“ip”获取IP地址,因为“ifconfig”已过期。 否则,使用“ifconfig”会出现问题,因为ifconfig的输出是语言依赖。

我使用这个命令获取所有IP(IPv4):

 ip addr show | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" 

也许这会有所帮助

 ifconfig eth1 

在Linux系统上

 hostname -i 

会给你只有IP地址。

在Solaris系统上使用:

 ifconfig e1000g0 | awk '/inet / {print $2}' 

一个class轮与sed:

 ifconfig wlan0 | sed -En -e 's/.*inet ([0-9.]+).*/\1/p' 

用所需的接口replacewlan0。