use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new ( host => "remotehost.com", password=> 'mypass', user => 'user1', raw_pty => 1 ); # logon to the SSH server using those credentials. my $login_output = $ssh->login(); if ($login_output !~ /Last login/) { die "Login has failed. Login output was $login_output"; } $ssh->exec("stty raw -echo"); $ssh->send("pbrun mypbgroup user2=ksh"); $ssh->waitfor('Password:\s*\z', 3) or die "prompt 'Password' not found after 1 second"; $ssh->send("mypass"); $ssh->waitfor('user2_prompt', 3) or die "prompt 'user2_prompt' not found"; my $cmd1 = $ssh->exec('ls -l'); print ($cmd1); my $cmd2 = $ssh->exec('whoami'); print ($cmd2);