Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图从PHP站点连接到远程Solaris服务器_Php - Fatal编程技术网

试图从PHP站点连接到远程Solaris服务器

试图从PHP站点连接到远程Solaris服务器,php,Php,我想通过PHP站点从Windows操作系统连接到Solaris服务器,在Solaris服务器上执行一些shell脚本。网站只是挂在那里,什么也没做 <?php exec('ssh root@192.168.175.128'); echo exec('cd Desktop'); echo exec('./chong.sh'); ?> 我猜这里的问题是,您正在通过ssh连接到Solaris Box,而没有对进程执行任何操作 当你调用ssh时root@192.168.175.128您

我想通过PHP站点从Windows操作系统连接到Solaris服务器,在Solaris服务器上执行一些shell脚本。网站只是挂在那里,什么也没做

<?php

exec('ssh root@192.168.175.128');
echo exec('cd Desktop');
echo exec('./chong.sh');

?>

我猜这里的问题是,您正在通过ssh连接到Solaris Box,而没有对进程执行任何操作

当你调用ssh时root@192.168.175.128您可以使用Solaris设备启动ssh会话。然后,此过程将挂起,等待您告诉它要做什么:

如果您没有设置证书,它可能会要求您输入密码。 即使不是这样,它也会像任何普通终端一样,挂起在远程框上的提示处等待命令。 即使如此,您仍试图在本地计算机上执行其他命令,并随后调用exec。为了在远程机器上执行任何操作,您需要将命令传递到您创建的ssh进程中

请尝试:


考虑一下,如果这是一台本地计算机,您不需要担心安全性,您可能会发现通过telnet而不是ssh连接更容易,因为如果您这样做,您可以简单地使用它,而不是混淆多个IO流,正如您需要使用proc_open所做的那样。

一个名为Chong的脚本挂在那里,什么也不做?可能是。当您在本地运行脚本时会发生什么?例如,从Solaris设备上的终端?ssh不需要一些用户输入,比如密码吗?另外,您确定“ssh”在您的Windows路径中吗?
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "errors.txt", "a") // stderr is a file to write to
);

$process = proc_open('ssh root@192.168.175.128', $descriptorspec, $pipes);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to errors.txt

    // Clear the input buffer before we send anything - you may need to parse
    // this to determine when to send the data
    while (!fgets($pipes[1])) continue;

    // You may need to send a password
    // fwrite($pipes[0],"password\n");
    // while (!fgets($pipes[1])) continue;

    // Send the first command and wait for a prompt
    fwrite($pipes[0],"cd Desktop\n");
    while (!fgets($pipes[1])) continue;

    // Send the next command
    fwrite($pipes[0],"./chong.sh\n");

    // Close the STDIN stream
    fclose($pipes[0]);

    // Fetch the result, output it and close the STDOUT stream
    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // Kill the ssh process and output the return value
    $return_value = proc_close($process);
    echo "\ncommand returned $return_value\n";

}