我对 php 相当陌生,目前正在为我的大学开发一个网站。我要做的是创建一个登录页面,允许用户建立与远程 SSH 服务器的连接,然后从 php 运行 linux 脚本。到目前为止,我发现了这段代码——我猜它应该允许用户登录。我试过运行代码,但它似乎不起作用。
编辑:当我运行代码时,页面只是说“函数 ssh2_connect 不存在”。
<?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("server1.example.com", 22))) {
echo "fail: unable to establish connection\n"; }
else {
// try to authenticate with username and password
if(!ssh2_auth_password($con, "username", "password")) {
echo "fail: unable to authenticate\n";
}
else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
if (!($stream = ssh2_exec($con, "ls -al" ))) {
echo "fail: unable to execute command\n";
}
else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
}
}
}
?>