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