Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #include <libssh/libsshpp.hpp>
  6. #include <libssh/libssh.h>
  7.  
  8. int main(int argc, const char **argv)
  9. {
  10. ssh::Session session;
  11.  
  12. int port = 22;
  13. int verbosity = SSH_LOG_NOLOG; //SSH_LOG_PROTOCOL
  14. string host = "localhost";
  15. string user = "user";
  16. string password = "password";
  17.  
  18. //Set options
  19. session.setOption(SSH_OPTIONS_PORT, &port);
  20. session.setOption(SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
  21. session.setOption(SSH_OPTIONS_HOST, host.c_str());
  22. session.setOption(SSH_OPTIONS_USER, user.c_str());
  23.  
  24. /* Attempt to connect to host. On failure, catch SshException and print
  25. * error message. */
  26. cout << "Connecting..." << flush;
  27. try
  28. {
  29. session.connect();
  30. cout << "Connected." << endl;
  31.  
  32. //Perform host-key checking
  33. cout << "Checking host keys..." << flush;
  34. int host_key_status = session.isServerKnown();
  35. switch(host_key_status)
  36. {
  37. case SSH_SERVER_KNOWN_OK:
  38. cout << "valid." << endl;
  39. break;
  40.  
  41. case SSH_SERVER_KNOWN_CHANGED:
  42. case SSH_SERVER_FOUND_OTHER:
  43. cout << endl << "Something strange is going on with the server's"
  44. "host key. Unless you know what is causing this,"
  45. "you may be the target of an attack." << endl;
  46. exit(1);
  47. break;
  48.  
  49. case SSH_SERVER_NOT_KNOWN:
  50. case SSH_SERVER_FILE_NOT_FOUND:
  51. //There is currently no way to get the host key fingerprint/hash through
  52. //the CPP bindings for libssh. If there were, we would present them to the
  53. //user to verify that they are connecting to the correct server.
  54. cout << "Connecting to an unknown server." << endl;
  55. break;
  56.  
  57. case SSH_SERVER_ERROR:
  58. cout << "Error while checking host keys." << endl;
  59. exit(1);
  60. break;
  61.  
  62. }
  63.  
  64. //Password authentication after connection
  65. cout << "Authenticating..." << flush;
  66. int auth_status = session.userauthPassword(password.c_str());
  67. switch(auth_status)
  68. {
  69. case SSH_AUTH_SUCCESS:
  70. cout << "done." << endl;
  71. break;
  72.  
  73. case SSH_AUTH_PARTIAL:
  74. case SSH_AUTH_DENIED:
  75. cout << "failed." << endl;
  76. exit(1);
  77. break;
  78.  
  79. default:
  80. cout << "unknown error." << endl;
  81. exit(1);
  82. break;
  83. }
  84.  
  85. cout << "Opening channel..." << flush;
  86. ssh::Channel channel(session);
  87. cout << "done." << endl;
  88.  
  89.  
  90. cout << "Opening session..." << flush;
  91. channel.openSession();
  92. cout << "done." << endl;
  93.  
  94. //Do something
  95. channel.requestExec( "ps aux" );
  96. sleep(1);
  97.  
  98. int recv_buf[512];
  99. int nbytes = channel.read(recv_buf, sizeof(recv_buf), 10);
  100. while(nbytes > 0)
  101. {
  102. if(fwrite(recv_buf, 1, nbytes, stdout) != nbytes)
  103. {
  104. cerr << "error reading" << endl;
  105. }
  106. nbytes = channel.read(recv_buf, sizeof(recv_buf), 10);
  107. }
  108.  
  109. //Close channel
  110. channel.close();
  111. channel.sendEof();
  112.  
  113. //Disconnect
  114. session.disconnect();
  115.  
  116. cout << "Done." << endl;
  117. }
  118. catch(ssh::SshException e)
  119. {
  120. cout << e.getError() << endl;
  121. exit(1);
  122. }
  123.  
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement