Is it possible to use php exec and answer questions from the script?

exec, linux, php

Solution

It's worth noting that the better solution in your case is to use SSH keys for authentication. But, you might be able to pull this off with PHP Expect.

ini_set("expect.timeout", -1);
ini_set("expect.loguser", "Off");

$stream = expect_popen("git pull");

while (true) {
    switch (expect_expectl ($stream, array (
            array ("username:", USERNAME),
            array ("password:", PASSWORD),
    ))) {
        case USERNAME:
            fwrite ($stream, "usename\n");
            break;
        case PASSWORD:
            fwrite ($stream, "secret\n");
            break;

        case EXP_TIMEOUT:
        case EXP_EOF:
            break 2;

        default:
            die ("Error has occurred!\n");
    }
}

fclose ($stream);

Problem

Imagine I have a script.sh on my server that asks for your name. You run the script and it says: ``` what is your name? ``` then you input your name and it prints: ``` hello name! ``` Is it possible, using php exec, to run such a script? That is to exec the script and somehow answer the different questions it could have? Hope I am clear. What I am really trying to do is automate the "git pull" command from our dev server but it keeps asking for a username/password. I know I could use ssh certificate login to avoid that but my question still stands. Can I use exec() and answer future questions from the script.

Original source