This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14174|Reply: 1

[linux] Obtain the local IP address method in the Linux shell script

[Copy link]
Posted on 11/22/2021 10:28:30 AM | | | |
Method 1: ifconfig -a

command parsing

ifconfig -a returns all the IP information of the machine in the same way as running this command under window
grep inet intercepts the line containing the IP
grep -v 127.0.0.1 Remove the line that points locally
grep -v inet6 to remove the line containing inet6
awk { print $2} $2 represents the second group that is divided by space by default, and $1 represents the first group
tr -d "addr: Remove the string "addr:"

Multi-network card situation

If there are multiple NICs, multiple IP addresses of different CIDR segments may appear, and if you still execute the above command, multiple IP addresses will be returned, as follows:

Suppose a machine has IP addresses of 192.*.*.8 and 10.*.*.*.* CIDR blocks, and now we want to implement the IP addresses of different CIDR segments to print different outputs, the shell script is as follows:




Method 2: IP addr

That's all for this article on how to get a native IP address in a Linux shell script.



Original:The hyperlink login is visible.




Previous:Notes: Based on. .NET platform
Next:Linux kernel:NMI watchdog: BUG: soft lockup - CPU
 Landlord| Posted on 10/6/2023 11:09:02 AM |
Windows does not have a browser to obtain an external (egress) IP address
https://www.itsvse.com/thread-10068-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com