easy to make a typo. By turning this task into a script, I prevent the possibility of making a typo. Even though the sequence is only a few lines, it's worth having as a script.
How to Automate
To automate something, first you have to do it manually. Then you write code for each step. Next, you bring the little bits of code together, testing each addition as it is added. Finally, you test the entire system.
Step 1: Do It Manually
The first step to automating a process is to make sure you can do the process manually. Document each step, and make sure you can write code to do that step. Then put all the pieces together.
Many times a protege has come to me asking for help automating something. 'Oh, I've worked on this problem for hours! I'm completely stuck!' he'll say.
'OK,' I reply, 'show me how you would do this manually.'
'I don't know. I can't figure that out.'
'The root of your problem is just that, young padawan. Hmmm?'
As discussed in Chapter 12, one of the benefits of documenting a procedure is that writing down the steps is the key to being able to automate something. I wasn't kidding. In fact, when I don't have time to automate something, I write the step-by-step procedure on my Wiki telling someone else how to do the task. When I do that, I've accomplished two things. First, I've contributed to the documentation of how our system works. Second, I've actually performed the first step of automating the process!
Tip
Document the steps, then automate them. If you can't write down the steps, you'll never figure out how to automate them.
The process of writing down the steps forces you to identify all the steps. Unlike keeping all of the steps in your head, you can show the document to other people to have them verify the process.
If you don't have a Wiki, you can use paper and a pencil or a text file. Do the steps manually and record the steps. Any command that you type should be pasted into the text document.
Step 2: Code Each Step
Turn each step into something that can be done from the command line or within a short program. Test each step individually. That is, you might write a series of small scripts, each one verifying that the code you have for that particular step is correct.
If any step involves a graphical user interface (GUI), you must find the command-line equivalents. Some operating systems make this easy. For example, HP-UX's System Administration Manager (SAM) has a button you can press to output the command-line equivalent of the action it is about to perform. Mac OS X has Automator and AppleScript that let you automate processes normally done though the GUI. Windows has many different tools that are similar. However, tools that automate the clicking of buttons may not be as useful as directly setting various registry keys or LDAP entries.
Recommended books for Microsoft Windows administrators:
Recommended books for Unix/Linux administrators: