ipcalc

IPの整合性チェックをしたいと思い表題を試してみることにした。

■ 環境

  • maxOS High Sierra
  • Amazon Linux

■ ipcalc

`ipcalc`でできるよ。という話を聞いて早速試してみた。ローカルで動かすスクリプトのつもりなのでMacで動いて欲しい。

■ macOS High Sierra

$ brew search ipcalc
==> Searching local taps...
ipcalc                           sipcalc
==> Searching taps on GitHub...
==> Searching blacklisted, migrated and deleted formulae...
$

sipcalc“というものもあるのか、と思ってこちらも調べてみたのだが、”sipcalc“はSubnetを計算させるもののようなので今回やりたいことにマッチしない。というわけで”ipcalc“をインストールした。

$ brew install ipcalc

実行しようと思ったところ、イメージと違う…。

$ ipcalc -h
Usage: ipcalc [options] >address< [[/]<NETMASK>] [NETMASC]

ipcalc takes an IP address and netmask and calculates the resulting broadcast, 
network, Cisco wildcard mask, and host range. By giving a second netmask, you 
can design sub- and supernetworks. It is also intended to be a teaching tool 
and presents the results as easy-to-understand binary values. 

-n --nocolor Don't display ANSI color codes. 
-b --nobinary Suppress the bitwise output. 
-c --class Just print bit-count-mask of given address. 
-h --html Display results as HTML (not finished in this version). 
-v --version Print Version. 
-s --split n1 n2 n3 
        Split into networks of size n1, n2, n3. 
-r --range Deaggregate address range. 
   --help Longer help text. 

Examples: 

ipcalc 192.168.0.1/24 
ipcalc 192.168.0.1/255.255.128.0 
ipcalc 192.168.0.1 255.255.128.0 255.255.192.0 
ipcalc 192.168.0.1 0.0.63.255 

ipcalc >ADDRESS1< - >ADDRESS2< deaggregate address range 

ipcalc >ADDRESS</>NETMASK< --s a b c 
        split network to subnets where a b c fits in. 

! New HTML support not yet finished. 

ipcalc 0.41 
$

これもSubnetやNetworkアドレスの計算とかができるものであって、IPアドレスとして正しいものかのチェックはできなさそうである。下記のようにエラーメッセージを見ればできるが、戻り値も正常で返るし少々使い勝手が自身には良くない。

$ ipcalc 192.168.33.256
INVALID ADDRESS: 192.168.33.256

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

$
$ echo $?
0
$

■ Amazon Linux

$ which ipcalc
/bin/ipcalc
$
$ rpm -qf /bin/ipcalc
initscripts-9.03.58-1.39.amzn1.x86_64
$
$ ipcalc -h
ipcalc: ip address expected
Usage: ipcalc [OPTION...]
  -c, --check         Validate IP address for specified address family
  -4, --ipv4          IPv4 address family (default)
  -6, --ipv6          IPv6 address family
  -b, --broadcast     Display calculated broadcast address
  -h, --hostname      Show hostname determined via DNS
  -m, --netmask       Display default netmask for IP (class A, B, or C)
  -n, --network       Display network address
  -p, --prefix        Display network prefix
  -s, --silent        Don't ever display error messages

Help options:
  -?, --help          Show this help message
  --usage             Display brief usage message
$

こっちがお目当の`ipcalc`であったようだ…。

$ ipcalc -c 192.168.33.256
ipcalc: bad IPv4 address: 192.168.33.256
$
$ ipcalc -c 192.168.33.255
$

Macではちょっと考えることにしよう。

以上。

■ 関連

Private IP Range 確認