Take a look at
Net::SSH::Expect... I use it quite a lot, and it works really well. It has configurable timeouts for connections, and many other options. Below is an example from one of my scripts:
my $ssh = Net::SSH::Expect->new (
host => "$serverlist[$host]",
user => "$user",
raw_pty => 1,
restart_timeout_upon_receive => 1,
timeout => 5,
ssh_option => " -x -o ConnectTimeout=4",
log_file=> "/tmp/$serverlist[$host].log",
);
One difference is that this module expects to use an SSH key instead of an interactive password. I don't know if that's an issue for you or not.