Title: Sending input to the blocking stream of PHP ssh
Tags: php;ssh;stream;blocking
Question: I have similar situation as the following question
SSH2 change a user password

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). 

```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);
        }
    }
```

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.)

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??

Again, thanks all for the help!

Referring to neubert's answer, using phpseclib still not work...

```//$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;
        }
    }
```

My solution to deal with SSH read and write stream

```//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;
        }
    }
```


Here is the accepted answer: Here's how you could do it with phpseclib, a pure PHP SSH2 implementation:

```<?php
include('Net/SSH2.php');

define('NET_SSH2_LOGGING', 3);

$ssh = new Net_SSH2('www.website1.com');
$ssh-&gt;login('username', 'password');

$ssh-&gt;write("ssh cristiangonzales@example.net\n");
$ssh-&gt;read(' password:');
$ssh-&gt;write("password\n");

$ssh-&gt;setTimeout(0.5);
echo $ssh-&gt;read();
```

Comment for this answer: Thanks for the method, but I have tried, still hang my script as before, don't know which part went wrong...
Comment for this answer: Hi neubert, sorry for late reply, I have finally figured it out, this is my program mistake, not related to the code, I can now do the read and write stream successfully using phpseclib, I will post the working code later this week, thanks all for the great help especially neubert!
Comment for this answer: I've updated my code to add `decine('NET_SSH2_LOGGING', 3)`. This'll output the raw unencrypted SSH packets that are being sent to and fro. If you could post that that'd be great. Thanks!

Here is another answer: ```scp``` (as well as ```ssh```) do not expect a password but from the standard input, but from the console (as in ```tty```), and this is a Good Thing. Storing your password anywhere such as some PHP code compromises the very purpose of ssh.

Instead you should create a RSA authentication key. Here is a simple how-to.

Once you are done, change your command line as follows:

```scp -i /path/to/your/rsa.private.key
```

Make sure to store this private key in a "secure" location. It must be accessible from your PHP script, but no one else. In particular do not make it accessible from a URL.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:07/04/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><ssh><stream><blocking>', 'Title': 'Sending input to the blocking stream of PHP ssh', 'ViewCount': 1540, 'id': 17469458, 'user_AboutMe': None, 'user_AccountId': 2916204.0, 'user_DisplayName': 'user2499325', 'user_Reputation': 417.0, 'user_Views': 40.0, 'user_id': 2499325.0}}