Advertisement
Guest User

Untitled

a guest
Sep 26th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <libssh/libssh.h>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. int show_remote_processes(ssh_session session, std::string command)
  8. {
  9. ssh_channel channel;
  10. int rc;
  11. char buffer[256];
  12. unsigned int nbytes;
  13. channel = ssh_channel_new(session);
  14. if (channel == NULL)
  15. return SSH_ERROR;
  16. rc = ssh_channel_open_session(channel);
  17. if (rc != SSH_OK)
  18. {
  19. ssh_channel_free(channel);
  20. return rc;
  21. }
  22. ssh_channel_set_blocking(channel, 1);
  23. rc = ssh_channel_request_exec(channel, "dir");
  24. if (rc != SSH_OK)
  25. {
  26. ssh_channel_close(channel);
  27. ssh_channel_free(channel);
  28. return rc;
  29. }
  30. nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
  31. while (nbytes > 0)
  32. {
  33. if (write(1, buffer, nbytes) != nbytes)
  34. {
  35. ssh_channel_close(channel);
  36. ssh_channel_free(channel);
  37. return SSH_ERROR;
  38. }
  39. nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
  40. }
  41.  
  42. if (nbytes < 0)
  43. {
  44. ssh_channel_close(channel);
  45. ssh_channel_free(channel);
  46. return SSH_ERROR;
  47. }
  48. ssh_channel_send_eof(channel);
  49. ssh_channel_close(channel);
  50. ssh_channel_free(channel);
  51. return SSH_OK;
  52. }
  53.  
  54. int main(){
  55. ssh_session session;
  56. int connection;
  57. session = ssh_new();
  58. ssh_set_blocking(session, 1);
  59. if (session == NULL)
  60. std::cerr << "Failed";
  61. ssh_options_set(session, SSH_OPTIONS_HOST, "10.50.239.211");
  62. ssh_options_set(session, SSH_OPTIONS_USER, "admin");
  63. connection = ssh_connect(session);
  64. if (connection != SSH_OK){
  65. std::cerr << "Failed to connect: " << ssh_get_error(session)
  66. << std::endl;
  67. ssh_disconnect(session);
  68. ssh_free(session);
  69. return 1;
  70. }
  71. connection = ssh_userauth_password(session, NULL, "kalafior");
  72. if (connection != SSH_AUTH_SUCCESS){
  73. std::cerr << "\nFailed to authenticate: " << ssh_get_error(session)
  74. << std::endl;
  75. ssh_disconnect(session);
  76. ssh_free(session);
  77. return 1;
  78. }
  79. if (show_remote_processes(session, "dir") != SSH_OK)
  80. std::cerr << "Something went wrong: " << ssh_get_error(session)
  81. << std::endl;
  82. ssh_disconnect(session);
  83. ssh_free(session);
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement