Pipe to Perl for the Quick and Dirty Big Jobs

I always find myself using Perl for systems administration tasks, particularly when you must do things on many systems at once. It is in Perl’s DNA to be down in the trenches of the operating system.

For example, last night I had an NTP configuration file I needed to get out to a few dozen machines. I had a list of them from a display of managed machines, and they were tabulated like this:

62 comp1.dom.com 8 Workstation 6 Company, Inc.
61 comp2.dom.com 8 Workstation 6 Company, Inc.
63 comp3.dom.com 8 Workstation 6 Company, Inc.

And I need to get an NTP configuration file to each. Sure, you can write a shell script using sed to do it just as easy, but with Perl you do get a little more flexibility to do the strange things that must be done from time to time.

#!/usr/bin/env perl

foreach (<STDIN>) {
    my($id, $host, $ignore) = split;
    print `scp -v ntp.conf root\@$host:/etc`;
}

Of course, if you like to live dangerously, you can always put in full ssh commands. You can make the command you want to run be an argument. You can use any of the numerous Perl modules available to handle specific things or concerns.

The command for this is something like:

list_hosts | my_perl_program

Perl just lets you read from STDIN as a normal pipe, with nothing extra you have to do. Columnar/tabular data – it’s so easy. Calling a shell command and sending the output anywhere? Right there.

Edit that to reload your NTP daemon on each. Or make the command an argument when you call the program. Or an option, using Getopt::Long, or whatever you like.

Anyway, my suggestion here, start using some Perl. For admin purposes, it’s like the shell, on multi-dimensional steroids that don’t cause cancer.

Leave a Reply

Your email address will not be published. Required fields are marked *