Get the current IP address in Shell

Source: Internet
Author: User

Reference address:

The information returned by ifconfig includes the IP address, but it is troublesome to obtain the current IP address in shell.

Obtain Method

The formats of ifconfig returned information vary in different systems:[1]

Linux:

Lc_all= CIfconfig| Grep 'Inet ADDR :'| Grep -V '2017. 0.0.1' |Cut-D:-F2 | Awk '{Print $1 }'

Lc_all = C English output
Ifconfig outputs IP information
 # Lc_all = C ifconfig Eth0 link encap: Ethernet hwaddr C2: AC: C3: E7: 4A: 33 Inet ADDR: 192.168.1.247 bcast: 0.0.0.0 mask: 255.255.255.0 Up broadcast running Multicast MTU: 1500 Metric: 1 RX packets: 84080 errors: 0 dropped: 80 Overruns: 0 frame: 0 TX packets: 3595264 Errors: 0 dropped: 0 overruns: 0 carrier: 0 Collisions: 0 txqueuelen: 1000 RX Bytes: 14627750 (13.9 MIB) TX Bytes: 417627051 (398.2 MiB) interrupt: 27 Lo link encap: local loopback Inet ADDR: 127.0.0.1 mask: 255.0.0.0Up loopback running MTU: 16436 Metric: 1 RX packets: 127 Errors: 0 dropped: 0 overruns: 0 frame: 0 TX packets: 127 Errors: 0 dropped: 0 overruns: 0 carrier: 0 Collisions: 0 txqueuelen: 0 RX Bytes: 18079 (17.6 kib) TX Bytes: 18079 (17.6 kib)

 

 
Grep'Inet ADDR: 'truncate the two lines containing IP addresses
# Lc_all = C ifconfig | grep "Inet ADDR :"Inet ADDR: 192.168.1.247 bcast: 0.0.0.0 mask: 255.255.255.0Inet ADDR:127.0.0.1 mask: 255.0.0.0
 
 
 
Grep -V '127. 0.0.1 'removes the line pointed to locally
 
# Lc_all = C ifconfig | grep "Inet ADDR:" | grep-V "127.0.0.1"Inet ADDR: 192.168.1.247 bcast: 0.0.0.0 mask: 255.255.255.0 
Cut-D:-F2-D: Split string-F2: Take the second group of data
 
# Lc_all = C ifconfig | grep "Inet ADDR:" | grep-V "127.0.0.1" | cut-D:-F2192.168.1.247 bcast
 
Awk'{Print $1}' $1 indicates the first group separated by spaces by default. Similarly, $ @ indicates the second group.
 
# Lc_all = C ifconfig | grep "Inet ADDR:" | grep-V "127.0.0.1" | cut-D:-F2 | awk '{print $1 }'192.168.1.247# Lc_all = C ifconfig | grep "Inet ADDR:" | grep-V "127.0.0.1" | cut-D:-F2 | awk '{print $2 }'Bcast
 

 

 
 
 
 
 
 
 
 
 

FreeBSD/OpenBSD:

Lc_all= CIfconfig| Grep -E 'Et. [0-9]' | Grep -V '2017. 0.0.1' |Awk '{Print $2 }'

Solaris:

 
Lc_all= CIfconfig - | GrepInet| Grep -V '2017. 0.0.1' |Awk '{Print $2 }'

Three paragraphsCodeThe principle is similar. All rows containing IP addresses are obtained first, and then the rows containing 127.0.0.1 are removed. Finally, retrieve the column where the IP is located.

End

Related Article

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.