Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <QCoreApplication>
  2. #include <QTextStream>
  3. #define LIBSSH_STATIC 1
  4. #include <libssh/libssh.h>
  5. #include <io.h>
  6.  
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. QCoreApplication a(argc, argv);
  11. QTextStream qout(stdout);
  12. qout << "Working" << endl;
  13.  
  14. ssh_session my_ssh_session = ssh_new();
  15.  
  16. if(!my_ssh_session) return -1;
  17. ssh_options_set(my_ssh_session,SSH_OPTIONS_HOST,"192.168.0.1");
  18. // int logg=SSH_LOG_FUNCTIONS;
  19. // ssh_options_set(my_ssh_session,SSH_OPTIONS_LOG_VERBOSITY,&logg);
  20. int time_out=15;
  21. ssh_options_set(my_ssh_session,SSH_OPTIONS_TIMEOUT,&time_out);
  22. int rc=ssh_connect(my_ssh_session);
  23. if(rc!=SSH_OK)
  24. {
  25. qout << "Error connectiong to host\n";
  26. return -1;
  27. }
  28.  
  29.  
  30. const char *password="password";
  31. const char *user="Admin";
  32.  
  33.  
  34. rc=ssh_userauth_password(my_ssh_session,user,password);
  35. if (rc!=SSH_AUTH_SUCCESS)
  36. {
  37. qout << "Failed to authenticate\n";
  38. return -1;
  39. }
  40.  
  41. ssh_channel channel=ssh_channel_new(my_ssh_session);
  42. if (channel==NULL) return SSH_ERROR;
  43.  
  44. rc=ssh_channel_open_session(channel);
  45. if (rc!=SSH_OK) return rc;
  46.  
  47. rc = ssh_channel_request_exec(channel,"ls -la");
  48. if (rc!=SSH_OK) return rc;
  49.  
  50. char buffer[256];
  51. int nbytes=ssh_channel_read(channel,buffer,sizeof(buffer),0);
  52. while (nbytes > 0)
  53. {
  54. if (write(1,buffer,nbytes)!=(unsigned) nbytes){
  55. ssh_channel_close(channel);
  56. ssh_channel_free(channel);
  57. return SSH_ERROR;
  58. }
  59. nbytes = ssh_channel_read(channel,buffer,sizeof(buffer),0);
  60. }
  61. ssh_channel_send_eof(channel);
  62. ssh_channel_close(channel);
  63. ssh_channel_free(channel);
  64.  
  65. ssh_disconnect(my_ssh_session);
  66. ssh_free(my_ssh_session);
  67.  
  68. return 0;
  69. return a.exec();
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement