One of my system administration scripts does a bunch of stuff to a shared filesystem, and then goes to each of the other hosts (in parallel) to tell each one to update their metadata. Unfortunately, when something goes wrong, its output comes back to the primary machine, and I don't know where it's coming from for further diagnosis. So, a short script:

#!/usr/bin/perl use strict; use warnings; my $tag = shift; while (<>) { print "$tag: $_"; }
Then, my admin script looks like this:
for host in foo bar baz do ssh $host "update-metadata" 2>&1 > /dev/null | taglines $host & done
Now I know when bar has a problem that foo and baz don't: the problem lines all show up with "bar: " in front. I've also discarded stdout, and only tag stderr. This comes in very handy for me. Nothing really novel about the code - it's all fairly obvious (and I'm sure someone'll show it as a one-liner). It's more of another way to approach a problem that perl makes really, really trivial.

Update: to answer Fletch's question: because the update-metadata tools create FAR too much ugly noise on stdout, and I run this all via cron, so I'll just end up with ugly-lookin' emails. But, that's my use-case. I can see where your solution would come in handy, too, though at that point I'd likely use perl to kick off the ssh, too, and use IPC::Open3 to capture and label everything. rewrite.pl -o "$host O: " -e "$host E: " -- ssh $host "update-metadata" would be the interface... but it's no longer just a short snippet of code :-)

Replies are listed 'Best First'.
Re: Tag lines
by jdporter (Paladin) on Dec 24, 2008 at 19:31 UTC
    ... | perl -pe "BEGIN { $a = shift } s/^/$a: /" $host

      <nitpick>Shouldn't you use single quotes there, to prevent a possible shell variable $a from being interpolated?</nitpick>


      Life is denied by lack of attention,
      whether it be to cleaning windows
      or trying to write a masterpiece...
      -- Nadia Boulanger

        Good catch. You're absolutely right for UNIXish shells. But for cmd.exe (MS Windows), the double-quotes are necessary and safe: single quotes won't work, and things like $a are not seen as variables.

        Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.
Re: Tag lines
by Fletch (Bishop) on Dec 24, 2008 at 19:49 UTC

    Why toss when you can label? Shell fu FTW.

    for host in foo bar baz ; do { ssh $host echo 'I R OUT' \; echo 'I R ERROR' 1\>\&2 } > >( perl -l +ne "print qq{$host O: \$_}" ) 2> >( perl -lne "print qq{$host E: \$_} +" ) done

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.