Multiple ways to take IP addresses from Linux commands

Source: Internet
Author: User

The following command environment is the centos6.6 system;

1234567891011121314151617181920212223 [[email protected] ~]# ifconfig eth0eth0      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/64Scope: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 add1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN    link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00    inet 127.0.0.1/8scope host lo    inet6 ::1/128scope host       valid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000    link/ether00: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/64scope 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.