[LINUX] Get only the address part of NIC (eth0)

Network address

If you display the ip with ifconfig, you can get the ip of multiple NICs. For example, it looks like the following. Think about how to get the NIC address of eth0 with ifconfig.

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.28.99.150  netmask 255.255.240.0  broadcast 172.28.111.255
        inet6 fe80::215:5dff:xxx:xxxx  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:90:90:41  txqueuelen 1000  (Ethernet)
        RX packets 742  bytes 149376 (149.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1216 (1.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Grep the address of eth0

To gerp with ifconfig and get only the IP address, do as follows.

ifconfig eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

Display the information of the NIC you want to get with ifcongit eth0. If you want to get the IP address of another NIC, specify the number like ethXX. (Is this the case ...) Then I pipe the result and grep it with a regular expression. o is an option to take only the matches, P is a Perl-style regular expression, With this combination, you can create a part where row extraction and clipping match.

Recommended Posts

Get only the address part of NIC (eth0)
Try installing only the core part of Ubuntu
Get the number of views of Qiita
Get the attributes of an object
Get the first element of queryset
Get the number of Youtube subscribers
Get the address from the zip code
Implement part of the process in C ++
Get the column list & data list of CASTable
Get the minutes of the Diet via API
Understand the "temporary" part of UNIX / Linux
Get the value of the middle layer of NN
Get the last day of the specified month
[Python] Get the character code of the file
Get the filename of a directory (glob)
Get the address from latitude and longitude
[Python] Get a list of folders only
[PowerShell] Get the reading of the character string
Get only the text from the Django form.
Use Pillow to make the image transparent and overlay only part of it
Get the contents of git diff from python
[Python] Read the source code of Bottle Part 2
[Python] Get / edit the scale label of the figure
Get the caller of a function in Python
Get only the subclass elements in a list
[Python] Get the last updated date of the website
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
Extract only complete from the result of Trinity
This is the only basic review of Python ~ 3 ~
To get the path of the currently running python.exe
[Python] Get the day of the week (English & Japanese)
[Python + OpenCV] Whiten the transparent part of the image
Get the update date of the Python memo file.
Get the variable name of the variable as a character string.
Explanation of the concept of regression analysis using python Part 2
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the text of the law from the e-GOV Law API
Cut a part of the string using a Python slice
Get the sum of each of multiple columns with awk
Get the image of "Suzu Hirose" by Google image search.
Get the absolute path of the script you are running
[python] Get the list of classes defined in the module
Since there are many earthquakes, get the history of earthquakes
Get the return code of the Python script from bat
[C language] [Linux] Get the value of environment variable
Get to know the feelings of gradient boosting trees
Get the size (number of elements) of UnionFind in Python
Try to get the contents of Word with Golang
Explanation of the concept of regression analysis using Python Part 1
[Django] Change the Default IP address of the runserver command
Tweet the probability of precipitation as part of the function of the bot
I tried to erase the negative part of Meros
[Django 2.2] Sort and get the value of the relation destination
Get the operation status of JR West with Python
Script to get the expiration date of the SSL certificate
#We will automate the data aggregation of PES! part1
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller