How can I use PHP to set up an interactive SSH session?

I am trying to establish an interactive SSH connection to a remote server using PHP via the command line in Mac OS X 10.6. I am currently using the PHP proc_open function to execute the following command:

ssh -t -t -p 22 user@server.com

It almost works. The parameters are supposed to -t -tforce a pseudo-terminal, which they almost do. I can enter the SSH password and press enter. However, after clicking enter a terminal that just freezes. There is no output, there is nothing - it is as if the SSH session failed. I can’t run commands or anything else and have to kill it all using Ctrl + C. I know that login is successful, because I can execute a command like ssh -t -t -p 22 user@server.com "ls -la"and get the correct output.

I thought the problem should be due to the fact that I used standard channels in my proc_open call, so I replaced them with pty. I get the following error: "pty pseudo-terminal is not supported on this system ..."

Mac OS X just doesn't support pty or pseudo terminals? (I'm new to using all this shell terminology).

Here's the PHP code:

$descriptorspec = array(0 => array("pty"), 1 => array("pty"), 2 => array("pty"));  
$cwd = getcwd();  
$process = proc_open('ssh -t -t -p 22 user@server.com', $descriptorspec, $pipes, $cwd);  
if (is_resource($process))  
{  
    while (true)  
    {  
        echo(stream_get_contents($pipes[1]));  
        $status = proc_get_status($process);  
        if (! $status["running"])  
            break;  
    }  
} 

(Sorry - I can’t figure out the instructions for formatting SO for life ...)

What am I doing wrong? Why can't I use pty? Is it just not possible on Mac OS X? Thank you for your help!

+3
source share
5 answers

You should use public key authentication, and not try to circumvent online password verification programmatically.

, tty, , . -t -t . , PHP proc_open() .

:

# Generate keypair
ssh-keygen -t rsa

# Copy public key to server
scp ~/.ssh/id_rsa.pub example.com:.ssh/authorized_keys

# Now you shouldn't be prompted for a password when connecting to example.com
# from this host and user account.
ssh example.com

# Since the web server (and thus PHP) probably has its own user account...
# Copy the ~/.ssh/id_rsa file somewhere else
cp ~/.ssh/id_rsa /some_path/id_rsa

# Change ownership of the file to the web server account
chown www-data:www-data /some_path/id_rsa

# Fix the file permissions (ssh ignore the keyfile if it is world readable)
chown 600 /some_path/id_rsa

# Try connecting to the server through the web server account
su -c "ssh -i /some_path/id_rsa -o UserKnownHostsFile=/some_path/known_hosts example.com" www-data

# Add the host to the known hosts file when prompted


plink ( PuTTY Linux) OpenSSH, plink -pw password example.com. , , ps aux , .

sshpass, ssh.

+5

, PHP passthru(). ( ) , ssh svn ( , ), , , ( ) , && ssh: ssh -t -t -p 22 hostname command1 && command2 ... Mac OS X, . , , - ! , . " ", , , . Alexandre!

+3
+1

phpseclib, PHP SSH:

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

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->read('username@username:~$');
$ssh->write("ls -la\n");
echo $ssh->read('username@username:~$');
?>
+1

, , , proc_open:

Pty PHP, (. 10yr old https://bugs.php.net/bug.php?id=33147)

python . ssh, python script:

import sys
import pty

args = " ".join(sys.argv[1:])
pty.spawn(['/usr/bin/ssh', args])

pty.spawn python:

Create a process and connect its control terminal to the current io standard processes. This is often used for default programs that insist on reading from the control terminal.

+1
source

Source: https://habr.com/ru/post/1773196/


All Articles