peroksid
peroksid

Reputation: 357

run rsync through execvp: StrictHostKeyChecking=no: unknown option

I am trying to run rsync through execvp with StrictHostKeyChecking option. This is my code:

#include <unistd.h>
int main() {
        char *argv[] = {"rsync",
                        "[email protected]:/tmp",
                        "/home/tmp/",
                        "-e 'ssh -o StrictHostKeyChecking=no'",
                        0};
        execvp("rsync", argv);
}

I am getting this error:

rsync: -e '-o StrictHostKeyChecking=no': unknown option rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]

I have tried another way:

#include <unistd.h>
int main() {
        char *argv[] = {"rsync",
                        "[email protected]:/tmp",
                        "/home/tmp/",
                        "-e",
                        "'ssh -o StrictHostKeyChecking=no'",
                        0};
        execvp("rsync", argv);
}

But now it is failing with error:

rsync: Failed to exec ssh -o StrictHostKeyChecking=no: No such file or directory (2) rsync error: error in IPC code (code 14) at pipe.c(84) [sender=3.0.6]

Why it don't understand StrictHostKeyChecking option?

Upvotes: 2

Views: 970

Answers (1)

Filipe Gon&#231;alves
Filipe Gon&#231;alves

Reputation: 21213

rsync expects to receive the options first, followed by the hosts. You're doing it backwards: first you need to specify -e and -o.

You also shouldn't be single-quoting the -o option: that is needed when invoking it from bash, to prevent bash from interpreting the arguments and splitting them into separate argv[] entries. Think about it: when bash sees '-o StrictHostKeyChecking=no', it passes -o StrictHostKeyChecking=no as a single argv[] entry, without the single quotes (because the single quotes is your way to tell the shell that you don't want argument splitting).

Last, but not least, you should check that execvp(3) didn't fail.

So, this is what you need:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    char *argv[] = {
        "rsync",
        "-e",
        "ssh -o StrictHostKeyChecking=no",
        "[email protected]:/tmp",
        "/home/tmp/",
        NULL
    };


    if (execvp("rsync", argv) < 0) {
        perror("rsync(1) error");
        exit(EXIT_FAILURE);
    }

    return 0;
}

Upvotes: 1

Related Questions