my $cmd = "..."; my $out = $ssh->capture({timeout => 120}, $cmd); unless (defined $out) { warn "unable to run command $cmd, retrying..."; $ssh->restart or die "Unable to restart SSH connection: " . $ssh->error; $out = $ssh->capture({timeout => 120}, $cmd); unless (defined $out) { die "definitively, I can't run the command $cmd: " . $ssh->error; } }