This little piece of code works for me. Beside replacing '???' at $user and $pass you should also replace '\$' with '#' in all three cases.
use strict;
use Expect;
my $user = "???";
my $pass = "???";
my $hostname = "localhost";
my $comm1 = "pwd";
my $comm2 = "date";
my $comm3 = "exit";
$Expect::Log_Stdout = 1;
my $session = Expect->spawn("ssh $user\@$hostname") or die "Error call
+ing external program: $!\n";
$session->expect(5,"password: ");
$session->send("$pass\r");
$session->expect(5, "\r
\$");
$session->send("$comm1\r");
$session->expect(5, "\r
\$");
$session->send("$comm2\r");
$session->expect(5, "\r
\$");
$session->send("$comm3\r");
$session->expect(undef);
|