Skip to content

Commit d4c7357

Browse files
committed
fix: Limit the number of attempts and reported errors when libssh2 fails
1 parent f8ee401 commit d4c7357

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Changes

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ inventory:
2121
* fix #611: Wrong bios value as array on win32 bios when using WMI in proxmox vm
2222
* fix #609: Fix rustdesk remote management version analysis
2323

24+
remoteinventory:
25+
* Limit the number of attempts and reported errors when libssh2 fails but ssh command
26+
works. Libssh2 attempts will be disabled for a minute on failed attempt.
27+
2428
netdiscovery/netinventory:
2529
* Keep device mac address found via snmp during netdiscovery as this is the one
2630
which will be reported during netinventory. This will fix duplication issues

lib/GLPI/Agent/Task/RemoteInventory/Remote/Ssh.pm

+20-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,18 @@ sub disconnect {
4545
sub _connect {
4646
my ($self) = @_;
4747

48+
# We don't need to retry too early if an attempt failed recently
49+
if ($self->{_ssh2_dont_retry_before}) {
50+
return 0 if time < $self->{_ssh2_dont_retry_before};
51+
delete $self->{_ssh2_dont_retry_before};
52+
}
53+
4854
unless ($self->{_ssh2} || ($self->mode('ssh') && !$self->mode('libssh2'))) {
4955
Net::SSH2->require();
5056
if ($EVAL_ERROR) {
5157
$self->{logger}->debug("Can't use libssh2: $EVAL_ERROR");
58+
# Don't retry to load libssh2 before a minute
59+
$self->{_ssh2_dont_retry_before} = time + 60;
5260
} else {
5361
my $timeout = $self->timeout();
5462
$self->{_ssh2} = Net::SSH2->new(timeout => $timeout * 1000);
@@ -70,7 +78,9 @@ sub _connect {
7078
my @error = $ssh2->error;
7179
$self->{logger}->debug("Can't reach $remote for ssh remoteinventory via libssh2: @error");
7280
undef $self->{_ssh2};
73-
return;
81+
# Don't retry to connect with libssh2 before a minute
82+
$self->{_ssh2_dont_retry_before} = time + 60;
83+
return 0;
7484
}
7585

7686
# Use Trust On First Use policy to verify remote host
@@ -103,9 +113,11 @@ sub _connect {
103113
}
104114
unless ($ssh2->check_hostkey($hostkey_checking)) {
105115
my @error = $ssh2->error;
106-
$self->{logger}->error("Can't trust $remote for ssh remoteinventory: @error");
116+
$self->{logger}->error("[libssh2] Can't trust $remote for ssh remoteinventory: @error");
107117
undef $self->{_ssh2};
108-
return;
118+
# Don't retry to connect with libssh2 before a minute
119+
$self->{_ssh2_dont_retry_before} = time + 60;
120+
return 0;
109121
}
110122

111123
# Support authentication by password
@@ -161,8 +173,12 @@ sub _connect {
161173
}
162174
}
163175

164-
$self->{logger}->error("Can't authenticate on $remote remote host");
176+
$self->{logger}->error("Can't authenticate on $remote remote host via libssh2");
165177
undef $self->{_ssh2};
178+
179+
# Don't retry libssh2 before a minute
180+
$self->{_ssh2_dont_retry_before} = time + 60;
181+
166182
return 0;
167183
}
168184

0 commit comments

Comments
 (0)