implementation of sending commands via ssh_channel_request_exec()
This commit is contained in:
parent
50e8aff8fa
commit
be39b3be8c
2 changed files with 31 additions and 77 deletions
|
@ -122,9 +122,6 @@ ErrorCode ServerController::uploadTextFileToContainer(DockerContainer container,
|
|||
{
|
||||
ErrorCode e = ErrorCode::NoError;
|
||||
QString tmpFileName = QString("/tmp/%1.tmp").arg(Utils::getRandomString(16));
|
||||
|
||||
qDebug() << "data" << file;
|
||||
|
||||
e = uploadFileToHost(credentials, file.toUtf8(), tmpFileName);
|
||||
if (e) return e;
|
||||
|
||||
|
@ -202,18 +199,7 @@ QByteArray ServerController::getTextFileFromContainer(DockerContainer container,
|
|||
|
||||
qDebug().noquote() << "Copy file from container END : \n" ;
|
||||
|
||||
|
||||
int pos=stdOut.lastIndexOf("'\"");
|
||||
|
||||
QString cuted_right(stdOut.right(stdOut.size()-pos-4));
|
||||
|
||||
pos=cuted_right.lastIndexOf("\n");
|
||||
|
||||
QString cuted(cuted_right.left(pos-1));
|
||||
|
||||
qDebug().noquote() << "Copy file cuted : \n" << cuted <<"cut END";
|
||||
|
||||
return QByteArray::fromHex(cuted.toUtf8());
|
||||
return QByteArray::fromHex(stdOut.toUtf8());
|
||||
}
|
||||
|
||||
ErrorCode ServerController::checkOpenVpnServer(DockerContainer container, const ServerCredentials &credentials)
|
||||
|
|
|
@ -17,15 +17,6 @@ namespace libssh {
|
|||
|
||||
Session::~Session()
|
||||
{
|
||||
if (m_isNeedSendChannelEof) {
|
||||
ssh_channel_send_eof(m_channel);
|
||||
}
|
||||
if (m_isChannelOpened) {
|
||||
ssh_channel_close(m_channel);
|
||||
}
|
||||
if (m_isChannelCreated) {
|
||||
ssh_channel_free(m_channel);
|
||||
}
|
||||
if (m_isSftpInitialized) {
|
||||
sftp_free(m_sftpSession);
|
||||
}
|
||||
|
@ -88,41 +79,6 @@ namespace libssh {
|
|||
if (error) {
|
||||
return error;
|
||||
}
|
||||
m_channel = ssh_channel_new(m_session);
|
||||
|
||||
if (m_channel == NULL) {
|
||||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
||||
m_isChannelCreated = true;
|
||||
int result = ssh_channel_open_session(m_channel);
|
||||
|
||||
if (result == SSH_OK && ssh_channel_is_open(m_channel)) {
|
||||
qDebug() << "SSH chanel opened";
|
||||
m_isChannelOpened = true;
|
||||
} else {
|
||||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
||||
result = ssh_channel_request_pty(m_channel);
|
||||
if (result != SSH_OK) {
|
||||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
||||
result = ssh_channel_change_pty_size(m_channel, 80, 1024);
|
||||
if (result != SSH_OK) {
|
||||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
||||
result = ssh_channel_request_shell(m_channel);
|
||||
if (result != SSH_OK) {
|
||||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
@ -131,8 +87,19 @@ namespace libssh {
|
|||
const std::function<void(const QString &, Session &)> &cbReadStdOut,
|
||||
const std::function<void(const QString &, Session &)> &cbReadStdErr)
|
||||
{
|
||||
m_channel = ssh_channel_new(m_session);
|
||||
|
||||
if (m_channel == NULL) {
|
||||
qDebug() << "ssh channel not initialized";
|
||||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
||||
int result = ssh_channel_open_session(m_channel);
|
||||
|
||||
if (result == SSH_OK && ssh_channel_is_open(m_channel)) {
|
||||
qDebug() << "SSH chanel opened";
|
||||
} else {
|
||||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
|
||||
|
@ -145,27 +112,25 @@ namespace libssh {
|
|||
int bytesRead = 0;
|
||||
char buffer[bufferSize];
|
||||
|
||||
int bytesWritten = ssh_channel_write(m_channel, data.toUtf8(), (uint32_t)data.size());
|
||||
if (bytesWritten == data.size() && ssh_channel_write(m_channel, "\n", 1)) {
|
||||
int bytesWritten = ssh_channel_request_exec(m_channel, data.toUtf8());
|
||||
if (bytesWritten == SSH_OK) {
|
||||
auto readOutput = [&](bool isStdErr) {
|
||||
std::string output;
|
||||
if (ssh_channel_is_open(m_channel) && !ssh_channel_is_eof(m_channel)) {
|
||||
bytesRead = ssh_channel_read_timeout(m_channel, buffer, sizeof(buffer), isStdErr, 200);
|
||||
while (bytesRead > 0)
|
||||
{
|
||||
output = std::string(buffer, bytesRead);
|
||||
if (!output.empty()) {
|
||||
qDebug().noquote() << (isStdErr ? "stdErr" : "stdOut") << QString(output.c_str());
|
||||
bytesRead = ssh_channel_read(m_channel, buffer, sizeof(buffer), isStdErr);
|
||||
while (bytesRead > 0)
|
||||
{
|
||||
output = std::string(buffer, bytesRead);
|
||||
if (!output.empty()) {
|
||||
qDebug().noquote() << (isStdErr ? "stdErr" : "stdOut") << QString(output.c_str());
|
||||
|
||||
if (cbReadStdOut && !isStdErr){
|
||||
cbReadStdOut(output.c_str(), *this);
|
||||
}
|
||||
if (cbReadStdErr && isStdErr){
|
||||
cbReadStdErr(output.c_str(), *this);
|
||||
}
|
||||
if (cbReadStdOut && !isStdErr){
|
||||
cbReadStdOut(output.c_str(), *this);
|
||||
}
|
||||
if (cbReadStdErr && isStdErr){
|
||||
cbReadStdErr(output.c_str(), *this);
|
||||
}
|
||||
bytesRead = ssh_channel_read_timeout(m_channel, buffer, sizeof(buffer), isStdErr, 2000);
|
||||
}
|
||||
bytesRead = ssh_channel_read(m_channel, buffer, sizeof(buffer), isStdErr);
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
@ -176,8 +141,11 @@ namespace libssh {
|
|||
qDebug() << ssh_get_error(m_session);
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
}
|
||||
m_isNeedSendChannelEof = true;
|
||||
return fromLibsshErrorCode(ssh_get_error_code(m_session));
|
||||
|
||||
ssh_channel_send_eof(m_channel);
|
||||
ssh_channel_close(m_channel);
|
||||
ssh_channel_free(m_channel);
|
||||
});
|
||||
watcher.setFuture(future);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue