Monday, March 2, 2015

Linux: IP Subnet (CIDR) Calculator That Will Help You With Network Settings

I'm a new user and networking newbie. I need help with network settings. I'm looking for a tool for calculating available host address ranges with CIDR using Linux command prompt. How do I use subnet calculator under a Linux or UNIX-like systems without visiting 3rd party websites?

Linux and Unix-like systems comes with various IP subnet calculators that will help you with network settings:
[a] Sipcalc is an is an "advanced" console based ip subnet calculator.
Tutorial details
DifficultyEasy (rss)
Root privilegesYes
Estimated completion time2m
In its simplest form it takes an ip-address and a subnet mask on the CLI and outputs information about the subnet. Sipcalc IPv4 features:
  1. Retrieving of address information from interfaces.
  2. Classful and CIDR output.
  3. Multiple address and netmask formats (dotted quad, hex, number of bits).
  4. Output of broadcast address, network class, Cisco wildcard, hosts/range, network range.
  5. Output of a userdefined number of extra networks.
  6. The ability to "split" a network based on a smaller netmask, now also with recursive runs on the generated subnets.
  7. DNS resolution.
Sipcalc IPv6 features:
  1. Compressed and expanded input addresses.
  2. Compressed and expanded output.
  3. Standard IPv6 network output.
  4. Reverse dns address generation.
  5. The ability to "split" a network based on a smaller netmask, now also with recursive runs on the generated subnets.
  6. DNS resolution.
[b] Whatmask is another tool will help you with network settings. It displays the following information given IP address and/or netmask:
  1. The netmask in the following formats: CIDR, Netmask, Hex, Wildcard Bits
  2. The Network Address
  3. The Broadcast Address
  4. The number of Usable IP Addresses
  5. The First Usable IP Address
  6. The Last Usable IP Address
In this tutorial, I'm going to explain how to install and use these two tools.

Sipcalc installation

You can install it as follows on a Debian or Ubuntu Linux using the apt-get command:
$ sudo apt-get update
$ sudo apt-get install sipcalc

Sample outputs:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 51 not upgraded.
Need to get 30.6kB of archives.
After this operation, 123kB of additional disk space will be used.
Get:1 lucid/universe sipcalc 1.1.4-2 [30.6kB]
Fetched 30.6kB in 2s (15.1kB/s)
Selecting previously deselected package sipcalc.
(Reading database ... 203411 files and directories currently installed.)
Unpacking sipcalc (from .../sipcalc_1.1.4-2_i386.deb) ...
Processing triggers for man-db ...
Processing triggers for doc-base ...
Processing 1 added doc-base file(s)...
Registering documents with scrollkeeper...
Setting up sipcalc (1.1.4-2) ...
On a CentOS/RHEL/Fedora Linux type the following yum command (first enable EPEL repo):
# yum install sipcalc
Sample outputs:
Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin, security
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
0 packages excluded due to repository protections
Resolving Dependencies
--> Running transaction check
---> Package sipcalc.x86_64 0:1.1.6-4.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
 Package             Arch               Version                   Repository        Size
 sipcalc             x86_64             1.1.6-4.el6               epel              34 k
Transaction Summary
Install       1 Package(s)
Total download size: 34 k
Installed size: 62 k
Is this ok [y/N]: y
Downloading Packages:
sipcalc-1.1.6-4.el6.x86_64.rpm                                    |  34 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : sipcalc-1.1.6-4.el6.x86_64                                            1/1
  Verifying  : sipcalc-1.1.6-4.el6.x86_64                                            1/1
  sipcalc.x86_64 0:1.1.6-4.el6
On a FreeBSD Unix operating system type the following command to install the sipcalc via port
# cd /usr/ports/net-mgmt/sipcalc/ && make install clean
Or add the package
# pkg install net-mgmt/sipcalc

How do I calculate subnets using sipcalc command?

Here is an example:
$ sipcalc
Sample outputs:
-[ipv4 :] - 0
Host address		-
Host address (decimal)	- 3232235776
Host address (hex)	- C0A80100
Network address		-
Network mask		-
Network mask (bits)	- 24
Network mask (hex)	- FFFFFF00
Broadcast address	-
Cisco wildcard		-
Addresses in network	- 256
Network range		- -
Usable range		- -
The above will provide network start & stop range, wildcard, mask and other information. You can calculate subnet as follows:
$ sipcalc -a
Sample outputs:
sipcalc IP subnet calculator in action
sipcalc IP subnet calculator in action

Interface specific calculation example

Instead of taking address information from the shell command line arg sipcalc can obtain relevant information by looking at a specified interface on the system. In this example, get information for eth0 interface on a Linux based system:
$ sipcalc eth0
Sample outputs:
-[int-ipv4 : eth0] - 0
Host address		-
Host address (decimal)	- 3232236542
Host address (hex)	- C0A803FE
Network address		-
Network mask		-
Network mask (bits)	- 24
Network mask (hex)	- FFFFFF00
Broadcast address	-
Cisco wildcard		-
Addresses in network	- 256
Network range		- -
Usable range		- -
To read more about its option type:
$ sipcalc --help
Sample outputs:
sipcalc 1.1.6
Usage: sipcalc [OPTIONS]... <[ADDRESS]... [INTERFACE]... | [-]>
Global options:
  -a, --all			All possible information.
  -d, --resolve			Enable name resolution.
  -h, --help			Display this help.
  -I, --addr-int=INT		Added an interface.
  -n, --subnets=NUM		Display NUM extra subnets (starting from
				the current subnet). Will display all subnets
				in the current /24 if NUM is 0.
  -u, --split-verbose		Verbose split.
  -v, --version			Version information.
  -4, --addr-ipv4=ADDR		Add an ipv4 address.
  -6, --addr-ipv6=ADDR		Add an ipv6 address.
IPv4 options:
  -b, --cidr-bitmap		CIDR bitmap.
  -c, --classful-addr		Classful address information.
  -i, --cidr-addr		CIDR address information. (default)
  -s, --v4split=MASK		Split the current network into subnets
				of MASK size.
  -w, --wildcard		Display information for a wildcard
				(inverse mask).
  -x, --classful-bitmap	Classful bitmap.
IPv6 options:
  -e, --v4inv6			IPv4 compatible IPv6 information.
  -r, --v6rev			IPv6 reverse DNS output.
  -S, --v6split=MASK		Split the current network into subnets
				of MASK size.
  -t, --v6-standard		Standard IPv6. (default)
Address must be in the "standard" dotted quad format.
Netmask can be given in three different ways:
 - Number of bits    [/nn]
 - Dotted quad       [nnn.nnn.nnn.nnn]
 - Hex               [0xnnnnnnnn | nnnnnnnn]
Interface must be a valid network interface on the system.
If this options is used an attempt will be made to gain the address
and netmask from the specified interface.
Replacing address/interface with '-' will use stdin for reading further

Say hello to whatmask utility

The whatmask tool can easily convert between three common subnet mask notations.

whatmask command installation

You can install it as follows on a CentOS/RHEL/Fedora Linux using yum command:
# yum install whatmask
Sample outputs:
Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin, security
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
0 packages excluded due to repository protections
Resolving Dependencies
--> Running transaction check
---> Package whatmask.x86_64 0:1.2-7.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
 Package              Arch               Version                  Repository        Size
 whatmask             x86_64             1.2-7.el6                epel              21 k
Transaction Summary
Install       1 Package(s)
Total download size: 21 k
Installed size: 39 k
Is this ok [y/N]: y
Downloading Packages:
whatmask-1.2-7.el6.x86_64.rpm                                     |  21 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : whatmask-1.2-7.el6.x86_64                                             1/1
  Verifying  : whatmask-1.2-7.el6.x86_64                                             1/1
  whatmask.x86_64 0:1.2-7.el6
On a FreeBSD based Unix server you can install it via the port:
# cd /usr/ports/net-mgmt/whatmask/ && make install clean
Or add the package
# pkg install net-mgmt/whatmask
Finally, you can download and compile the source code as follows too:
$ cd /tmp
$ wget
$ tar -zxvf whatmask-1.2.tar.gz
$ cd whatmask-1.2/
$ ./configure
$ make
$ sudo make install

How do I use the whatmask tool?

You can use it as follows to find out usable ip address with /29 subnet:
$ whatmask {netmask}
$ whatmask {ip/netmask}
$ whatmask /29

Sample outputs:
CIDR = .....................: /29
Netmask = ..................:
Netmask (hex) = ............: 0xfffffff8
Wildcard Bits = ............:
Usable IP Addresses = ......: 6
OR use ip/netmask syntax:
$ whatmask
Sample outputs:
IP Entered = ..................:
CIDR = ........................: /27
Netmask = .....................:
Netmask (hex) = ...............: 0xffffffe0
Wildcard Bits = ...............:
Network Address = .............:
Broadcast Address = ...........:
Usable IP Addresses = .........: 30
First Usable IP Address = .....:
Last Usable IP Address = ......:
A few more examples:
whatmask command in action on a Linux based system
whatmask command in action on a Linux based system

Monitor network traffic with Cacti

Learn how to install and configure Cacti in order to watch the traffic of a Cisco ADSL router using SNMP

Cacti is an open source network graphing application that uses RRDTool – a data logging and graphing system for time series data.
A router, by default, connects two different networks, and therefore it should have at least two distinct network interfaces. This article will use a Cisco 877W ADSL router that uses three interfaces: one for the ADSL connection, one RJ45 Ethernet port and a Wi- Fi connection. What you want to monitor is the ADSL connection. Don’t worry – do not think that you will need to use the ADSL interface to get the desired data; SNMP can ask any one of the three interfaces and get the same ADSL- related monitoring data!
Our previous tutorial about MRTG in issue 145 of LU&D used the ADSL interface whereas this tutorial will use the IP of the Wi-Fi interface. Using a different IP address or interface makes no difference. Cacti has many more capabilities including support for plugins that enable developers to generate additional Cacti features without dealing with Cacti’s source code. A forthcoming article will show you how to develop a Cacti plugin, but for now we’ll focus on installation and configuration.
Cacti’s Preview view shows all monitoring graphs and is an easy way to get a general overview of what you monitor
Cacti’s Preview view shows all monitoring graphs and is an easy way to get a general overview of what you monitor




Step 01 Get Cacti
On an Ubuntu system you can get Cacti by running the following command:
# apt-get install cacti
This command will automatically install RRDTool as well as other required packages. Cacti installs its files at /usr/share/cacti/.
As Cacti is actually a group of PHP scripts and a database working together and creating a monitoring site, multiple devices can be easily monitored using a centralised site.
Step 02 Installation steps
Installing Cacti is far more difficult than MRTG because Cacti uses a database to save its data instead of plain text files. Cacti uses PHP so your Apache configuration should also support PHP. So, you should have MySQL up and running as well as Apache with PHP support before continuing with the installation of Cacti.
The power that Cacti offers does come at a price that you will only have to pay the first time you install it.
Step 03 Pre-installation actions
You should have root privileges in order to set up Cacti on your Linux machine. You should also know the SNMP-related information of the Cisco router. If the ADSL router is not properly configured to support SNMP, you will also need to have administrative privileges on it and set up SNMP yourself.
The Linux machine must also run MySQL. Although it is not necessary, it is very convenient to have a separate MySQL database to store all Cacti-related data.
Step 04 MySQL Setup
Cacti needs a database in order to work and store its data. The default option is the very popular MySQL database. For the purposes of this article, the name of the MySQL user will be ‘cacti’ and the password will be ‘cactipass’. It is good to use a separate database to store all Cacti related data; it will be called ‘cactiDB’.
You should manually edit the /etc/cacti/debian.conf file and put in the correct database data. This file replaces the include/config.php file that is usually found in Cacti source files.
Step 05 Apache Setup
The Cacti site will be at the /cacti/ URL as defined in the (default) /etc/apache2/conf-available/cacti.conf file. You will also need to install and turn on PHP support. First install the libapache2-mod-php5 package, then enable the php5 Apache module; on an Ubuntu system, this module is automatically enabled after installation. Otherwise, you will have to either run a2enmod or enable the module manually by editing the Apache config files and restarting.
Step 06 Start Cacti installation
The next step is very important. Before doing anything else you should import the Cacti database data inside the MySQL database. On an Ubuntu Linux System, this can be done with the following command:
$ cat /usr/share/doc/cacti/cacti.sql | mysql -u cacti -p cactiDB
Without this step, the installation will fail and you are going to get no data from Cacti!
In order to start the installation process, you should now point your favourite browser at the /cacti/ URL. We will use the http://mb13:3023/cacti/ URL but yours will vary.
Step 07 Finish the installation
Two more simple steps will be required that should present little to no difficulty. After finishing all steps, you should log in to Cacti using the admin user. The first thing you will be asked to do is change the default password for the admin user, which is also “admin”. From now on you can use the http://mb13:3023/cacti/index.php URL to connect to your Cacti installation.
Step 08 Check Cisco configuration
SNMP is a known TCP/IP protocol that is available for most ‘clever’ devices, including Linux and other UNIX machines, routers, network switches, Windows machines, etc.
Before you start installing Cacti, you should make sure that the device you are going to monitor is properly configured. Cacti will acquire data from the Cisco router using SNMP, so you should check if SNMP is properly working on Cisco.
Step 09 Add the Cisco device
After installing and configuring Cacti, you are ready to add devices and graphs to Cacti. The things that you need to know in advance in order to monitor your Cisco router with Cacti are the name of the SNMP community (LUD) and the name or the IP address of the router (
To add the ADSL router, you should first click on Devices on the left. Then select Cisco Router and ‘Enabled’ on Status, and then click Add. Now you will have to fill in the required information that depends on your configuration. It is important to put the correct data in the Hostname and ‘SNMP community’ fields. Then, click Create.
If everything is okay, the next screen will display ‘Create Graphs for this Host’ on the upper-right side of the screen. Click on the ‘Create Graphs for this Host’ link to go to the next screen.
The next screen after this lists all the available Cisco interfaces for this particular router. What interests us right now is Interface number 14 (Dialer1), which is the ADSL Internet connection interface, so make it active. The desired graph type should be ‘In/Out Bits’. Now click the Create button. Other interfaces of interest may be Number 5 (Dot11Radio0), which is Cisco’s Wi-Fi interface and Number 13, which is the Ethernet interface. Now select Graph Trees from the left menu and then click Add. The next screen will allow you to select the device you want to monitor. Follow the instructions on-screen, and you are done!
Step 10 The output generated by Cacti
A script that runs as a cron job updates the output of Cacti – this is automatically configured during installation. You can look at the /etc/cron.d/cacti file for more information about the way Cacti is being executed.
Select the Graphs tab and then, from the Default Tree, select the desired host. You will have to wait a little, until some data is obtained in order for the graphs to be populated.
Step 11 User management
Users in Cacti can be divided into three brief categories: Anonymous, Normal and Administrators. What differentiates these three categories is the way they authenticate and the permissions they have.
You can also add users using the command- line interface of Cacti but using the graphical interface is simpler. You can visit the Cacti docs to find more information about Cacti command line scripts.
Step 12 Create and use a new User
Head across to Console>Utilities>User Management. You can see that two users are created by default: admin and guest. Click Add to create a new Normal user called ‘LUD’. Fill in the required information. On the Realm Permissions, turn on the View Graphs checkbox. Now, press Create to create the new user. It is always useful to turn on the ‘User must change password at next login’ option.
After user creation, edit the new user, go to the Graph Permissions tab and add the graphs that you want the user to be allowed to see.
Step 13 Cacti directories
The /var/lib/cacti/rra directory contains all the RRD files that keep your performance data. The /usr/share/cacti/resource directory holds all the XML files responsible for the data queries of Cacti. The /var/log/cacti directory contains all Cacti log files of Cacti – you should visit its files when there are problems with Cacti. The /usr/share/cacti/cli directory holds all the command line scripts.
Step 14 Back up and restore
Although backing up MRTG is a simple copy process, backing up Cacti is more demanding because its data is stored on a database. You can manually backup the MySQL database using the following command:
$ mysqldump -u cacti -pcactipass cactiDB > cacti.sql
The generated cacti.sql plain text file contains SQL commands that can reconstruct a database from scratch (including its data).
Similarly, you can back up all Cacti configuration files using a simple UNIX script; just make sure that you include all files and directories. If you are not that familiar with MySQL, you should ask your database administrator for help, because backup is a critical task.
Step 15 Bonus material
The default Cacti installation automatically gathers and displays monitoring data about the current machine (localhost). You can see it by selecting Graphs>Default Tree>Host:Localhost.
Step 16 Final thoughts
Installing Cacti is not as easy as installing MRTG. Nevertheless, Cacti produces a more professional output, supports plugins and multiple users with different privileges. Cacti also supports templates. They are used for simplifying the creation and administration of graphs.
Depending on your needs, you can choose MRTG or Cacti and be assured that any of them will serve you well.

Record Screen as Animated GIF in Ubuntu with Byzanz

We have previously shown you how to record your screen as an animated GIF in Windows and Mac OS X, but what about Linux? If you are a Linux user and are looking for ways to record your screen as an animated GIF, byzanz is the app for you.
Byzanz is a simple command line tool to record a running X desktop to an animation suitable for presentation in a web browser, which in this case, refers to an animated GIF. While the support and development stopped years ago, the current version is still working well on Ubuntu 14.10.
For Ubuntu 14.04 and above, Byzanz is available in the Universe Repository. To install, simply search for it in Ubuntu Software Center,or use the command:
sudo apt-get install byzanz
For Fedora, you can use the command:
sudo yum install byzanz
The command to use Byzanz is byzanz-record. To get started, open a terminal and type:
The texts in uppercase are the options that you need to change. Change the DURATION to the length of time to record the animation. The DELAY option is optional, and when set will delay for the specified time before the recording starts. The X-COORDINATE and Y-COORDINATE refer to the location on the screen for it to start capturing and the WIDTH and HEIGHT is the size of the recording. For example, to record the whole desktop for 10 seconds with a 5 second delay, use the command:
byzanz-record -d 10 --delay=5 -x 0 -y 0 -w 1440 -h 900 desktop-animation.gif
Note: you can add the -v flag to get it to display the progress of the recording.
By default, you will need to specify the coordinates of the recording area and the width and height for byzanz to work. Won’t it be great if there is a GUI tool that you can use to record a window? Thanks to user Rob W and MHC on AskUbuntu, here is the bash script that you can use as a GUI for Byzanz.
Open a text editor and paste the following code to it:
# AUTHOR:   (c) Rob W 2012, modified by MHC (
# NAME:     GIFRecord 0.1
# DESCRIPTION:  A script to record GIF screencasts.
# DEPENDENCIES:   byzanz,gdialog,notify-send (install via sudo add-apt-repository ppa:fossfreedom/byzanz; sudo apt-get update && sudo apt-get install byzanz gdialog notify-osd)
# Time and date
TIME=$(date +"%Y-%m-%d_%H%M%S")
# Delay before starting
# Standard screencast folder
# Default recording duration
# Sound notification to let one know when recording is about to start (and ends)
beep() {
    paplay /usr/share/sounds/freedesktop/stereo/message-new-instant.oga &
# Custom recording duration as set by user
USERDUR=$(gdialog --title "Duration?" --inputbox "Please enter the screencast duration in seconds" 200 100 2>&1)
# Duration and output file
if [ $USERDUR -gt 0 ]; then
# Window geometry
read X < <(awk -F: '/Absolute upper-left X/{print $2}' <<< "$XWININFO")
read Y < <(awk -F: '/Absolute upper-left Y/{print $2}' <<< "$XWININFO")
read W < <(awk -F: '/Width/{print $2}' <<< "$XWININFO")
read H < <(awk -F: '/Height/{print $2}' <<< "$XWININFO")
# Notify the user of recording time and delay
notify-send "GIFRecorder" "Recording duration set to $D seconds. Recording will start in $DELAY seconds."
#Actual recording
sleep $DELAY
byzanz-record -c --verbose --delay=0 --duration=$D --x=$X --y=$Y --width=$W --height=$H "$FOLDER/GIFrecord_$TIME.gif"
# Notify the user of end of recording.
notify-send "GIFRecorder" "Screencast saved to $FOLDER/GIFrecord_$TIME.gif"
Save the script as “byzanz-gui” (or any other name that you prefer) to your Home folder without any extension.
Next, grant the script executable permission:
chmod +x byzanz-gui
Lastly, execute the script:
In the window that pops up, enter the duration for the recording and click OK.
You will notice that your mouse cursor has become a crosshair. Click on the window that you want to record. It will start the recording with a 10 second delay.
You can find the animated GIF in your Pictures folder.
There is also another script that allows you to capture a region instead of a window. However, for that to work, it requires xrectsel which you have to download, compile and install manually. For that, we won’t cover the script in this tutorial.
Byzanz is not the only tool that can record your screen as an animated GIF, but it is one that can do the job well. While it doesn’t come with a beautiful user interface like other screen recording tools for Windows and Mac, being a command line tool gives it the benefit of customizability and can be easily extended with simple bash script.