[Libssh] channel_read - Save read characters in "string"

Aris Adamantiadis aris at 0xbadc0de.be
Mon Jun 30 00:19:33 CEST 2008


Rico Kerster a écrit :
> Hello libssh-List
> I successfully logged into my SSH server, can issue a command and output the comands' result to the terminal window (as in the example, I achieved that with the function "write()")
> 
>     string command_output;
>     char cmd[] = "date\n";
>     BUFFER *readbuf = buffer_new();
>     int num = channel_write(channel,cmd,sizeof(cmd));
>     num = channel_read(channel,readbuf,0,0);
> 
>     write(1,buffer_get(readbuf),buffer_get_len(readbuf));
> 
> But now I'd like to store the content of readbuf in the string "command_output", I studied the _M_write() template and how "write()" gets out the characters but I couldn't manage to understand and copy it.  So to sum up my question:
> 
> How do I store the characters of readbuff (BUFFER) in command_output (string) or in a char[]?
> 
> Thanks in Advance.
> 
Hello,

Consider the output of buffer_get like a char [] type string, except it 
is not nul terminated.
http://www.cplusplus.com/reference/string/string/assign.html
Look at string::assign(const char *s, size_t n);

I's much like a C++ problem you've got. The next release of libssh will 
have a more classical channel_read() which won't require a BUFFER object.

Regards,
Aris



More information about the Libssh mailing list