Shell script implementation to determine whether the IP address in an IP segment code sharing

Source: Internet
Author: User
Tags exit min printf ipcalc

This article mainly introduces the shell script to determine whether the IP address in an IP segment code sharing, this article directly to the implementation code, the need for friends can refer to the

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 Cat ipcalc.sh #!/bin/bash ip=$1 If [-Z $IP] then echo-e "e[31;42m sh ipcalc.sh IP e[0m" Exit fi while read MASK do min= '/usr/bin/ipcalc $MASK |awk '/hostmin:/{print $} ' max= '/usr/bin/ipcalc $MASK |awk '/hostmax:/{print $} ' min= ' echo $ Min|awk-f "." ' {printf '%.0fn ', $1*256*256*256+$2*256*256+$3*256+$4} ' max= ' echo $max |awk-f. ' ' {printf '%.0fn ', $1*256*256*256+$2*256*256+$3*256+$4} ' ipvalue= ' echo $IP |awk-f. ' ' {printf '%.0fn ', $1*256*256*256+$2*256*256+$3*256+$4} ' if [' $IPvalue '-gt ' $MIN '] && [' $IPvalue '-lt ' $MAX '] t Hen echo-e "e[31;42m $IP in Ipset $MASK e[0m" Exit fi done<ipset.conf

ipset.conf format

?

1 2 3 4 5 6 7 8 More ipset.conf 58.24.64.0/18 58.24.0.0/18 58.24.128.0/18 58.24.192.0/18 58.25.128.0/18 58.25.192.0/18 64.254.111.0/24

Run SH ipcalc.sh IP address

Less efficient hey, next time, try Perl.

/usr/bin/ipcalc This is not a system-led command is a Perl version of the

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.