Table of Contents
Use the Shell command line to obtain the local IP address
Home Web Front-end HTML Tutorial Use the Shell command line to obtain the local IP address

Use the Shell command line to obtain the local IP address

Sep 07, 2017 pm 03:22 PM
shell Command Line local machine


Use 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.

mac ifconfig The following implementation

We enter

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

So, let’s optimize the code as follows:

ifconfig | grep inet
Copy after login

Get the following information:

    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
Copy after login

Okay, it’s been streamlined a lot.

We can get our local IP by excluding the information of

inet6 and 127

ifconfig | grep inet | grep -v inet6 | grep -v 127
Copy after login

Get

    inet 192.168.12.139 netmask 0xffffff00 broadcast 192.168.12.255
Copy after login

The front is a tab character, we don’t care, we separate it with spaces and take the second field to get our IP information

ifconfig | grep inet | grep -v inet6 | grep -v 127 | cut -d ' ' -f2
Copy after login

We can successfully get the other local machine we want IP address.

We create a

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 configure

~/.bin/ as an environment variable first. Please refer to this command as a system command, which can be executed at any time

Implement the script to obtain the local IP under linux centos 7

linux Do not use the ifconfig command to obtain information, but use ip a to obtain.

So we modified the above command to

ip a | grep inet | grep -v inet6 | grep -v 127 | sed 's/^[ \t]*//g' | cut -d ' ' -f2
Copy after login

Since the front of the information obtained in Linux is not a tab character but a space, we added

sed ' s/^[ \t]*//g' to remove leading spaces.

Other logic is the same. If you like, you can also make it global, and the logic is basically the same as that of Mac.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explorer.exe does not start on system startup [Fix] Explorer.exe does not start on system startup [Fix] Jun 03, 2023 am 08:31 AM

Explorer.exe does not start on system startup [Fix]

Learn how to use the command line tool sxstrace.exe effectively Learn how to use the command line tool sxstrace.exe effectively Jan 04, 2024 pm 08:47 PM

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 How to quickly delete the line at the end of a file in Linux Mar 01, 2024 pm 09:36 PM

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

Common commands and shortcuts in Linux systems Common commands and shortcuts in Linux systems Jun 18, 2023 am 08:46 AM

Common commands and shortcuts in Linux systems

Upgrade Ubuntu 20.04 to 22.04 via command line Upgrade Ubuntu 20.04 to 22.04 via command line Mar 20, 2024 pm 01:25 PM

Upgrade Ubuntu 20.04 to 22.04 via command line

109 practical shell script examples, the code is clear and easy to use! 109 practical shell script examples, the code is clear and easy to use! Aug 02, 2023 pm 03:25 PM

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

Detailed explanation of python command line parameters Detailed explanation of python command line parameters Dec 18, 2023 pm 04:13 PM

Detailed explanation of python command line parameters

How to perform log aggregation and statistics through Linux command line tools? How to perform log aggregation and statistics through Linux command line tools? Jul 30, 2023 pm 10:07 PM

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

See all articles