PHP - How to SCP local file from BASH OSX to remote via SSH with Password in PHP? -


i want php send myfile.txt remote ssh uses password login.

from osx bash use command send file manually:

scp /users/myname/documents/myfile.txt root@192.168.1.116:/docs/myfile.txt 

since need via php, made upload.sh bash script:

#!/usr/bin/expect spawn scp /users/myname/documents/myfile.txt root@192.168.1.116:/docs/myfile.txt expect "root@192.168.1.116's password:" send "mypassword\r" interact 

i can run above code with: ./update.sh in terminal , sends file want.

now problem when add bash script in php, dont send file.

i have tried:

exec('/library/webserver/update.sh 2>&1', $output); print_r($output);  

and stuck @ error/msg:

array (     [0] => spawn scp /users/myname/documents/myfile.txt root@192.168.1.116:/docs/myfile.txt     [1] => root@192.168.1.116's password: ) 

in bash password sent , ok, via php dont uploads file. noticed first time run via php said ssh host keys dont match, copied .ssh folder server root location , error gone.

can please how make work? thank you

working expected, not rocksolid solution.

i found solution , got working not sure if correct way. problem not password related. problem is/was: bash script fast. or maybe php fast?

anyway solved problem adding sleep after send "mypassword\r". here working bash command access php send file remote password:

#!/usr/bin/expect spawn scp /library/webserver/documents/test34/myfile.txt root@192.168.1.116:/docs/myfile.txt expect "root@192.168.1.116's password:" send "mypassword\r" sleep 0.2 interact 

i saved as: update.sh , did chmod +x update.sh

then in php file: index.php wrote:

<?php $do = shell_exec("(/library/webserver/documents/test34/update.sh)"); var_dump($do); ?> 

and got success result:

string(320) "spawn scp /library/webserver/documents/test34/myfile.txt root@192.168.1.116:/docs/myfile.txt root@192.168.1.116's password:   myfile.txt                                  0%    0     0.0kb/s   --:-- eta myfile.txt                                100% 3649     3.6kb/s   00:00     " 

so adding sleep 0.2in bash script solved problem.

i tried sleep 0.1 got error. therefore sleep time might depended on how big file? myfile.txt 3.6kb , 0.2 seconds sleep works great.

if file bigger can try sleep 1 or sleep 5 etc..

this method works , solves problem doesn't feel secure since might fail if file size varies on every call or if there slow ssh connection.

if can contribute improve answer highly appreciate it.


Comments