Use shell commands to obtain IP addresses

Source: Internet
Author: User

1. Obtain the IPv4 address of a single network adapter. The method is as follows:

Method 1:
$/Sbin/ifconfig ethx | awk '/inet ADDR/{print $2}' | cut-F2-d ":"

Method 2:
$/Sbin/ifconfig ethx | awk '/inet ADDR/{print $2}' | awk-F: '{print $2 }'

Method 3:
$/Sbin/ifconfig ethx | sed-ne's /\(. * \) ADDR: \ ([[: digit:].] *\)\(. * \)/\ 2/P'

Replace X in ethx with the serial number of the NIC to be viewed, for example, eth0, eth1...

Obtain the IPv4 address of all NICs on the local machine. The method is as follows:
$/Sbin/ifconfig | awk '{if ($1 = "Inet" & $3 ~ /^ Bcast/) Print $2} '| cut-F2-d ":"

For an IPv6 network, simply modify the preceding command, for example:
$/Sbin/ifconfig | awk '/inet6/{print $3}' ---- All
$/Sbin/ifconfig ethx | awk '/inet6/{print $3}' ---- A single

 

Ii. Read methods in the AIX operating system:

# Ifconfig EN2 | sed-n "2,2 P" | awk '{print $2 }'

Ii. Reading methods in Linux

# Ifconfig eth0 | grep "Inet ADDR" | cut-F 2-D ":" | cut-F 1-d ""

About cut Parameters

-D is used to define the delimiter. The default Delimiter is the tab key,

-F indicates the field to be retrieved, and 2 indicates obtaining the second field separated by the separator specified by-D.

Of course, you can also use cut to get the specific characters in each line in the file, for example:

Cut-c3-5/etc/passwd

It is the third to fifth character of each line in the output/etc/passwd file.

The-C and-F parameters can be described as follows:

N nth character or field

N-from the first character or field to the end of the file

N-M from N to M characters or fields

-M: From the first character to the MB character or field

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.