Perl Training Logo: Praying Mantis
Perl for Unix System Administration

Perl as opposed to Shell
Traditionally, Perl was developed as a means to bind together various UNIX System administrator tools. Therefore, many of Perl's built-in features are strongly tied to the UNIX Operating System. Many administrative tools of the modern UNIX or UNIX-like operating systems are written in Perl.

Perl's platform independent interface
While there are certain cases when the various Unix shells give you a shorter way to do things, once you start writing shell scripts that are longer than 4-5 lines or if you need to do something that is not strictly sysadmin work, Perl will be a better choice.
One of the difficulties of administrating several different versions of Unix and Linux systems is the difference in both the options and the output format of the various tools. Just think about ps. Perl provides a platform independent interface to most of the standard Unix commands.
In general compared to the various shell languages, Perl is a much more powerful.

Perl on different platforms
When writing a shell script one is actually using the shell itself (e.g. tcsh, sh, bash) which is a small language with many external calls (ls, ps, do, cat, grep, awk, sed,...). Both the parameters and the output of these commands can vary between various Unix flavors and even between different versions of the same Unix. Even the location of these commands is not standard. So when moving a shell script written on SunOS to AIX or Linux or even a modern Sun Solaris, several issues will have to be fixed.
Most of these issues can be avoided when using Perl.

No subprocesses
Most tasks can be performed faster and easier when writing in Perl. Perl is a more secure language than shell. If written correctly, Perl uses only one process and does not require splitting several processes in order to accomplish your task, thus using less resources.

Perl as the super shell language
Perl is available on most modern UNIX operating systems. Perl is a superset of, sed, awk, grep, sort etc., it is often called a super shell language.
Perl offers a "sed to perl" and an "awk to perl" translator so if you are familiar with these languages you can get up-to-speed with perl faster.

Examples from our course

Parse Apache log file In many cases you will need to parse a log file and generate some report based on the data in that file. In this example we take a standard web server log file from the Apache web server and generate a report - number of hits per client IP address.

Rename many files at once. In the example we rename all the xml files in the current directory to have .html extension. In other cases you will have to rename or move or copy files based on much more complex rules. The given script gives you a quick start, how to do this.

Traverse file system and change every file based on some definition. In the given example we change the Copyright notice in every .pl file we encounter in the given tree. Obviously there are more complex tasks that you might need to do on every file in a directory structure. On Unix you can usually do similar tasks by running find .... | xargs do_something but this will execute the do_something for every file. Sometimes that's just unnecessary overhead in other cases it is impossible to use this as you need to maintain information between calls.

Create UNIX user account
When creating new user accounts on unix systems one often faces the requirement to all kind of specific configurations. Just to see a very simple issue, in one organization the policy is that when a person called Foo Bar arrives, she needs to have a user account bfoo.
There are many ways to extend the script:

  • Also create an e-mail alias bar.foo@corporate.com
  • Add to specific UNIX groups
  • Add to the Samba configuration file
  • Create NFS shares

Backup/Restore (example coming soon).

Monitor User behavior especially disk usage
A generic script to check available disk space.
Another one to check and report disk space usage on the mail server.
A simple version of du reporting diskspace use ordered by directory size.

Allow anyone to view this report via web interface (example coming soon).
Here is a generic example on how to create an HTML document using plain Perl script via CGI.

Send report on top 10 disk users to head of IT department (example coming soon).
It is not enough to see a report via the web interface, the head of IT department also wants to receive e-mail notification with the top 10 users. (In terms of diskspace usage). While we don't have that specific example in the training, there is an example on how to send e-mail from a script.

Collect log files from several system via ssh (example coming soon).

Reading an Excel file. This is not a standard Unix sysadmin task there are cases when you are given an Excel file (a real one, not just a csv file) and you need to extract information from it. All that while running on a UNIX or Linux machine.

Writing an Excel file. (example coming soon).
Many times we are required to create reports in text format. That's easy with Perl, that's why Reporting is part of one of the Perl name explanations (Practical Extraction and Reporting Language). Occasionally though we are required to create reports in other formats such as HTML or even Microsoft Excel. You could either drag all your data to a Windows machine, or use Open Office but in both cases you are into manual creating the report. This examples shows how to generate an Excel file while running on a UNIX or Linux machine.

Database access. In the given example we access SQLite database but it is just as easy to access Oracle, DB2 or any other relational database. Once you can fetch records you can start generating reports as requested by your managers.

Interface to LDAP. In systems, especially those built on Microsoft Active Directory your way to access user information will be vi LDAP. LDAP is certainly not something you could give the average user to try to figure out. It is hard for seasoned system administrators as well. Using the Net::LDAP module, as seen in the above example you could easily give a command line or even a web based interface to the specific needs of your organization.

To get started with Perl, we recommend our Fundamentals of Perl course.

Other useful resource are the following books:

Articles:

You can also read the full material of the Fundamentals of Perl course on-line.