#! /usr/bin/perl use Net::SSH::Expect; #Preparing for ssh session my $ssh = Net::SSH::Expect->new ( host=>"poseidon", user=>'testusr', timeout=>10, raw_pty=>1, password=>'wel123go', log_file=>"log.txt" ); #Logging in to the session $ssh->login(); print "Logged in \n"; # Commands executed $ssh->exec("stty raw -echo"); $whoami=$ssh->exec("whoami"); print "\$whoami : ",$whoami,"\n"; $ssh->send("su -"); $ssh->waitfor('Password:\s*\z', 1) or die "prompt 'Password' not found after 1 second"; my $pass = 'passw0rd'; $result = $ssh->send($pass); print "\$result : ",$result,"\n"; $whoami=$ssh->exec("whoami"); print "\$whoami : ",$whoami,"\n"; # Expect to see root #closing the session $ssh->close();