PHP-SSH2 连接到远程服务器时遇到问题

Having trouble with PHP-SSH2 connecting to remote server

我正在尝试编写我的第一个 PHP 脚本,我很挣扎,主要问题是 connection/exec 部分不起作用,而是抛出 "Could not connect to server" 留言...

尝试更改 if 语句的代码。

<?php
  include('Net/SSH2.php');
  //Coded by Aaron Akhtar
  //May not be the best code, this is my first ever php script ;P

  //edit the information below to match your ssh details or this will not work!
  $address = "104.xxx.xx.xx";
  $user = "root"; //do not change unless you are using another user from root
  $password = "xxxxx";

  $command = $_GET["command"];

  if(empty($command)){
    echo "Please specify a command to send to the server...";
    die();
  }

    function execute(){
    $connection = ssh2_connect($address, 22);
    if(ssh2_auth_password($connection, $user, $password)){
      echo ssh2_exec($connection, $command);
    }else {
      die("Could not connect to server...");
    }

  }

  execute();

 ?>

我正在尝试让它向我的远程服务器发送命令。

试试这些改变:

function execute($address,$user, $password, $command){
    $connection = ssh2_connect($address, 22);
    if(ssh2_auth_password($connection, $user, $password)){
        echo ssh2_exec($connection, $command);
    }else {
        die("Could not connect to server...");
    }
}

execute($address,$user, $password, $command);

您在函数内引用的变量实际上不可用,它们需要在调用函数时作为参数传入,就像我在上面的代码片段中所做的那样,或者在函数内全局声明它们.