The following command environment is the centos6.6 system;
1234567891011121314151617181920212223 |
[[email protected] ~]
# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:2A:03:61
inet addr:192.168.20.50 Bcast:192.168.20.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2a:361
/64
Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:339 errors:0 dropped:0 overruns:0 frame:0
TX packets:270 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30384 (29.6 KiB) TX bytes:30677 (29.9 KiB)
Interrupt:18 Base address:0x2000
[[email protected] ~]
# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link
/ether
00:0c:29:2a:03:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.20.50
/24 brd 192.168.20.255 scope global eth0
inet6 fe80::20c:29ff:fe2a:361
/64
scope link
valid_lft forever preferred_lft forever
|
Grep+cut Method:
[Email protected] ~]# ifconfig eth0|grep ' inet addr: ' |cut-d:-f2|cut-d ' "-f1
192.168.20.50
Sed method: Replace with regular
[[email protected] ~]# ifconfig eth0|sed-nr ' 2s#^.*addr: (. *) Bca.*$#\1#g ' P
192.168.20.50
Sed method (not very well understood)
Match/inet/This line is replaced 2 times, replaced by empty, the rest is the IP address;
[Email protected] ~]# ifconfig eth0|sed-n '/inet/{s/.*addr://;s/. *//;p} '
192.168.20.50
Awk method:
[Email protected] ~]# ifconfig Eth0|awk '/inet addr:/ {print $} ' |awk-f: ' {print $} '
192.168.20.50
Awk a command method: (depending on the actual environment, check the IP row to adjust)
[[Email protected] ~]# IP add|awk-f ' [/]+ ' nr==8 {print $} '
192.168.20.50
Get "(Labor income, do not like to spray)"
Multiple ways to take IP addresses from Linux commands