Note that there are some explanatory texts on larger screens.

plurals
  1. POSending input to the blocking stream of PHP ssh
    primarykey
    data
    text
    <p>I have similar situation as the following question <a href="https://stackoverflow.com/questions/10274496/ssh2-change-a-user-password">SSH2 change a user password</a></p> <p>The only difference is my action is "scp", I want to send ssh command through PHP to send the files to remote machine using scp command (I know php itself has method to achieve this, but for some reason, I skip that method). </p> <pre><code>Code is as follows //run specified cmd with stream reading and writing function runCMDAdvance($cmd){ if (strpos($cmd,'scp') !== false) { //reply machine when ask for password in file transfer $confirmPass = $this-&gt;password."\n".$this-&gt;password."\n"; if (!($stream = ssh2_exec($this-&gt;conn, $cmd ))) { echo "fail: unable to execute command\n"; fclose($stream); return ERROR_SSH_EXEC_COMM; } fwrite($stream,$confirmPass); //&gt;&gt;Actually I want to pass the password information by writing to the stream, but turn out that it does not work stream_set_blocking($stream, true); } } </code></pre> <p>When I try to write the password information after the stream set blocking, the php execution code will stuck. (I guess the reason will be the remote machine is waiting for the password information but php does not able to give them, so it just keep waiting and jam the php running thread.)</p> <p>I have read through the above question but can't seem to get the right direction to achieve this, is there any ideas on this??</p> <p>Again, thanks all for the help!</p> <p>Referring to neubert's answer, using phpseclib still not work...</p> <pre><code>//$cmd is $cmd = 'scp '.$scpPath.' root@'.$ip.':/root'; function runCMDAdvance($cmd, $tVal){ if (strstr($cmd,"scp")!==FALSE){ if((include('Net/SSH2.php')) &amp;&amp; (include('Net/SCP.php'))){ //set ssh timeout value $this-&gt;setTimeoutVar($tVal); $ssh = new Net_SSH2($this-&gt;host); if (!$ssh-&gt;login($this-&gt;username, $this-&gt;password)){ echo "Bad Login"; return false; } $ssh-&gt;write($cmd."\n"); $ssh-&gt;read(' password:'); $ssh-&gt;write($this-&gt;password."\n"); } } else{ echo "Wrong command used, please check your code"; return false; } } </code></pre> <p>My solution to deal with SSH read and write stream</p> <pre><code>//run any specified cmd using ssh //parameter: //cmd&gt;&gt;running command //tVal&gt;&gt;command run timeout //return:terminal stdout function runCMDAdvance($sshIP, $cmd, $tVal,$tarIP){ $ssh = new Net_SSH2($sshIP); if (!$ssh-&gt;login($this-&gt;username, $this-&gt;password)){ echo "Bad Login"; return false; } //set timeout for running ssh command, should be done after the ssh object has been initialized $ssh-&gt;setTimeout($tVal); //scp action detected if (strstr($cmd,"scp")!==FALSE){ //if this is the first time to do the ssh, connect with "yes" for answering if ($ssh-&gt;write($cmd)){ //store the output of the text message given by the router $ssh_stdout = $ssh-&gt;read(); //echo $ssh_stdout."|||"; if (strstr($ssh_stdout,'trusted ')!==FALSE){ $ssh-&gt;write("y\n"); //and ask password $ssh-&gt;read(' :password'); } //deal with known host situation elseif (strstr($ssh_stdout,'ssh-keygen ')!==FALSE){ $ssh-&gt;write('ssh-keygen -f '.'/root/.ssh/known_hosts -R '.$tarIP."\n"); $ssh-&gt;write($cmd); $ssh-&gt;read(' :password'); $ssh-&gt;write($this-&gt;password."\n"); return true; } //deal with still connecting situation elseif (strstr($ssh_stdout,'Are you sure you want to continue connecting')!==FALSE){ $ssh-&gt;write("yes\n"); $ssh-&gt;read(' :password'); $ssh-&gt;write($this-&gt;password."\n"); return true; } $ssh-&gt;write($this-&gt;password."\n"); $returnRead = $ssh-&gt;read(); $ssh-&gt;disconnect(); return true; }//end of inner inner if }//end of scp if (deal with scp) else{//if not scp action,just normal action,simply execute the command //exec will not return anything if the command is not run successfully $res = $ssh-&gt;exec($cmd); $ssh-&gt;disconnect(); return $res; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload