#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Expect; select STDOUT; $| = 1; select STDERR; $| = 1; my $timeout = 5; my $password = ''; my $user = ''; my $host = ''; my $ssh = Net::OpenSSH->new(host=>$host, user=>$user, password=>$password); my ($pty, $pid) = $ssh->open2pty("sudo -k; sudo su - imail -c ls") or die "open2pty failed: " . $ssh->error . "\n"; my $expect = Expect->init($pty); $expect->raw_pty(1); $expect->expect($timeout, ':') or die "expect failed\n"; $expect->send("$password\n"); $expect->expect($timeout, "\n") or die "bad password\n"; while(<$pty>) { print "$. $_" }