Search code examples
perlsshmodulecisco

Perl: Unable to check validity of hostname for Net::Appliance::Session


When I tried to pass an invalid hostname, the code will get into an infinite loop.

my $s = Net::Appliance::Session->new({
personality => 'ios',
transport => 'SSH',
host => $ip
});

Is there a way to overcome this bug?

EDIT: Here's my full code: I use the subroutine to download the config file of my network device. When I pass in an invalid IP address in download_config, it will get into an infinite loop.

sub download_config
{
    my ($ip) = @_;
    my $s = Net::Appliance::Session->new({
     personality => 'ios',
     transport => 'SSH',
     host => $ip,
     Timeout => 1
    });

$s->set_global_log_at('debug'); # maximum debugging

    eval {
        $s->connect({ username => $username, password => $password });
        $s->begin_privileged({ password => $enable_password });

        #get hostname to set the file name
        $hostname_result = $s->cmd('sh run | inc hostname');
        $hostname_result =~ m/hostname (.*)/;
        $hostname = $1;

        #download the file
        my @running_config = $s->cmd('sh run');
        @running_config = @running_config[ 2 .. (@running_config -1)];#remove header and footer of the file
        open(FH, "> temp/".$hostname.".txt") or die("Cannot open config file : $!");
        print FH @running_config;
        close FH;
        $s->end_privileged;
    };
    if ($@) {
    #when the login details are wrong
        print redirect('../../na/unauthorised.html');
    }
    $s->close;
}

Solution

  • The developer has fixed the bug.