php - PHP-SSH2 连接到远程服务器时遇到问题
问题描述
我正在尝试编写我的第一个 PHP 脚本,但我很挣扎,主要问题是连接/执行部分不起作用,而是抛出“无法连接到服务器”消息......
尝试更改代码 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);
您在函数内部引用的变量实际上并不可用,它们需要在调用函数时作为参数传入,就像我在上面的代码片段中所做的那样,或者通过在函数内部全局声明它们。
推荐阅读
- python - GeoPandas 多图中的共享图例
- c++ - 使用MinGW将Linux下的wxWidgets交叉编译到WINdows
- python - n 维井字棋对角线检查器(预测谁将一举获胜)
- php - 创建条件短代码回显 - WP/Woocommerce
- python-3.x - 无法在 Jetson Nano 上安装 scikit_learn
- python - 使用每个类的 ImageDataGenerator 仅使用 N 个图像
- python - 使用 Pandas 的 Vlookup 在选择 2 列的 2 个文件之间合并
- generics - Kotlin T 值不能用返回任何函数的函数赋值
- java - 为什么 Integer.parseInt 方法不适用于拆分字符串?
- kubernetes - 为 GCP 上的负载平衡器应用注释以获取 GKE 上的源 IP 防火墙规则