Exemplo n.º 1
0
int test()
{
    ssh_session my_ssh_session;
    int rc;

    // Open session and set options
    my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
      exit(-1);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "localhost");

    // Connect to server
    rc = ssh_connect(my_ssh_session);
    if (rc != SSH_OK)
    {
      fprintf(stderr, "Error connecting to localhost: %s\n",
              ssh_get_error(my_ssh_session));
      ssh_free(my_ssh_session);
      exit(-1);
    }

    // Verify the server's identity
    // For the source code of verify_knowhost(), check previous example
//    if (verify_knownhost(my_ssh_session) < 0)
//    {
//      ssh_disconnect(my_ssh_session);
//      ssh_free(my_ssh_session);
//      exit(-1);
//    }

    // Authenticate ourselves
    QString password("whcrosedu");
    rc = ssh_userauth_password(my_ssh_session, NULL, password.toLatin1());
    if (rc != SSH_AUTH_SUCCESS)
    {
      fprintf(stderr, "Error authenticating with password: %s\n",
              ssh_get_error(my_ssh_session));
      ssh_disconnect(my_ssh_session);
      ssh_free(my_ssh_session);
      exit(-1);
    }
    qDebug()<<"evrika";

    show_remote_processes(my_ssh_session);

    ssh_disconnect(my_ssh_session);
    ssh_free(my_ssh_session);
}
Exemplo n.º 2
0
int main(int argc, char **argv) {

    // check for args num
    if (argc < 3) exit(-1);

    // assign first arg to host var
    char host[20];
    strcpy(host, argv[1]);

    // assign second arg to port var
    int port = atoi(argv[2]);

// set verbosity if need
//    int verbosity = SSH_LOG_FUNCTIONS;
//    int verbosity = SSH_LOG_PROTOCOL;
    int connection;

    ssh_session session;
    session = ssh_new();

    if (session == NULL) exit(-1);

    ssh_options_set(session, SSH_OPTIONS_HOST, host);
//    ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
    ssh_options_set(session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(session, SSH_OPTIONS_USER, "gluck");

    printf("Connecting to host %s and port %d\n", host, port);
    connection = ssh_connect(session);

    if (connection != SSH_OK) {
        printf("Error connecting to %s: %s\n", host, ssh_get_error(session));
        exit -1;
    } else {
        printf("Connected.\n");
    }

    if (verify_knownhost(session) < 0) {
        ssh_disconnect(session);
        ssh_free(session);
        exit(-1);
    }

    int rc;
    char *password;

    password = getpass("Password: "******"Error authenticating with password: %s\n",
            ssh_get_error(session));
        ssh_disconnect(session);
        ssh_free(session);
        exit(-1);
    }

    if (show_remote_processes(session) != SSH_OK) {
        printf("Error executing request\n");
        ssh_get_error(session);
        ssh_disconnect(session);
        ssh_free(session);
        exit(-1);
    } else {
        printf("\nRequest completed successfully!\n");
    }

    ssh_disconnect(session);
    ssh_free(session);
}