Sample Perl script

This sample is a Perl script that remotely submits a local JCL file from the z/OS system.

#!/usr/bin/perl -w
#
# Provided as-is by IBM for purpose of example. IBM provides no guarantee of quality.
#
# submit script to submit a local JCL on remote zOS machine
# with automatic login using a .netrc file.
#
# usage: submit [options[ <jcl filename>
# options:
#    --mvsjcl   MVS filename

use strict;
use Getopt::Long;
use Expect;

my $username;
my $password;
my $hostname = 'mvs';
my $input;

my $mvsjcl = 0;

#####################################################################
# process commandline
#####################################################################

GetOptions('mvsjcl!'    => \$mvsjcl,
           'hostname=s' => \$hostname,
          );

if ($#ARGV != 0)
{
    print <<USAGE_HELP;
buildusage: submit [options] <filename.jcl>

    Submit the specified local JCL file to MVS.

options:
    --hostname=<hostname>
                Specifies an alternate hostname instead of the
                default 'mvs'.

    --mvsjcl    Specifies that the JCL filename exists on
                MVS instead of the local filesystem. It is
                advised that you use the full path to the file.

examples:
    submit test.jcl
    submit ~myuserid/jcl/tpfcs.oldr.jcl
    submit --mvsjcl /u/myuserid/test.jcl
    submit --hostname=othermachine test.jcl

USAGE_HELP
    exit;
}
$input = $ARGV[0];


#####################################################################
# check for existance of file
#####################################################################

if (!$mvsjcl && ! -r $input)
{
    print "Local JCL file '$input' does not exist.\n";
    exit 1;
}


#####################################################################
# process the netrc file (as per TPF usage)
#####################################################################

if (! -r "$ENV{'HOME'}/.netrc")
{
    print "~/.netrc does not exist or is not readable.\n";
    exit 1;
}

open(FH, "$ENV{'HOME'}/.netrc") || die("could not open file");
while (<FH>)
{
    /machine\s+($hostname\S*)\s+login\s+(\S*)\s+password\s+(\S*)/;
    if ($1 && $2 && $3)
    {
        $hostname = $1;
        $username = $2;
        $password = $3;
        last;
    }
}
close(FH);

if (!$username || !$password)
{
    print "Could not find login information for '$hostname'.\n";
    exit 1;
}


#####################################################################
# Run the submit
#####################################################################

print "Submitting '$input'\n\tvia '$username\@$hostname'\n";

my $cmd = "rexec -n $hostname submit < $input";
if ($mvsjcl)
{
  $cmd = "rexec -n $hostname submit $input";
}

print "command is '$cmd'\n\tvia '$username\@$hostname'\n";

# send the command
my $exp = new Expect;
$exp->raw_pty(1);
$exp->spawn($cmd);
$exp->log_stdout(0);
$exp->expect(10,
    [ qr/Username/i =>
        sub { my $exp = shift;
              sleep(1);
              $exp->send("$username\n");
              exp_continue;
            }],
    [ qr/Password/i =>
        sub { my $exp = shift;
              sleep(1);
              $exp->send("$password\n");
              $exp->log_stdout(1);
              exp_continue;
            }],
    );
$exp->soft_close();
$exp->hard_close();


# Done!