In Linux, how does one obtain ip addresses only through commands? in linux, how does one obtain ip addresses?

Source: Internet
Author: User

In Linux, how does one obtain ip addresses only through commands? in linux, how does one obtain ip addresses?

A friend of a colleague is taking the test. When he encounters such a problem, he can help solve the problem. As a result, the colleague finds him and helps him solve the problem. I really feel: in some aspects, I really don't know if it is a good thing! The question is roughly as follows. Generally, we use ifconfig to view the NIC information. What command can you use to output only the IP address 192.168.42.128?

A seemingly simple problem is not easy to implement. Let's take a look at the following ideas.

[root@DB-Server ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:9E:70:0E  
          inet addr:192.168.42.128  Bcast:192.168.42.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe9e:700e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:135 errors:0 dropped:0 overruns:0 frame:0
          TX packets:216 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:14062 (13.7 KiB)  TX bytes:26007 (25.3 KiB)

 

[root@DB-Server ~]# ifconfig eth0 | grep "inet addr"
          inet addr:192.168.42.128  Bcast:192.168.42.255  Mask:255.255.255.0

This step is very simple, and we need to use awk to implement it, as shown below, to solve this problem.

[root@DB-Server ~]# ifconfig eth0 | grep "inet addr" | awk '{ print $2}'
 
addr:192.168.42.128
 
[root@DB-Server ~]# ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'
 
192.168.42.128

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.