#ip #network #csv

bin+lib ripcalc

ripcalc, format and lookup IP addresses

7 releases

0.1.11 Aug 31, 2023
0.1.10 Jul 24, 2023
0.1.9 Jun 15, 2023
0.1.7 Apr 28, 2023
0.1.3 Mar 5, 2023

#16 in #csv

48 downloads per month


1.5K SLoC


Calculate or lookup network addresses.


$ git clone 'https://gitlab.com/edneville/ripcalc.git'
$ cd ripcalc \
  && cargo build --release \
  && please install -m 0755 -s target/release/ripcalc /usr/local/bin


Ripcalc allows networks to be provided by argument

$ ripcalc
IP is:
Broadcast is:
Network is:
Subnet is:
Wildcard is:
Network size: 16777216

The same output is visible with stdin:

echo '' | ripcalc

The output format can be customised:

$ ripcalc 2001:ba8:1f1:f1cb::4/64 --format "select * from IP6 where (ip >= %ln and ip <= %lb) and active = 1;\nupdate IP6 set active = 0 where (ip >= %ln and ip <= %lb) and active = 1;"
select * from IP6 where (ip >= 42540724579414763292693624807812497408 and ip <= 42540724579414763311140368881522049023) and active = 1;
update IP6 set active = 0 where (ip >= 42540724579414763292693624807812497408 and ip <= 42540724579414763311140368881522049023) and active = 1;


Sometimes if you want to quickly see if an address is part of a group of networks you can set something like this in your .bash_aliases:

alias is_our_networks='ripcalc --inside --format short'

With this alias it would then be possible to do something like this to quickly see if the domain uses your infrastructure:

dig +short domain.com | is_our_networks


% denotes a format control character, followed by one of the following:

placeholder effect
%a IP address string
%n Network address string
%s Subnet address string
%w Wildcard address string
%b Broadcast address string

Additional characters prefixing the above placeholder can control the representation:

placeholder effect
%B Binary address string
%S Split binary at network boundary string
%l Unsigned integer string
%L Signed integer string
%x Hex address string

Other format characters:

placeholder effect
%c CIDR mask
%t Network size
%r Network reservation information (if available)
%d Matching device interface by IP
%m Matching media link interface by network
%k RBL-style format
%% %
\n Line break
\t Tab character

For example:

$ ripcalc --format '%k.all.s5h.net\n'

With a csv it can find networks that an IP address is within, use %{field} to print matches:

$ cat nets.csv
rfc1918,,mr nobody
$ ripcalc --csv nets.csv -i range --format '%{owner}\n'

Addresses can be read via file or from stdin (-):

$ cat list
$ ripcalc --csv nets.csv -i range --format '%{range} %{owner}\n' -s list mr nobody bob cliff mr nobody

When -a is used, addresses read from -s will not be shown when listing -l a network, showing only available addresses.

When -e is used with -s the smallest encapsulating network will be returned.

Given a list of IP addresses, print only those that match the network. When s and inside are used, only addresses from -s are printed if they are that are inside of the IP source network on the command line. This can be inverted with `--outside:

$ echo -e '\n192.167.255.255\n' | ripcalc -s - --inside --format short
$ echo -e '\n192.167.255.255\n' | ripcalc -s - --outside --format short

IP addresses can be treated as reversed, if /proc/net/route holds addresses in reversed format, --reverse inputs and --base 16 could be used together to convert to dotted-quad.


Networks can be divided into subnets:

$ ripcalc --divide 26 --format cidr


    -4, --ipv4 IPv4     ipv4 address
    -6, --ipv6 IPv6     ipv6 address
    -f, --format STRING format output
                        'cidr' expands to %a/%c\n
                        'short' expands to %a\n
                        See manual for more options
    -m, --mask CIDR     cidr mask
    -c, --csv PATH      csv reference file
    -i, --field FIELD   csv field
    -l, --list          list all addresses in network, combine with -m to list
    -h, --help          display help
    -b, --base INTEGER  ipv4 base format, default to oct
    -a, --available     display unused addresses
        --outside       display only outside network
        --inside        display only inside network
    -d, --divide CIDR   divide network into chunks
    -r, --reverse       (none, inputs, sources or both) v4 octets, v6 hex
    -s, --file PATH     lookup addresses from, - for stdin
    -e, --encapsulating
                        display encapsulating network from lookup list
    -v, --version       print version


~57K SLoC