Networker

Re: [Networker] How can I do this in perl?

2004-11-12 12:44:09
Subject: Re: [Networker] How can I do this in perl?
From: George Sinclair <George.Sinclair AT NOAA DOT GOV>
To: NETWORKER AT LISTMAIL.TEMPLE DOT EDU
Date: Fri, 12 Nov 2004 12:46:31 -0500
Thanks, guys. This helps a lot. I was screwing around with this the
other day, and like I said, I tried all kinds of pipes and other stuff
and was just getting frustrated trying to mimic the Perl end of things
and getting no where, so I figured why waste any more time when I can
ask you guys. LOL!

Since this script will be receiving information that is passed in, and
may use this information to construct
certain variables, I will most likely turn taint mode on for security
reasons. In the past, because many of my script variables are considered
tainted, this causes problems when using various Perl commands (e.g.
open, chmod, chdir, chown, chgrp, etc.). Untainting these doesn't seem
to always help. In the past, to get around this problem, I have used
system command with the necessary arguments in a list to avoid calling
the shell. I presume the user knows what he's doing. In one case, I must
be able to capture the output that the system command would normally
return which is not possible. To avoid using backticks (insecure) or
other screwing around, I would have in the past used exec with a pipe to
allow the parent to read the results from the child. Sometimes I've used
sysopen.

Here's a portion of code I used before for something different:


    undef(@args);
    # Construct a list of arguments for the system command, so we can
safely bypass the shell.
    @args = ("$RSYNC_COMMAND", "$RSYNC_ARG1", "$data_dir",
"$target_dir");

    # Rsync the source data (data_dir) to the target (target_dir).
    system(@args) == 0 or die "system @args failed: $?";

    if ($DIR_LIST)  # Create a directory listing.
    {
       # Parse out the actual directory from the data directory.
       $dir = basename($data_dir);

       undef(@args);
       # Construct a list of arguments for the system command, so we can
safely bypass the shell.
       @args = ("$DIR_LIST_COMMAND", "$DIR_LIST_ARG1",
"$target_dir/$dir");

       # Create a recursive directory listing on data directory using
DIR_LIST_COMMAND.
       # NOTE: We could use the system command, or possibly chdir, to
'cd' to the 'about'
       # directory to run the DIR_LIST_COMMAND, but that's awkward, and
we may be modifying the output (e.g.
       # removing the parenthesis around the file name), so we need to
be able to capture the output. The
       # system command will not allow us to do this, and we don't want
to use backticks for security
       # reasons. As a result, we use exec (child) and a pipe to read
(parent) the output that the child
       # execed.
       $result = open(CHILD, "-|");
       die "Couldn't open pipe to subprocess" unless defined($result);
       exec(@args) or die "Can't exec @args: $!" if ($result == 0);

       undef(@output);
       $i = 0;
       while(<CHILD>)
       {
          chomp;
          $i++;
          push(@output,$_) if ($i>4); # Skip the header lines (first 4
lines) from the MD5
listing.
       }
       close(CHILD);

       # Create and open the output file for writing, create new file if
needed or else truncate
       # old file. Regular open command will NOT work in taint mode.
       sysopen (OUT, "$dir/version/dir.lst", O_WRONLY|O_TRUNC|O_CREAT)
|| die "Can't
open '$dir/version/dir.lst' for writing: $!";

       # Loop through the MD5 listing output.
       foreach $str (@output)
       {
           Blah-Blah-Blah ......

George

"Reed, Irene" wrote:
>
> I am sorry that the spacing got off!  Let me know if you want the file
> and I will send it out.
>
> Irene
>
> -----Original Message-----
> From: Legato NetWorker discussion [mailto:NETWORKER AT LISTMAIL.TEMPLE DOT 
> EDU]
> On Behalf Of Reed, Irene
> Sent: Wednesday, November 10, 2004 9:22 PM
> To: NETWORKER AT LISTMAIL.TEMPLE DOT EDU
> Subject: Re: [Networker] How can I do this in perl?
>
> Here is a perl snippet I use where I write it all out to a file first
> and then pipe it to the nsradmin command.  I can send you the entire
> script if you want it.
>
> Irene Reed
>
> # Now we will make the change to the legato client.
>  # We will create the input file for the nsradmin -i command
>  # The following will edit the clients with schedule AIX_NT_Incr (sch1)
>  open (FSOUT1, ">/tmp/legfs1$$") || die "Could not create the file for
> $sch1 with nsradmin | $!";
>  print FSOUT1 "show save set\n";
>  print FSOUT1 "print type: NSR client; name: $hsfq; schedule: $sch1\n";
> print FSOUT1 "update save set: " . join(", " , @fsnodumps) . "\n";
>
>  close (FSOUT1);
>
>  open (Q1, "/usr/bin/nsradmin -s $server -i /tmp/legfs1$$ 2>&1|") || die
> "Could not run nsradmin | $!";
>  close (Q1);
>  unlink ("/tmp/legfs1$$");
>
>  # Now we will make the change to the legato client.
>  # We will create the input file for the nsradmin -i command
>  # The following will edit the clients with schedule DBDumps_Incr
>  open (FSOUT2, ">/tmp/legfs2$$") || die "Could not create the file for
> $sch2 with nsradmin | $!";
>  print FSOUT2 "show save set\n";
>  print FSOUT2 "print type: NSR client; name: $hsfq; schedule: $sch2\n";
>  print FSOUT2 "update save set: " . join(", " , @fsdumps_sort) . "\n";
>
>  close (FSOUT2);
>
>  open (Q2, "/usr/bin/nsradmin -s $server -i /tmp/legfs2$$ 2>&1|") || die
> "Could not run nsradmin | $!";
>  close (Q2);
>  unlink ("/tmp/legfs2$$");
> }
>
> -----Original Message-----
> From: Legato NetWorker discussion [mailto:NETWORKER AT LISTMAIL.TEMPLE DOT 
> EDU]
> On Behalf Of Darren Dunham
> Sent: Wednesday, November 10, 2004 5:47 PM
> To: NETWORKER AT LISTMAIL.TEMPLE DOT EDU
> Subject: Re: [Networker] How can I do this in perl?
>
> > How can I do the equivalent of this shell script in perl?
> >
> > #!/bin/sh
> > /usr/sbin/nsradmin -s server -i <<EOF
> > . type: NSR $1; name: $2
> > print
> > EOF
>
> There are several ways.
> #1 most like your shell..
>
> #!/usr/bin/perl
> system ("/usr/sbin/nsradmin -s server -i " . <<EOF);
> print type: NSR $ARGV[0]; name: $ARGV[1]
> EOF
>
> #2
>
> #!/usr/bin/perl
> open (ADM, "|/usr/sbin/nsradmin -s server -i") or
>    die "Couldn't open pipe to nsradmin.\n";
>
> print ADM "type: NSR $ARGV[0]; name: $ARGV[1]\n";
>
> Neither of those solutions capture the output.  It just goes to STDOUT
> and the screen. Something like...
>
> my @reply = `/usr/sbin/nsradmin -s server -i " . <<EOF`;
> print [whatever....]
> EOF
>
> would return the data in @reply.
>
> > where I'm just printing out the info to standard output and $1 and $2
> > are passed to the script and would be something like 'client' and
> > 'client_name', or 'group' and 'group_name', etc. I tried all kinds of
> > open commands with pipes, etc. Nothing seems to work.
>
> --
> Darren Dunham                                           ddunham AT taos DOT 
> com
> Senior Technical Consultant         TAOS            http://www.taos.com/
> Got some Dr Pepper?                           San Francisco, CA bay area
>          < This line left intentionally blank to confuse you. >
>
> --
> Note: To sign off this list, send a "signoff networker" command via
> email
> to listserv AT listmail.temple DOT edu or visit the list's Web site at
> http://listmail.temple.edu/archives/networker.html where you can
> also view and post messages to the list. Questions regarding this list
> should be sent to stan AT temple DOT edu
> =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
>
> --
> Note: To sign off this list, send a "signoff networker" command via
> email
> to listserv AT listmail.temple DOT edu or visit the list's Web site at
> http://listmail.temple.edu/archives/networker.html where you can
> also view and post messages to the list. Questions regarding this list
> should be sent to stan AT temple DOT edu
> =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
>
> --
> Note: To sign off this list, send a "signoff networker" command via email
> to listserv AT listmail.temple DOT edu or visit the list's Web site at
> http://listmail.temple.edu/archives/networker.html where you can
> also view and post messages to the list. Questions regarding this list
> should be sent to stan AT temple DOT edu
> =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

--
Note: To sign off this list, send a "signoff networker" command via email
to listserv AT listmail.temple DOT edu or visit the list's Web site at
http://listmail.temple.edu/archives/networker.html where you can
also view and post messages to the list. Questions regarding this list
should be sent to stan AT temple DOT edu
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=