phpseclib ssh附加到屏幕进程exec命令回应响应

Hi I am trying to use Phpseclib to connect to my dedicated server and then attach to the screen process and then execute commands within the screen process and return the result,

I can get php to connect to the server and I think reattach to the screen process but the command I try to echo back just returns "1" instead of what I expected.

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '.././static/classes/phpseclib');

include('.././static/classes/phpseclib/Net/SFTP.php');

//SSH Run Command
$ssh = new Net_SSH2($host);
if (!$ssh->login($user,$pass)) {
    exit('Login Failed SSH');
}

//Run the server
echo 'Attaching to screen process...';
$ssh->exec('screen -r server1');
echo 'Attached';
echo $ssh->write('status
');
?>

If someone could maybe shed some light on my problem I would appreciate it.

Also some more info the reason I am connecting like this is because it is a SRCDS server I am connecting to and I could use a php script to query the srcds game server but it wont work because I am on shared webhosting which blocks the ports I need to socket connect to.

You probably need to do $ssh->write("screen -r server1 ") instead of $ssh->exec('screen -r server1');

The problem with exec() is that it always does commands on a new channel. This isn't a problem with phpseclib but with the SSH protocol. It's like doing "screen -r server1" via putty, closing putty, reopening putty, doing "status" and expecting it to output what you'd like. It won't.