Subscribe to Windows IT Pro
October 16, 2006 12:00 AM

A Tool Harness

Automate your command-line tools with this simple shell script
Windows IT Pro
InstantDoc ID #93531
Rating: (1)

I've written a homegrown tool that automates other command-line tools by using input data from a text file. My tool is a simple, one-line shell script that works on Windows, Linux, and Mac OS X with just a few modifications.

How It Works
Let's say you run a firewall report of the top outgoing activity destinations by users of your network. Unfortunately, the firewall doesn't perform the IP address-to-name lookup function and instead simply gives you a list of IP addresses and the number of connections initiated for each address, similar to the information in Table 1.

This report would become much more interesting (and useful) if the IP addresses were resolved to domain names. Then it would reveal at a glance which users were visiting a portal, streaming music, accessing Web mail, or conducting other types of Internet-based activities.

Several common command-line tools such as Whois, dig, and Microsoft's NBTStat help you resolve IP addresses to their owners, DNS names, and Windows NetBIOS names, respectively. It's easy to run Whois for a single address. For example, typing

whois 66.102.7.99 

shows you who owns that IP address. The tool takes a few seconds to run and you need to scroll through the output to find the data you want.

To automate the process of reading a list of objects, such as our firewall-report IP addresses, from a text file and processing each object through the Whois command, you can write a wrapper. The first step is to copy the IP addresses into a text editor, one per line,

66.102.7.99 

216.239.63.83

66.94.230.34

207.68.183.35

17.112.152.32

66.102.7.104

66.102.7.147

and save the entries as a text file, with a name like data.txt.

The next step is to create a short script that reads the file, loops through each item in the file, executes a command targeting each item, and manipulates the output to improve the presentation. Many systems administrators use Linux and Mac OS X as well as Windows, so fortunately, it's easy to port this useful script between platforms. We'll look at the Bourne-Again Shell (BASH) version of the script first and then the same script as a Windows shell script.

Related Content:

ARTICLE TOOLS

Comments
  • Marc
    5 years ago
    Jan 19, 2007

    Very useful in spurring me to examine in more depth the power of scripting to speed up investigations involving large log files. I knew a little bit but the article is good kick in the butt to redouble mastering the various scripting tools.

    Thanks

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.