my $ssh = Net::OpenSSH->new($remote_host); my $command = 'remote privileged command'; my $root_pw = 'IamNotTheRealPassword'; my ($pty, $pid) = $ssh->open2pty({stderr_to_stdout => 1}, 'su', '-') or die "Can't open PTY: $!\n"; my $expect = Expect->init($pty); $expect->expect(10, [qr/assword/ => sub { shift->send("$root_pw"); }] ) or die "Timed out\n"; $expect->raw_pty(1); $expect->spawn($command) or die "Can't execute command $command: $!\n"; $expect->expect(2); my $out = $expect->before();