Why are you terminating sends with "\r" and not "\n"?
#!/usr/bin/env perl
use 5.014;
use warnings;
use Expect;
my $exp = Expect->new;
$exp->spawn('ssh tak');
$exp->expect(10, '~$ '); # prompt. using ssh keys so no password
$exp->send("ls\n");
$exp->expect(10, '~$ ');
$exp->send("exit\n");
$exp->soft_close();
__END__
Output:
zengargoyle@tak:~$ ls
... ls output ...
zengargoyle@tak:~$ exit
logout
Connection to tak closed.
I would guess that the password input is accepting the "\r" as an input terminator (because login/password type input is wonky like that) but the proper input terminator for the type of session input and for general shell commands is "\n".
Just a guess, unless your Solaris is wonky or running some special shell (not csh/sh/etc) you should be using "\n". I've done a bunch of Expect stuff with Solaris, routers, switches, ... over ssh and never have I used "\r". I always use "\n" and have never had the sort of problem you seem to be having.