#!/usr/bin/perl -w use strict; use Net::OpenSSH; use Expect; $Expect::Exp_Internal = 1; my $hostname = shift; my $username = shift; my $password = shift; my $path = shift; my $rootpassword = shift; # Lets Connect First my $ssh = Net::OpenSSH->new(host => $hostname, user => $username, password => $password); $ssh->error and die "Unable to connect to remote host: $hostname as $username " . $ssh->error; print "Username: $username Password: $password\n"; # Figure Out What Command To Run my ( $pty, $pid ) = $ssh->open2pty({stderr_to_stdout => 1}, '/usr/bin/sudo', -p => $rootpassword, 'su', '-') or return "failed to attempt su: $!\n"; my $expect = Expect->init($pty); $expect->log_file("$path/expect_pm.log", "w"); my @cmdlist =( "ls -l", "pwd", "ls", "who am i", "id", "whoami" ); foreach my $expect_cmd (@cmdlist){ $expect->expect(2, [ qr/$rootpassword/ => sub { shift->send("$password\n");} ], [ qr/Sorry/ => sub { die "Login failed" } ], [ qr/#/ => sub { shift->send("$expect_cmd \n");}] ) or die "___Timeout!"; } $expect->expect(2);