0

Disclaimer: Google-fu'ing my way through this...

I was wondering if anyone would be able to take a look at this & point me in the right direction & thank you for anyone able to solve...I'm fresh out of idea's again...

The file size function associated with the length variable is this;

ifstream::pos_type filesize(const char* filename)
{
    ifstream in(filename, ios::binary | ios::ate);
    return in.tellg();
}

I'm catching the "scp status code 1d not valid" from this piece;

rc = ssh_scp_push_file(scp, "samp_batch", length, 0766);
if (rc != SSH_OK)
{
    fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session));
    ssh_free(my_ssh_session);
    exit(-1);
}

of this function;

int ssh()
{
    ssh_session my_ssh_session;
    ssh_scp scp;
    int port = 22;
    int rc;
    int method;
    const long int length = filesize("samp_batch");
    char password[128] = { 0 };
    my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
        exit(-1);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "10.52.136.185");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "security");

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

    //verify the servers identity
    if (verify_knownHost(my_ssh_session) < 0)
    {
        fprintf(stdout, "unkown host\n");
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }


    // Try to authenticate
    rc = ssh_userauth_none(my_ssh_session, NULL);
    if (rc == SSH_AUTH_ERROR) {
        error(my_ssh_session);
        return rc;
    }

    method = ssh_auth_list(my_ssh_session);
    while (rc != SSH_AUTH_SUCCESS) {
        // Try to authenticate with public key first
        if (method & SSH_AUTH_METHOD_PUBLICKEY) {
            rc = ssh_userauth_autopubkey(my_ssh_session, NULL);
            if (rc == SSH_AUTH_ERROR) {
                error(my_ssh_session);
                return rc;
            }
            else if (rc == SSH_AUTH_SUCCESS) {
                break;
            }
        }

        // Try to authenticate with keyboard interactive";
        if (method & SSH_AUTH_METHOD_INTERACTIVE) {
            rc = authenticate_kbdint(my_ssh_session, NULL);
            if (rc == SSH_AUTH_ERROR) {
                error(my_ssh_session);
                return rc;
            }
            else if (rc == SSH_AUTH_SUCCESS) {
                break;
            }
        }

        if (ssh_getpass("Password: ", password, sizeof(password), 0, 0) < 0) {
            return SSH_AUTH_ERROR;
        }

        // Try to authenticate with password
        if (method & SSH_AUTH_METHOD_PASSWORD) {
            rc = ssh_userauth_password(my_ssh_session, NULL, password);
            if (rc == SSH_AUTH_ERROR) {
                error(my_ssh_session);
                return rc;
            }
            else if (rc == SSH_AUTH_SUCCESS) {
                break;
            }
        }
    }

    //SCP samp_batch file here
    scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/");
    if (scp == NULL)
    {
        fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session));
        return SSH_ERROR;
    }
    rc = ssh_scp_init(scp);
    if (rc != SSH_OK)
    {
        fprintf(stderr, "Error initializing scp session: %s\n", ssh_get_error(my_ssh_session));
        ssh_scp_free(scp);
        return rc;
    }

    rc = ssh_scp_push_file(scp, "samp_batch", length, 0766);
    if (rc != SSH_OK)
    {
        fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session));
        ssh_free(my_ssh_session);
        exit(-1);
    }

    const char *contents = "samp_batch";
    rc = ssh_scp_write(scp, contents, length);
    if (rc != SSH_OK)
    {
        fprintf(stderr, "Cant write to remote file: %s\n", ssh_get_error(my_ssh_session));
        ssh_free(my_ssh_session);
        exit(-1);
    }

    ssh_scp_close(scp);
    ssh_scp_free(scp);
    return SSH_OK;

    //execute remote command here


    ssh_free(my_ssh_session);
    return 0;
}

Again, any insight would be great. I can't even find anything explaining what status code 1d means :/

Snow
  • 41
  • 8
  • 1
    Try running `ssh user@remotehost echo foo` using the same user & host that you're using for this process. Do you get anything besides "foo" from the remote server? In particular, do you get a welcome banner or anything like that? Also, are you able to copy this file by running the `scp` utility interactively? – Kenster Apr 20 '17 at 17:37
  • @Kenster For "ssh user@remotehost echo foo", [foo] is the only thing returned, no banners, etc. it is a known host w/ a shared pub key. for scp, [scp samp_batch user@remotehost:/home/user/samp_batch worked like a charm – Snow Apr 20 '17 at 18:01
  • @Kenster [ssh_scp_new(session, ssh_scp_write, "filepath"); was different! it's actually creating a file on the far end now :D. (I'm also getting a segmentation fault now, but i'll work on that tomorrow lol). thanks for the help. – Snow Apr 20 '17 at 18:04
  • I have also received the 1d error. In my case, it was a matter of changing the mode. I had started with the scp example code at https://github.com/substack/libssh/blob/master/examples/libssh_scp.c, and running it unmodified, I was unable to send the file. When I changed `mode = ssh_scp_request_get_permissions(src->scp);` to `mode = 0600;`, I was able to get the desired behavior. I think it might have to do with the umask on the server to which I was sending, but I'm not sure. – Mike Spear Sep 25 '18 at 14:26

1 Answers1

1
scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/");
if (scp == NULL)
{
    fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session));
    return SSH_ERROR;
}

Should have been;

scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/home/user/");
if (scp == NULL)
{
    fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session));
    return SSH_ERROR;
}
Snow
  • 41
  • 8