简体   繁体   中英

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 root@192.168.1.1:/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 root@192.168.1.1:/etc/test [1] => root@192.168.1.1's password: ) 0 

The file appears on the server at /etc/test but it is blank, ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM