Multiple ways to take IP addresses from Linux commands

Source: Internet
Author: User

Often encountered in the command line to take the IP address, or write a script reference IP address, need to use the command to take out; The following method is adjusted according to the actual situation

The following command environment is the centos6.6 system;

[[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/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 add1: 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 forever2: 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




This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1721127

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.