I use expect in perl to do ssh, following is my code snipet.
my $exp = Expect->spawn("ssh renjithp\@192.168.1.12") or die "could not spawn ssh";
my $op = $exp->expect(undef,'renjithp>');
print "*$op*";
I wanted to handle the error if the host is not reachable (destination IP is down), I was in a impression die will hit when the ip is not reachable, but when i give a wrong IP script is not terminating and it continuous execution.
what is the right way to handle this scenario?
EDITED
I am observing $op value is 1 when the ssh is succesfull, and 0 when the destination IP is not up. is it right way to use $op to take a decision?
I have one more doubt, when the destination IP is not reachable why the control coming out of expect,i mean ‘$exp->expect(undef,’renjithp>’);’ should return only after its getting the prompt right?
If you need to make the ssh connection contingent on IP address reachability with the expect module, you should test connectivity with a
ncfirst…The
nccommand isnetcat…nc -ztests for TCP port openAlternatively, you could use a module like
Net::OpenSSHthat makes error handling a bit easier…