Use the Shell command line to obtain the local IP address
Sep 07, 2017 pm 03:22 PMUse the Shell command line to obtain the local IP address
Enter ifconfig
under mac or enter# under linux
##ip a can get our network card information. But usually, what we need to check is our IP address, and we don't need so much information. So, I want to extract this information.
ifconfig and we will get a large piece of information. We can see that the string in front of the IP address we want is
inet
ifconfig | grep inet
inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 inet6 fe80::85e:9798:4041:1717%en0 prefixlen 64 secured scopeid 0x4 inet 192.168.12.139 netmask 0xffffff00 broadcast 192.168.12.255 inet6 fd86:415c:c5f9::c72:1c02:f044:65de prefixlen 64 autoconf secured inet6 fd86:415c:c5f9::cd76:7bb1:f77d:46f0 prefixlen 64 deprecated autoconf temporary inet6 fd86:415c:c5f9::508d:685b:6a66:b3ce prefixlen 64 autoconf temporary inet6 fe80::9446:a1ff:fe5e:9b8f%awdl0 prefixlen 64 scopeid 0x9 inet6 fe80::9d71:6fa:3da5:9cb6%utun0 prefixlen 64 scopeid 0xa
inet6 and
127
ifconfig | grep inet | grep -v inet6 | grep -v 127
inet 192.168.12.139 netmask 0xffffff00 broadcast 192.168.12.255
ifconfig | grep inet | grep -v inet6 | grep -v 127 | cut -d ' ' -f2
getip file under
~/.bin/ and use
chmod +x ~/.bin/getip to execute it permissions. Then enter
getip in the command line to get our local IP.
You need to configureImplement the script to obtain the local IP under linux centos 7~/.bin/
as an environment variable first. Please refer to this command as a system command, which can be executed at any time
linux Do not use the
ifconfig command to obtain information, but use
ip a to obtain.
ip a | grep inet | grep -v inet6 | grep -v 127 | sed 's/^[ \t]*//g' | cut -d ' ' -f2
sed ' s/^[ \t]*//g' to remove leading spaces.
The above is the detailed content of Use the Shell command line to obtain the local IP address. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
![Explorer.exe does not start on system startup [Fix]](https://img.php.cn/upload/article/000/887/227/168575230155539.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Explorer.exe does not start on system startup [Fix]

Learn how to use the command line tool sxstrace.exe effectively

How to quickly delete the line at the end of a file in Linux

Common commands and shortcuts in Linux systems

Upgrade Ubuntu 20.04 to 22.04 via command line

109 practical shell script examples, the code is clear and easy to use!

Detailed explanation of python command line parameters

How to perform log aggregation and statistics through Linux command line tools?
