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

时间:2019-07-10 10:28:53

标签: php ssh2-exec

我正在尝试编写有史以来的第一个PHP脚本,但我挣扎了很多,主要问题是连接/执行部分不起作用,而是抛出了“无法连接到服务器”消息... < / p>

试图更改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();

 ?>

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

1 个答案:

答案 0 :(得分:0)

尝试以下更改:

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);

您在函数内部引用的变量实际上并不可用,如我在上面的代码片段中所述,在调用函数时需要将它们作为参数传递,或者在函数内部进行全局声明。 / p>