sub connect { my $host = shift; my $SSH = Expect->spawn( @ssh, "$user\@$host" ); ( $match_num, $error, $match, $before, $after ) = $SSH->expect( $time, '-re', '[Nn]ew [Pp]assword:', '-re', '^[Pp]assword.*:', '-re', $prompt ); if ($match_num == 1) { $result = "FAILED: Account Expired"; } elsif ( $match_num == 2 ) { print $SSH "$pass\r"; ( $match_num, $error, $match, $before, $after ) = $SSH->expect( $time, '-re', '[Pp]assword.*:', '-re', $prompt ); if ( $match_num == 1) { $result = "FAILED: Incorrect Password"; } elsif ( $match_num == 2 ) { print $SSH "export PS1='scripts: '\r"; ( $match_num, $error, $match, $before, $after ) = $SSH->expect( $time, '-re', '\r\nscripts: ' ); if ( $match_num == 1 ) { $result = "SUCCESS: you are logged into $host"; } else { $before =~ s/\r\n//g; $after =~ s/\r\n//g; $result = "ERROR: $error BEFORE: $before AFTER: $after"; } } elsif ( $match_num == 3) { $result = "SUCCESS: you are logged into $host"; } } elsif ( $match_num == 3) { $result = "SUCCESS: you are logged into $host"; } else { $before =~ s/\r\n//g; $after =~ s/\r\n//g; $result = "ERROR: $error BEFORE: $before AFTER: $after"; } }