ssh now can return a string
This commit is contained in:
parent
ae11c5d0b8
commit
f7c104e979
2 changed files with 74 additions and 0 deletions
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef ARC_SSH_H_
|
||||
#define ARC_SSH_H_
|
||||
|
||||
#include "arc/std/string.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
|
@ -44,6 +46,11 @@ void ARC_Ssh_RunInSession(ARC_Ssh *ssh, ARC_Ssh_SessionFn sessionFn);
|
|||
*/
|
||||
void ARC_Ssh_ExecStrInNewSession(ARC_Ssh *ssh, char *command);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
ARC_String *ARC_Ssh_ExecStrInNewSessionAndGetResponse(ARC_Ssh *ssh, char *command);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "arc/networking/ssh.h"
|
||||
|
||||
#include "arc/std/errno.h"
|
||||
#include "arc/std/string.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <libssh/libssh.h>
|
||||
|
|
@ -222,3 +223,69 @@ void ARC_Ssh_ExecStrInNewSession(ARC_Ssh *ssh, char *command){
|
|||
ssh_channel_close(channel);
|
||||
ssh_channel_free(channel);
|
||||
}
|
||||
|
||||
ARC_String *ARC_Ssh_ExecStrInNewSessionAndGetResponse(ARC_Ssh *ssh, char *command){
|
||||
ssh_channel channel;
|
||||
channel = ssh_channel_new(ssh->session);
|
||||
if(channel == NULL){
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
ARC_DEBUG_ERR("ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed to create channel\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int32_t returnCode = ssh_channel_open_session(channel);
|
||||
if(returnCode != SSH_OK){
|
||||
ssh_channel_free(channel);
|
||||
arc_errno = ARC_ERRNO_CONNECTION;
|
||||
ARC_DEBUG_LOG(arc_errno, "ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed to open session with return code: %d\n", returnCode);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
returnCode = ssh_channel_request_exec(channel, command);
|
||||
if(returnCode != SSH_OK){
|
||||
ssh_channel_close(channel);
|
||||
ssh_channel_free(channel);
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
ARC_DEBUG_LOG(arc_errno, "ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed when executing command with error code: %d\n", returnCode);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//this is taken from https://api.libssh.org/master/libssh_tutor_command.html
|
||||
char buffer[256];
|
||||
int32_t bytesSize;
|
||||
|
||||
ARC_String *returnString = NULL;
|
||||
|
||||
bytesSize = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
||||
while(bytesSize > 0){
|
||||
if(returnString == NULL){
|
||||
ARC_String_Create(&returnString, buffer, bytesSize);
|
||||
|
||||
bytesSize = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
ARC_String_AppendCString(&returnString, (const char *)buffer, bytesSize);
|
||||
|
||||
bytesSize = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
||||
}
|
||||
|
||||
if(bytesSize < 0){
|
||||
ssh_channel_close(channel);
|
||||
ssh_channel_free(channel);
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
ARC_DEBUG_ERR("ARC_Ssh_RunInSession(ssh, sessionFn), ssh failed when reading bytes from channel\n");
|
||||
|
||||
if(returnString != NULL){
|
||||
ARC_String_Destroy(returnString);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssh_channel_send_eof(channel);
|
||||
ssh_channel_close(channel);
|
||||
ssh_channel_free(channel);
|
||||
|
||||
return returnString;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue