Check for IP validity

后端 未结 13 1445
庸人自扰
庸人自扰 2020-12-05 07:13

How do I check the validity of an IP address in a shell script, that is within the range 0.0.0.0 to 255.255.255.255?

13条回答
  •  死守一世寂寞
    2020-12-05 07:29

    I prefer to use ipcalc to do this, as long as my script doesn't have to be portable.

    ipcalc 1.1.1.355                                                                         
    INVALID ADDRESS: 1.1.1.355
    
    Address:   192.168.1.1          11000000.10101000.00000001. 00000001
    Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
    Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
    =>
    Network:   192.168.1.0/24       11000000.10101000.00000001. 00000000
    HostMin:   192.168.1.1          11000000.10101000.00000001. 00000001
    HostMax:   192.168.1.254        11000000.10101000.00000001. 11111110
    Broadcast: 192.168.1.255        11000000.10101000.00000001. 11111111
    Hosts/Net: 254                   Class C, Private Internet
    

    There is a great page showing how to use it in scripting, etc, here: SleeplessBeastie's Notes

提交回复
热议问题