ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


FreeBSD Basics

Useful Unix Utilities

07/19/2001

Whenever I get a chance to roam through the ports collection, I always end up feeling like a kid in a candy store.

At the moment, there are nearly 5,500 ports to choose from and they're all just a make install clean away. In today's article, I'd like to concentrate on some of the more useful ports I've come across in the last month or so, but I'm sure I'll also wander a bit into the less-than-useful-but-worth-trying-out-anyways ports as well.

As a reminder, if you find a port you're interested in, become the superuser, cd into the directory of the port you wish to build, and type make install clean. If you use either csh or tcsh as your shell, type rehash when the build is finished so the new executable will be in your path. If you're curious as to where the executable was installed, which name_of_port should reveal the path.

As any of my students can attest, I'm a stickler for ensuring they know how to quickly calculate anything possible in IPv4 addressing land. From subnet masks to CIDR notation to broadcast IDs to Cisco wildcard bit masks, they end up learning far more about TCP/IP than they could have anticipated. If you're a bit rusty on your calculations, there are several utilities in the ports collection that deal with subnet masks and IPv4 addressing.

The whatmask utility in the net section is a very quick way to see a subnet mask in CIDR notation, regular notation, or its wildcard bit mask equivalent. (Wildcard bit masks are used in Cisco access lists.) If you just type whatmask, you'll see that it uses a very basic syntax:

whatmask

whatmask version 1.0, Copyright (C) 2001  Joe Laffey <joe@laffeycomputer.com>
Visit: http://www.laffeycomputer.com/software.html for updates.

whatmask comes with ABSOLUTELY NO WARRANTY; for details see the COPYING file
that accompained this distribution. This is free software, and you are welcome
to redistribute it under the terms of GNU PUBLIC LICENSE.

Usage:   whatmask <CIDR bits>
  - or -
         whatmask <subnet mask>
  - or -
         whatmask <wildcard bit mask>

Note: whatmask will autodetect the input and show you all three.

I'll try the following:

whatmask 255.255.255.224

---------------------------------------------
       TCP/IP SUBNET MASK EQUIVALENTS
---------------------------------------------
CIDR = .....................: /27
Netmask = ..................: 255.255.255.224
Wildcard Bits = ............: 0.0.0.31

Very short and sweet, and this can save a few brain cells when you're in a hurry and don't feel like doing math. If you also need to determine the subnet IDs, broadcast addresses, and host IDs provided by a subnet mask, you can save yourself some calculations using the ipcalc utility, also found in the net section of the ports collection. Again, type ipcalc for its syntax:

ipcalc
Usage: ipcalc [-n|-h|-v|-help] <ADDRESS>[/]<NETMASK> [NETMASK]

ipcalc takes an IP address and netmask, and calculates the 
resulting broadcast, network, Cisco wildcard mask, and host 
range. By providing 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    Don't display ANSI color codes
 -b    Suppress the bitwise output
 -h    Display results as HTML
 -help Longer help text
 -v    Print Version
 
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

I personally found the ANSI color codes to be ugly, so I used -n when I tried the following example:

ipcalc -n 192.168.10.40/27

Address:   192.168.10.40         11000000.10101000.00001010.001 01000
Netmask:   255.255.255.224 == 27 11111111.11111111.11111111.111 00000
Wildcard:  0.0.0.31              00000000.00000000.00000000.000 11111
=>
Network:   192.168.10.32/27      11000000.10101000.00001010.001 00000 (Class C)
Broadcast: 192.168.10.63         11000000.10101000.00001010.001 11111
HostMin:   192.168.10.33         11000000.10101000.00001010.001 00001
HostMax:   192.168.10.62         11000000.10101000.00001010.001 11110
Hosts/Net: 30                    (Private Internet RFC 1918)

A utility with a similar function but slightly different output is cidr, also in the net section. It has a fairly long syntax, so I'll snip a bit of its output:

cidr
<snip>

Short form:

cidr <ipaddress/prefix> [-H]

Note: Short form only supports dotted-quad IP address and
      decimal(integer) prefix.  Host list ("-H") is optional.
<snip the Long form section>

And I'll try out the same example:

cidr 192.168.10.40/27

ip address..........:  192.168.10.40
netmask.............:  255.255.255.224

network address.....:  192.168.10.32
broadcast address...:  192.168.10.63

please wait while host addresses are validated...

total host addresses:  30

If I had included the -H, it would have also printed out each host IP address on a separate line. That output might be handy to send to a printer if I was making an inventory of a network.

The last related utility I'll demonstrate is ipsc found in the sysutils section of the ports collection:

ipsc
usage: ipsc [options] <addr/mask | addr/offset | addr>
    -C <class>  Network class (a, b, or c).  Must be used with -B
	-B <bits>	Subnet bits (must be used with -C)
    -i <if>		Reverse engineer an interface (e.g. eth0)
	-a          Print all information available
	-g          Print general information
    -s          Print all possible subnets
	-h          Print host information
	-c          Print CIDR information
    -v          Print the program version
    -?          Print this help message

                Report bugs to dan@vertekcorp.com

The most interesting feature of this utility is the -i option. Let's see what happens when I use it and specify my outbound NIC:

ipsc -i ed1
Network class:          A         
Network mask:           255.0.0.0 
Network mask (hex):     FF000000  
Network address:        24.141.116.0
Subnet bits:            14        
Max subnets:            16384     
Full subnet mask:       255.255.252.0
Full subnet mask (hex): FFFFFC00  
Host bits:              10        
Hosts per subnet:       1024      
Bit map:                nnnnnnnn.ssssssss.sssssshh.hhhhhhhh

This utility also gives a rather exhaustive output when you use the -a switch. I'll do that with the same example I used for the other utilities:

ipsc -a 192.168.10.40/27
Network class:            C         
Network mask:             255.255.255.0
Network mask (hex):       FFFFFF00  
Network address:          192.168.10.32
Subnet bits:              3         
Max subnets:              8         
Full subnet mask:         255.255.255.224
Full subnet mask (hex):   FFFFFFE0  
Host bits:                5         
Hosts per subnet:         32        
Bit map:                  nnnnnnnn.nnnnnnnn.nnnnnnnn.ssshhhhh

Subnet 1:                 192.168.10.0     192.168.10.31    
Subnet 2:                 192.168.10.32    192.168.10.63    *
Subnet 3:                 192.168.10.64    192.168.10.95    
Subnet 4:                 192.168.10.96    192.168.10.127   
Subnet 5:                 192.168.10.128   192.168.10.159   
Subnet 6:                 192.168.10.160   192.168.10.191   
Subnet 7:                 192.168.10.192   192.168.10.223   
Subnet 8:                 192.168.10.224   192.168.10.255   

IP address:               192.168.10.40
Hexadecimal IP address:   C0A80A28  
Address allocation range: 192.168.10.32 - 192.168.10.63
Full subnet mask:         255.255.255.224
Subnet mask:              0.0.0.224 
Subnet ID:                0.0.0.32  
Network ID:               192.168.10.0
Host ID:                  0.0.0.40  

Cisco Wildcard:           0.0.0.31  
CIDR notation:            192.168.10.0/27
Route/Mask:               192.168.10.0 / 255.255.255.224
Hexadecimal route/mask:   C0A80A00   / FFFFFFE0
Supernet max:             0

Let's leave TCP/IP land and take a look at some utilities that deal with logs. If you're using Sendmail, you might want to take a look at mreport in the sysutils section of the ports collection. If you just type mreport, you'll receive a summary of your Sendmail log:

mreport
      [hostname] [/var/log/maillog]

[ 24] 101080 owner-freebsd-questions@FreeBSD.ORG genisis@localhost
[  3]  11067 owner-freebsd-security@FreeBSD.ORG genisis@localhost
[  2]   7858 firewall-wizards-admin@nfr.com genisis@localhost
[  2]   6407 owner-freebsd-newbies@FreeBSD.ORG genisis@localhost
[  1]   6158 owner-freebsd-advocacy@FreeBSD.ORG genisis@localhost
[  2]   3913 root                           root
[  1]   3415 tlangdon@atctraining.com.au    genisis@localhost
[  1]   3176 shrdlu@cnchost.com             genisis@localhost
[  1]   2378 listserv@ugu.com               genisis@localhost
[  1]   2183 giuliomaria.fontana@sinapsi.com genisis@localhost

=====================
Total Bytes         : 147635
Number of Records   : 10
---------------------
Host Name           : hostname
Input File          : /var/log/maillog
Output File         : stdout
First Record        : Jul 15 01:30:51
Last Record         : Jul 15 09:35:51
---------------------
Time Taken          :  10729 µs
=====================
mreport-0.9 by Jason Armstrong

If you read the man page for this utility, you'll see that the number in the [] represents the number of emails, followed by the size in bytes; who the email is from; and who the email is destined for. There are also switches to customize how the output is sorted. Several examples are cited in the man page to get you started.

Another utility in the sysutils section that deals with logs is colorize. Once you've finished building this port, you'll have to do the following as the superuser so the colorize utility can read its configuration file:

cp /usr/local/etc/colorizerc.sample /usr/local/etc/colorize

This file describes which colors are associated with which key terms and can be edited to your preference. You can also apply various attributes such as "bold" or "blink" to key terms.

To actually use the utility, use the < redirector to receive input from the desired log file. Again, you'll have to remain the superuser due to the permissions that are set on log files. For example, to view the security log:

colorize < /var/log/security

Unfortunately, you can't send output to a pager utility and there isn't a switch to page the results. You can see the beginning or end of a large log if you pipe the results to either head or tail like so:

colorize < /var/log/security | head
colorize < /var/log/security | tail

One of the more interesting available switches allows you to create a colorized HTML version of a log. Here I'll create a page called security.html and send it to my home directory:

colorize -h < /var/log/security > ~genisis/security.html

I wasn't able to view the colors using Lynx, but the output looked very nice in Galeon and Netscape.

While I was in the sysutils section of the ports collection, I also built the stat utility which can be used to print out inode contents. If you remember back from "Understanding File System Inodes," every file has a unique inode entry which contains its inode number and its metadata. Let's use the stat utility on one of the files in my home directory which is called unix:

stat unix
  File: "unix"
  Size: 10800 Allocated Blocks: 22 Filetype: Regular File
  Mode: (0644/-rw-r--r--) Uid: (1001/genisis) Gid: (0/wheel)
Device: 160773 Inode: 912755 Links: 1
Access: Thu Jul  5 07:51:42 2001
Modify: Thu Jul  5 07:53:29 2001
Change: Thu Jul  5 07:53:29 2001

There are several nifty utilities in the textproc section that deal with handling documents in various formats. I'll take a look at ascii2pdf, word2x, and catdoc.

The utility ascii2pdf can quickly convert an ACSII file to PDF format. To use, simply specify the name of the file you wish to convert; it will create a second file with the same name but with a .pdf extension. To view the new PDF file, you can use acroread from the print section of the ports collection.

Another handy conversion utility is word2x which can convert a Word 97 document to ASCII, HTML, or Latex. I have to admit that I've done a pretty good job of training my friends not to send me Word documents and I had to go out on the Internet to find one to test out this program. I figured the best place to find one would be at Microsoft's web site, so I did a search for "*.doc" and downloaded the first document I found, it happened to be called Core_cal.doc. I then ran the utility like so:

word2x Core_cal.doc

Within a second, I had my prompt back and a new file called Core_cal.txt. When I sent this file to the more utility, I was pleasantly surprised to see that it was in a legible format.

Very handy utility indeed. Even though the man page for this utility didn't specify how, I tried to see if I could also create an HTML file using the format switch like so:

word2x -f html Core_cal.doc

Again, a new file was created called Core_cal.html which displayed quite nicely in my web browsers. Being curious, I also ran the file utility:

file Core*
Core_cal.doc:  Microsoft Office Document
Core_cal.html: exported SGML document text
Core_cal.txt:  ASCII English text

If you just want to read a Word document without converting it, try catdoc. I could read that original Word document like so:

catdoc Core_cal.doc |more

Or, if I wished to convert it to ASCII, I could redirect the output to another file:

catdoc Core_cal.doc > Core
more Core

I guess I won't have to grumble to myself anymore the next time I come across a Word document.

To end today's article, let's take a look at some slightly less useful utilities. The uf-view utility in the misc section has a nice little interface to view various online cartoons. For some reason, I was unable to get this utility to fetch the "User Friendly" cartoon, but I was able to easily scroll through several years worth of "Calvin & Hobbes" which more than made up for that glitch in my books. This utility will also let you view the Penny Arcade, Mega Tokyo, Little Gamers, and Angst Technology comic strips.

Another utility that falls into the easily amused category is oneko in the games section. You may want to start it from xterm by typing /usr/X11R6/bin/oneko; this way, if you get tired of watching the antics of the kitten as it follows your mouse, you can quickly kill it from xterm. A similar utility in the games section is xneko, but I found it rather cruel to watch the kitten try to scratch its way out of its box in order to chase the mouse. Your mileage may vary.

Finally, try out the cowsay utility in the games section. Read the well-written man page and see the care that was taken when this utility was created. Try each of the numerous switches. As you do so, you'll probably shake your head in wonder like I did and ask yourself, "Why?" The only answer I could come up with is "Why not?"

Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.


Read more FreeBSD Basics columns.

Return to the BSD DevCenter.

Copyright © 2009 O'Reilly Media, Inc.