Validity of the IP, subnet mask, and gateway in a Linux shell script [go]

Source: Internet
Author: User



The following examples provide only a few ideas and an easy way. Please refer to the use. The following code is validated in bash and adjusts itself if you use a different shell.



1:IP Format validity judgment





 
 
1 #return 1(failure) invalid ip,0(success) valid ip  
2 is_valid_ip_format()   
3 {  
4     if [[ "$1" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] ;then  
5         return 0  
6     else  
7         return 1  
8     fi  
9 }  
 

 





Additional notes:






If you use the Shell's regular expression in a judgment statement, be aware that you do not need quotes on regular expressions! This is a new feature of the shell that has similar statements on the web but is not available because of the quotation marks on the regular expression.






2:ip Conflict Detection





 
 1 #return 1(failure) conflict,0(success) not conflict  
 2 is_ip_conflict()  
 3 {  
 4     conflict=`ping -c 2 -t 1 $1 >/dev/null 2>&1 && arp $1`  
 5     if [ -z "$conflict" ];then  
 6         return 0  
 7     else  
 8         return 1  
 9     fi  
10 }  





Additional notes:






IP collision detection seems to be a very complex problem, in fact, in the TCP/IP protocol, an additional function of the ARP protocol, the principle is very simple, using the ARP protocol to broadcast their own IP address to the network, if the response there is an IP conflict, this function in the tcp/ The first volume of the IP protocol ARP protocol chapter is described in detail. This first uses the ping command to probe the set IP address, and then in the ARP cache to find out if there are valid records, if any, there is an IP conflict. There is no need to care whether the ping command succeeds, whether or not it succeeds, as long as the machine is on, ARP can get to its MAC address, because ARP works at the data link layer.






3: Subnet Mask validation


 
 1 #return 1(failure) is not a valid mask,0(success) is a valid mask  
 2 is_valid_mask()  
 3 {  
 4     nm=$(aton $1)  
 5   
 6     bit=1  
 7     flag=0  
 8     for i in {0..31}  
 9     do  
10         v=$(( $bit << $i))  
11         flag=$(($nm & $v))  
12         if [ $flag -ne 0 ];then  
13             break  
14         fi  
15     done  
16   
17     # ex:xxx.xxx.xxx.254/255 are not a valid mask  
18     if [ "$i" -lt 2 ];then  
19         return 1  
20     fi  
21   
22     for j in `seq $i 31`  
23     do  
24         v=$(( $bit << $j))  
25         flag=$(($nm & $v))  
26         if [ $flag -eq 0 ];then  
27             break  
28         fi  
29     done  
30   
31     if [ "$flag" -eq 0 ];then  
32         return 1  
33     else  
34         return 0  
35     fi  
36 }  





Additional notes:






The subnet mask can also be considered a very characteristic IP address. It is characterized in that if the subnet mask is turned into binary, then the 1 and 0 are continuous, that is, there will be No 1 and 0 cross occurrence phenomenon, also because of this, IP configuration can have 192.168.1.23/24 such a way of writing. Therefore, the detection of the subnet mask is valid is to determine whether the 1 is continuous.






Validity judgment of 4:IP, subnet mask and gateway





 
 1 #return 1(failure) is not a valid net,0(success) is a valid net  
 2 is_valid_net()  
 3 {  
 4     ip=$1  
 5     mask=$2  
 6     gw=$3  
 7       
 8     #ip&mask == gw&mask while is a valid net  
 9     ipn=$(aton $ip)  
10     maskn=$(aton $mask)  
11     gwn=$(aton $gw)  
12       
13     if [ $(($ipn & $maskn)) -ne $(($gwn & $maskn)) ];then  
14         return 1  
15     else  
16         return 0  
17     fi  
18 }  





Additional notes:
As the basis of the network, the host IP and gateway should be in the same subnet, based on this, can be used as a simple way to determine the IP address, subnet mask and gateway basic principle, that is, the IP address and subnet mask bit and the result should be the same as the gateway and subnet mask bit and the result.






5:ip address string to Integer





 
1 aton()  
2 {  
3     echo $1|gawk ‘{c=256;split($0,ip,".");print ip[4]+ip[3]*c+ip[2]*c^2+ip[1]*c^3}‘  
4 }  





Additional notes:



Converting an IP address to an integer should be the most basic data conversion for easy computation. This function is used for both 4 and 52 of the above methods.






ref:http://blog.csdn.net/ssmile/article/details/53188050



Validity of the IP, subnet mask, and gateway in a Linux shell script [go]


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.