#!/usr/bin/perl -w
use Expect;

# connection parameters
$SERVER = 'Redhat9';
$USER   = 'krang';
$PASS   = 'krang';

# spawn the date command on $SERVER running as $USER
my $spawn = Expect->spawn(qq{ssh $USER\@$SERVER date})
  or die "Unable to spawn ssh.\n";
$spawn->log_stdout(0);

# provide the password when prompted, waiting up to 5 seconds
if ($spawn->expect(5, 'password:')) {
    $spawn->send($PASS . "\n");
} 
# wait for the date and print it out
if ($spawn->expect(5, -re => qr/^.*?\d{4}\r?\n/)) {
    print "The date on $SERVER is " . $spawn->match();
}

Example 3: get_date.pl gets the date via ssh.

Back to Article