在新的 routerOS 7.1 上,需要在 ssh_request_exec 之后启动 ssh_read 之前添加一些延迟,在另一种情况下,ssh_read 返回错误 - “远程通道已关闭”或空错误取决于 libssh 版本。不要影响 routeros (6.*) 的先前版本可能对某人有帮助。
int execResult = ssh_channel_request_exec(Device.channel, Command.c_str());
if (execResult != SSH_OK) {
ssh_channel_close(Device.channel);
ssh_channel_free(Device.channel);
Device.channel = nullptr;
return false;
}
double pauseTime = 0.3;
do {
Answer.append(answerFromDevice, readed);
pauseSeconds(pauseTime); // without this string (300 ms delay) ssh_channel_read return -1 on second read
readed = ssh_channel_read(Device.channel, answerFromDevice,
sizeof(answerFromDevice), 0);
} while (readed > 0);
if (readed < 0) {
std::string ErrorReason = ssh_get_error(Device.session);
if (ErrorReason.empty() || ErrorReason == "Remote channel is closed.") { // was error
} else {
ssh_channel_close(Device.channel);
ssh_channel_free(Device.channel);
Device.channel = nullptr;
return false;
}
}