-
-
Save harasou/62ce00e50d7bc6283c8a to your computer and use it in GitHub Desktop.
シェルスクリプトで IPアドレス計算 ref: http://qiita.com/harasou@github/items/5c14c335388f70e178f5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ip2decimal 192.168.0.1 | |
3232235521 | |
$ decimal2ip 3232235521 | |
192.168.0.1 | |
$ | |
$ ip2decimal 255.255.255.0 | |
4294967040 | |
$ cidr2decimal 24 | |
4294967040 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IPアドレス表記 -> 32bit値 に変換 | |
function ip2decimal(){ | |
local IFS=. | |
local c=($1) | |
printf "%s\n" $(( (${c[0]} << 24) | (${c[1]} << 16) | (${c[2]} << 8) | ${c[3]} )) | |
} | |
# 32bit値 -> IPアドレス表記 に変換 | |
function decimal2ip(){ | |
local n=$1 | |
printf "%d.%d.%d.%d\n" $(($n >> 24)) $(( ($n >> 16) & 0xFF)) $(( ($n >> 8) & 0xFF)) $(($n & 0xFF)) | |
} | |
# CIDR 表記のネットワークアドレスを 32bit値に変換 | |
function cidr2decimal(){ | |
printf "%s\n" $(( 0xFFFFFFFF ^ ((2 ** (32-$1))-1) )) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192.168.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 10進数 | |
3232235521 | |
# 2進数 | |
11000000 10101000 00000000 00000001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IPアドレス表記 -> 32bit値 に変換 | |
function ip2decimal(){ | |
local IFS=. | |
local c=($1) | |
printf "%s\n" $(( (${c[0]} << 24) | (${c[1]} << 16) | (${c[2]} << 8) | ${c[3]} )) | |
} | |
# 32bit値 -> IPアドレス表記 に変換 | |
function decimal2ip(){ | |
local n=$1 | |
printf "%d.%d.%d.%d\n" $(($n >> 24)) $(( ($n >> 16) & 0xFF)) $(( ($n >> 8) & 0xFF)) $(($n & 0xFF)) | |
} | |
# CIDR 表記のネットワークアドレスを 32bit値に変換 | |
function cidr2decimal(){ | |
printf "%s\n" $(( 0xFFFFFFFF ^ ((2 ** (32-$1))-1) )) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ip2decimal 192.168.0.1 | |
3232235521 | |
$ decimal2ip 3232235521 | |
192.168.0.1 | |
$ | |
$ ip2decimal 255.255.255.0 | |
4294967040 | |
$ cidr2decimal 24 | |
4294967040 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ IP=192.168.0.1 | |
$ MASK=255.255.255.0 | |
$ | |
$ num=$(( $(ip2decimal $IP) & $(ip2decimal $MASK) )) | |
$ decimal2ip $num | |
192.168.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ IP=192.168.0.1 | |
$ CIDR=24 | |
$ | |
$ num=$(( $(ip2decimal $IP) & $(cidr2decimal $CIDR) )) | |
$ decimal2ip $num | |
192.168.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ IP=192.168.0.1 | |
$ MASK=255.255.255.0 | |
$ | |
$ num=$(( $(ip2decimal $IP) | (0xFFFFFFFF ^ $(ip2decimal $MASK)) )) | |
$ decimal2ip $num | |
192.168.0.255 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ function iplist(){ | |
> local num=$(ip2decimal $1) | |
> local max=$(($num + $2 - 1)) | |
> | |
> while : | |
> do | |
> decimal2ip $num | |
> [[ $num == $max ]] && break || num=$(($num+1)) | |
> done | |
> } | |
$ iplist 192.168.0.253 5 | |
192.168.0.253 | |
192.168.0.254 | |
192.168.0.255 | |
192.168.1.0 | |
192.168.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ function ipwith(){ | |
> local addr=$1 | |
> local mask=$2 | |
> local num=$(ip2decimal $3) | |
> | |
> local net=$(( $(ip2decimal $addr) & $(ip2decimal $mask) )) | |
> local brd=$(( $(ip2decimal $addr) | (0xFFFFFFFF ^ $(ip2decimal $mask)) )) | |
> | |
> [ $net -le $num -a $num -le $brd ] && return 0 || return 1 | |
> } | |
$ ipwith 192.168.0.1 255.255.255.0 192.168.0.200 | |
$ echo $? | |
0 | |
$ ipwith 192.168.0.1 255.255.255.0 192.168.1.200 | |
$ echo $? | |
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment