Use SMIT to learn which AIX commands are executed to complete certain tasks. Discover how those commands can be redirected to a different file, then used in your own scripts.
If you're an AIX system administrator, it's important to have the ability to write scripts to automate repetitive tasks. One way to learn scripting is to review the System Management Interface Tool (SMIT) logs. When you use SMIT to perform an administrative task on your AIX box, it writes to three logs:
- smit.log: This log contains all the menu options and commands that executed, along with their output.
- smit.script: This log contains the shell commands that were executed.
- smit.transaction: This log documents the dates when each command was executed. It primarily provides an audit trail for SMIT.
Reviewing these three logs is an excellent way to learn what SMIT does in the background—particularly what commands it executes for different tasks.
The smit.script log is the most helpful of the three logs because the commands are presented in a clear manner. You can copy a command from this log and paste it into your own script. If you're working in a large environment containing many AIX boxes, you can even connect to remote boxes and execute the command through a Secure Shell (SSH) script. This is a fairly simple process—let's first see how you can use the command within a script to be run on the local machine.
Creating a Script for the Local Machine
One of the best ways to learn which commands get called to perform a task is to perform that task in SMIT and see the commands it uses. For some tasks (e.g., removing devices), actually performing the task isn't feasible. Fortunately, SMIT lets you log the generated commands without executing them. You can also specify where to write those commands. The two flags you use are x, which will log but not execute the commands, and s, which will write the commands to the specified file.
For example, suppose you want to see the commands SMIT uses to get a list of Ethernet adapters, but you don't want the commands to actually run. Instead, you want the commands redirected to the file listdev.txt. First, you run the command:
Next, in SMIT, you select Devices, Communication, Ethernet Adapter, Adapter, List all Ethernet Adapters and press Enter. As Figure 1 shows, SMIT will then inform you that the command has been logged because the x flag was used.
If you look at the contents of the listdev.txt file using the command
you'll see the command that SMIT generated to get the list of Ethernet adapters and when it was generated:
lsparent -C -k ent
To execute listdev.txt as a script, you just need to change the permissions and run it:
The results will look like this:
ent1 Available Virtual I/O Ethernet Adapter (l-lan)
Creating an SSH Script
Using the same command, you can create an SSH script to list all the Ethernet adapters on your remote boxes, assuming SSH is installed. The example below shows a sample SSH script named get_ether.
cat /usr/local/bin/servers.txt | while read host
ssh -T -t -l root $host<<'mayday'
echo " $myhost "
lsparent -C -k ent
This script assumes the keys have been exchanged. In get_ether, I copied and pasted the command into the "here statement" block between the mayday tags. It's that easy.
The get_ether script retrieves the names of the remote servers from the servers.txt file. When you execute the script, it will loop through servers.txt. For each remote server listed, it will connect to the box, execute the lsparent command, and send the results to standard output. Sample output is below:
ent0 Available 09-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent1 Available 09-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent2 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent0 Available Virtual I/O Ethernet Adapter (l-lan)
ent1 Available Virtual I/O Ethernet Adapter (l-lan)
To save the output to a file, simply redirect it like this:
As you can see, generating the Ethernet listing command from SMIT and using that command in scripts is pretty straightforward.
Walking Through Another Example
Using the same principles, let's generate another command using SMIT. This time, the command will create a user. First, run the following to capture rather than execute the generated commands and write them to a file named user.txt:
Then, in SMIT, select Security and Users, Users, Add a User, and enter the user attributes. To see the contents of user.txt, run the command
The commands that were captured in user.txt when I created the user voop are as follows:
for i in "$@"
if [ "$i" = "admin=true" ]
eval mkuser $SET_A $LIST
x pgrp='staff' groups='staff' su='false' gecos='apps support' histsize='5' histe
xpire='1' maxage='1' minage='5' minlen='6' voop
Notice that a function named x is called with parameters containing the user's attributes set in the create user screen in SMIT.
To execute user.txt as a script, simply change the permissions and run it:
You can confirm that the user has been created with the specified attributes by using the lsuser command. In my case, I'd run:
Excerpts from the results are below:
loginretries=0 pwdwarntime=0 account_locked=false minage=5 maxage=1 maxexpired=-1
minalpha=0 minloweralpha=0 minupperalpha=0 minother=0 mindigit=0 minspecialchar=0 mindiff=0 maxrepeats=8 minlen=6 histexpire=1 histsize=5 pwdchecks= dictionlist= default_roles=
Another way to confirm that the user is created is to grep the /etc/passwd file. In my case, I'd run
which produces the results:
Adding Error Checking
IBM has tested AIX's SMIT for many years; however, things can go wrong that might be out of its control. For example, the commands to create a new user would probably fail if /home or /tmp is full. As you probably already know, AIX will report an error if a command fails. However, it's good practice to include some basic error checking when creating scripts from commands generated by SMIT.
For example, you can check for the last exit status. The block structure for the last exit status could be:
echo ″it failed″
echo ″it worked″
You can use more solid error checking, such as the check_error function shown below:
# check_error example
# $1 = last exit status of cmd parsed
# $2 = the cmd parsed
if [ "$1" != "0" ]
echo "oop's.. Error occured on: $2"
# Use below to redirect any errors to the variable cmd as well
# not recommended though
# cmd=$( lsuser dxtans 2>&1)
if check_err $? $cmd
echo "It worked, so do something else now"
echo "command failed !!"
With this function, there's no repeating code to check for errors and thus no redundancy. The function can be called for any command execution. In this example, the lsuser dxtans command is executed.
Well Worth the Time
It's well worth your time to play around with SMIT using those x and s flags. Review the commands that are placed in the smit.script file. Look and learn from them. This is a great way to enhance your AIX knowledge. Before long, you'll probably only use SMIT occasionally to run commands, because you'll have all the common commands and parameters stuck in your head. I state this with confidence because this is how I learned most of the AIX administrative commands when I first got into AIX.