Search code examples
phpsshcygwinexpectscp

PHP exec expect script with SCP


I have a PHP script at C:\WampServer\www\dnsmasq\php\dnsmasq.php:

function uploadConfig() {
    exec('C:\cygwin64\bin\expect -f /home/Dave/bin/scp.expect asdf', $output, $exitCode);
    print_r($output);
    print($exitCode);
}

Which calls a script at /home/Dave/bin/scp.expect:

#!/usr/bin/expect -f

set password [lindex $argv 0]

spawn /usr/bin/scp /home/Dave/bin/test [email protected]:/etc/test

expect {
    -re ".*yes.*no.*" {
        exp_send "yes\r"
        exp_continue
    }
    -re ".*password.*" {
        exp_send "$password\r"
    }
}

interact

The file to upload contains one line:

Testing

Running the uploadConfig function results in the following output on the page:

Array ( [0] => spawn /usr/bin/scp /home/Dave/bin/test [email protected]:/etc/test [1] => [email protected]'s password: ) 0 

The file appears on the server at /etc/test but it is blank, i.e. it doesn't contain the word "Testing".

I can run the expect script inside a cygwin shell and it uploads the file with the content:

$ expect -f /home/Dave/bin/scp.expect asdf

I'm not sure where the failure point is when I try to do this from PHP.


Solution

  • You use interact when you want to, guess what, interact with the spawned application interactively.

    If you want to spawn a program, send some data to it, and wait for it to finish, use expect eof instead.