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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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

Nowadays, many Windows users start encountering severe Windows system problems. The problem is that Explorer.exe cannot start after the system is loaded, and users cannot open files or folders. Although, Windows users can open Windows Explorer manually using Command Prompt in some cases and this must be done every time the system restarts or after system startup. This can be problematic and is due to the following factors mentioned below. Corrupted system files. Enable fast startup settings. Outdated or problematic display drivers. Changes were made to some services in the system. Modified registry file. Keeping all the above factors in mind, we have come up with some that will surely help the users

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

When processing files under Linux systems, it is sometimes necessary to delete lines at the end of the file. This operation is very common in practical applications and can be achieved through some simple commands. This article will introduce the steps to quickly delete the line at the end of the file in Linux system, and provide specific code examples. Step 1: Check the last line of the file. Before performing the deletion operation, you first need to confirm which line is the last line of the file. You can use the tail command to view the last line of the file. The specific command is as follows: tail-n1filena

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

Many friends who use win10 system have encountered this problem when playing games or installing the system. The application cannot be started because the parallel configuration of the application is incorrect. For more information, see the application event log, or use the command line sxstrace.exe tool. This may be because the operating system does not have corresponding permissions. Let’s take a look at the specific tutorial below. Tutorial on using the command line sxstrace.exe tool 1. This problem usually occurs when installing programs and games. The prompt is: The application cannot be started because the parallel configuration of the application is incorrect. For more information, see the application event log, or use the command line sxstrace.exe tool. 2. Start →

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

This article details the steps to upgrade Ubuntu 20.04 to 22.04. For users using Ubuntu 20.04, they have missed the new features and advantages brought by version 22.04. In order to get a better experience and security, it is recommended to upgrade to a newer Ubuntu version in time. Ubuntu22.04 is codenamed "Jamie Jellyfish", let's explore how to get the latest LTS version! How to upgrade Ubuntu 20.04 to 22.04 via the command line Mastering the command line will give you an advantage. While it is possible to update Ubuntu via the GUI, our focus will be via the command line. First, let’s check the currently running version of Ubuntu using the following command: $

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

With the widespread application of the Linux operating system, more and more people are beginning to need to learn and understand the basic commands and shortcuts in the Linux system. In this article, we will introduce some commonly used Linux commands and shortcuts to help beginners understand the Linux system and improve work efficiency. Commonly used commands 1.1ls command The ls command is one of the most commonly used commands in Linux. It is mainly used to list files and subdirectories in the current directory. Commonly used options are: -l: Display file information in long format, including file type

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

In Python, parameters can be passed to scripts via the command line. These parameters can be used inside scripts to perform different actions based on different inputs. Detailed explanation of Python command line parameters: 1. Positional parameters: parameters passed to the script in order on the command line. They can be accessed through position inside the script; 2. Command line options: parameters starting with - or -, usually Used to specify specific options or flags for the script; 3. Pass parameter values: Pass parameter values ​​through the command line.

A first look at Django: Create your first Django project using the command line A first look at Django: Create your first Django project using the command line Feb 19, 2024 am 09:56 AM

Start the journey of Django project: start from the command line and create your first Django project. Django is a powerful and flexible web application framework. It is based on Python and provides many tools and functions needed to develop web applications. This article will lead you to create your first Django project starting from the command line. Before starting, make sure you have Python and Django installed. Step 1: Create the project directory First, open the command line window and create a new directory

javac is not recognized as an internal or external command or an operable program. How to solve the problem? javac is not recognized as an internal or external command or an operable program. How to solve the problem? Jun 08, 2023 pm 04:54 PM

Solution to the problem that javac is not an internal or external command and is not an operable program: 1. First download the latest version of JDK from the official website and install it; 2. Configure the system environment variables and add the jdk installation path to the path; 3. Enter the computer command Run the interface, enter "java -v" and the version number will appear.

See all articles