#!/usr/bin/env perl use warnings; use strict; use Net::OpenSSH; use Expect; die "Usage: $0 HOST" unless @ARGV==1; my $ssh = Net::OpenSSH->new($ARGV[0]); die $ssh->error if $ssh->error; my @cmd = ('/tmp/test.pl'); my ($pty, $pid) = $ssh->open2pty(@cmd) or die $ssh->error; my $exp = Expect->init($pty); $exp->log_stdout(1); $exp->expect(5, [ qr/foo .+\?/i, sub { my $exp = shift; # prompt user if ($exp->match=~/quz/i) { $exp->send(scalar ); } # automatic responses elsif ($exp->match=~/bar/i) { $exp->send("y\n"); } else { $exp->send("n\n"); } exp_continue; } ] ); $exp->soft_close; waitpid($pid, 0); $ssh->disconnect; print "Done.\n";