我使用php和ssh,并希望执行图形程序,如gnome-calculator。我该怎么做呢?这是我的代码:
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("localhost", 22))){
echo "fail: unable to establish connection'n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "terminator", "root")) {
echo "fail: unable to authenticate'n";
} else {
// allright, we're in!
echo "okay: logged in...'n";
$get = $_GET['cmd'];
// execute a command
if (!($stream = ssh2_exec($con, $get ))) {
echo "fail: unable to execute command'n";
}
else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while($line = fgets($stream)) {
echo nl2br($line);
}
fclose($stream);
}
}
}
?>
`
我可以执行ls -a或mkdir等命令。但我不能得到像一个应用程序来执行图形界面(如执行gnome计算器),我该怎么做呢?我这样使用我的代码:http://localhost/index.php?cmd=ls%20-a
只是在胡闹!
你希望它会从浏览器内部弹出x窗口应用程序吗?根据HTTP和X11应用程序的概念,这是不可能的。
这不可能。PHP在任何数据传回客户端机器之前在服务器上执行。如果您尝试执行任何应用程序,它将以您指定的用户(或web服务器的用户,如果没有设置)在服务器上运行,结果在服务器上解释(如果它们被捕获),然后必须在网页上呈现给客户端。
要在客户端机器上运行外部程序,您需要使用该语言在客户端机器上运行的其他系统。这样的系统是否存在还有待进一步讨论(换句话说,我不知道,所以我不打算为自己挖一个洞)
你需要考虑的明显问题是:如果客户端机器没有安装和可用的应用程序,你该怎么办?