Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QCoreApplication>
- #include <QTextStream>
- #define LIBSSH_STATIC 1
- #include <libssh/libssh.h>
- #include <io.h>
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- QTextStream qout(stdout);
- qout << "Working" << endl;
- ssh_session my_ssh_session = ssh_new();
- if(!my_ssh_session) return -1;
- ssh_options_set(my_ssh_session,SSH_OPTIONS_HOST,"192.168.0.1");
- // int logg=SSH_LOG_FUNCTIONS;
- // ssh_options_set(my_ssh_session,SSH_OPTIONS_LOG_VERBOSITY,&logg);
- int time_out=15;
- ssh_options_set(my_ssh_session,SSH_OPTIONS_TIMEOUT,&time_out);
- int rc=ssh_connect(my_ssh_session);
- if(rc!=SSH_OK)
- {
- qout << "Error connectiong to host\n";
- return -1;
- }
- const char *password="password";
- const char *user="Admin";
- rc=ssh_userauth_password(my_ssh_session,user,password);
- if (rc!=SSH_AUTH_SUCCESS)
- {
- qout << "Failed to authenticate\n";
- return -1;
- }
- ssh_channel channel=ssh_channel_new(my_ssh_session);
- if (channel==NULL) return SSH_ERROR;
- rc=ssh_channel_open_session(channel);
- if (rc!=SSH_OK) return rc;
- rc = ssh_channel_request_exec(channel,"ls -la");
- if (rc!=SSH_OK) return rc;
- char buffer[256];
- int nbytes=ssh_channel_read(channel,buffer,sizeof(buffer),0);
- while (nbytes > 0)
- {
- if (write(1,buffer,nbytes)!=(unsigned) nbytes){
- ssh_channel_close(channel);
- ssh_channel_free(channel);
- return SSH_ERROR;
- }
- nbytes = ssh_channel_read(channel,buffer,sizeof(buffer),0);
- }
- ssh_channel_send_eof(channel);
- ssh_channel_close(channel);
- ssh_channel_free(channel);
- ssh_disconnect(my_ssh_session);
- ssh_free(my_ssh_session);
- return 0;
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement