您的位置:首页 > 运维架构 > Linux

Linux系统下取IP地址的几种方法

2016-03-29 15:59 477 查看
Linux系统下取IP地址所在行的方法:
(1)、ifconfig eth0 | grep "inet addr"
inet addr:10.57.36.112 Bcast:10.57.36.255 Mask:255.255.255.0
注释:grep过滤包含"inet addr"字符串的内容
(2)、ifconfig eth0 | sed -n '2p'
inet addr:10.57.36.112 Bcast:10.57.36.255 Mask:255.255.255.0
注释:sed -n '2p'其中-n参数表示"取消默认打印",2p表示"打印第2行"
(3)、ifconfig eth0 | sed -n '/inet addr/p'
inet addr:10.57.36.112 Bcast:10.57.36.255 Mask:255.255.255.0
(4)、ifconfig eth0 | sed -n '/Bcast/p'
inet addr:10.57.36.112 Bcast:10.57.36.255 Mask:255.255.255.0
注意:sed -n '/match/p' 等同于"grep match"

Linux系统下取IP地址的几种方法:
(1)、 ifconfig eth0 | grep "inet addr" | awk -F":" '{print $2}' | awk '{print $1}'
10.57.36.112
(2)、ifconfig eth0 | grep "inet addr"| sed 's#^.*addr:##g' | sed 's#..Bc.*$##g'
10.57.36.112
(3)、ifconfig eth0 | grep "inet addr"| cut -d: -f2| cut -d" " -f1
10.57.36.112
(4)、ifconfig eth0 | grep "inet addr" | awk -F"[: ]+" '{print $4}'
10.57.36.112
说明:其中取IP地址所在行的方法可采用上述4种方法之一,然后再进行过滤、替换或截取即可获得IP!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息