Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <libssh/libssh.h>
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <string>
- int show_remote_processes(ssh_session session, std::string command)
- {
- ssh_channel channel;
- int rc;
- char buffer[256];
- unsigned int nbytes;
- channel = ssh_channel_new(session);
- if (channel == NULL)
- return SSH_ERROR;
- rc = ssh_channel_open_session(channel);
- if (rc != SSH_OK)
- {
- ssh_channel_free(channel);
- return rc;
- }
- ssh_channel_set_blocking(channel, 1);
- rc = ssh_channel_request_exec(channel, "dir");
- if (rc != SSH_OK)
- {
- ssh_channel_close(channel);
- ssh_channel_free(channel);
- return rc;
- }
- nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
- while (nbytes > 0)
- {
- if (write(1, buffer, nbytes) != nbytes)
- {
- ssh_channel_close(channel);
- ssh_channel_free(channel);
- return SSH_ERROR;
- }
- nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
- }
- if (nbytes < 0)
- {
- ssh_channel_close(channel);
- ssh_channel_free(channel);
- return SSH_ERROR;
- }
- ssh_channel_send_eof(channel);
- ssh_channel_close(channel);
- ssh_channel_free(channel);
- return SSH_OK;
- }
- int main(){
- ssh_session session;
- int connection;
- session = ssh_new();
- ssh_set_blocking(session, 1);
- if (session == NULL)
- std::cerr << "Failed";
- ssh_options_set(session, SSH_OPTIONS_HOST, "10.50.239.211");
- ssh_options_set(session, SSH_OPTIONS_USER, "admin");
- connection = ssh_connect(session);
- if (connection != SSH_OK){
- std::cerr << "Failed to connect: " << ssh_get_error(session)
- << std::endl;
- ssh_disconnect(session);
- ssh_free(session);
- return 1;
- }
- connection = ssh_userauth_password(session, NULL, "kalafior");
- if (connection != SSH_AUTH_SUCCESS){
- std::cerr << "\nFailed to authenticate: " << ssh_get_error(session)
- << std::endl;
- ssh_disconnect(session);
- ssh_free(session);
- return 1;
- }
- if (show_remote_processes(session, "dir") != SSH_OK)
- std::cerr << "Something went wrong: " << ssh_get_error(session)
- << std::endl;
- ssh_disconnect(session);
- ssh_free(session);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement