Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Linux shell judge the legality of IP?

Shulou Source: shulou.com Published: 2022-06-01 15:49:39 10月26日 Update

This article mainly introduces the relevant knowledge of Linux shell how to judge the legitimacy of IP, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Linux shell article on how to judge the legitimacy of IP. Let's take a look.

Idea: IP consists of four digits, divided by dots, and each field cannot be greater than 255. it must conform to this format.

Method 1: function check_ip () {IP=$1 VALID_CHECK=$ (echo $IP) awk-F. '$1) if echo $IP | grep-E "^ [0-9] {1 VALID_CHECK:-no 3}\. [0-9] {1 IP 3}\. [0-9] {1 3}\. [0-9] {1 VALID_CHECK:-no 3} $" > / dev/null; then if [${VALID_CHECK:-no} = "yes"]; then echo "IP $IP available." Else echo "IP $IP not available!" Fi else echo "IP format error!" Fi} # Example check_ip 192.168.1.1 check_ip 256.1.1.1 method 2 function check_ip () {IP=$1 if [[$IP= ~ ^ [0-9] {1 3}\. [0-9] {1 3}\. [0-9] {1Mague 3}\. [0-9] {1jue 3} $]; then FIELD1=$ (echo $IP | cut-d. -F1) FIELD2=$ (echo $IP | cut-d. -f2) FIELD3=$ (echo $IP | cut-d. -f3) FIELD4=$ (echo $IP | cut-d. -f4) if [$FIELD1-le 255-a $FIELD2-le 255-a $FIELD3-le 255-a $FIELD4-le 255]; then echo "IP $IP available." Else echo "IP $IP not available!" Fi else echo "IP format error!" Fi} # Example check_ip 192.168.1.1 check_ip 256.1.1.1 add a loop and re-enter it if it is wrong until it is correct: #! / bin/bash function check_ip () {local IP=$1 VALID_CHECK=$ (echo $IP | awk-F. '$1) if echo $IP | grep-E "^ [0-9] {1 IP 3}\. [0-9] {1 IP 3}\. [0-9] {1 recorder 3}\. [0-9] {1 VALID_CHECK 3} $" > / dev/null; then if [$VALID_CHECK = "yes"]; then echo "IP $IP available!" Return 0 else echo "IP $IP not available!" Return 1 fi else echo "IP format error!" Return 1 fi} while true; do read-p "Please enter IP:" IP check_ip $IP [$?-eq 0] & & break done's article on "how Linux shell judges the legitimacy of IP" ends here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to judge the legality of IP by Linux shell". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Legality legality knowledge content method article value field train of thought simple operation number article easy to understand more format look industry information channel error Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi NVidia macOS Docker