BSD DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Useful Unix Utilities
Pages: 1, 2

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.





Sponsored by: