使用 PHP SSH 连接到远程服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18799799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:14:12  来源:igfitidea点击:

SSH Connection to a remote server using PHP

phpssh

提问by Alireza Fallah

I want to make a SSHconnection to a remote server using php.

我想使用 php 与远程服务器建立SSH连接。

Im using php 5.3 on Linux/CEntOS.

我在 Linux/CEntOS 上使用 php 5.3。

What I have done so far :

到目前为止我所做的:

$connection = ssh2_connect('192.168.1.22', 22);
ssh2_auth_password($connection, '_username', '_password');
$stream = ssh2_exec($connection, 'ls -l');

But I'm getting this error :

但我收到此错误:

Fatal error: Call to undefined function ssh2_connect()

致命错误:调用未定义的函数 ssh2_connect()

So, my questions are :

所以,我的问题是:

  1. Are ssh2_*functions not installed by default in php?

  2. Do I need an extension or library for using these functions?

  3. How can I solve this problem ?

  1. ssh2_*功能默认不安装PHP?

  2. 我是否需要扩展或库来使用这些函数?

  3. 我怎么解决这个问题 ?

回答by Ronak Patel

You can configure your server (Ubuntu):

您可以配置您的服务器(Ubuntu):

sudo apt-get install libssh2-php
sudo service apache2 restart

回答by Alireza Fallah

phpseclib

phpseclib

  • Download it from hereor here( direct link ) ,

  • Include it to the project ,

  • 这里这里(直接链接)下载它,

  • 将其包含到项目中,

And then :

进而 :

   include('Net/SSH2.php');
   $ssh = new Net_SSH2('www.example.com');
   $ssh->login('username', 'password') or die("Login failed");
   echo $ssh->exec('command');

回答by Tobias

ssh_* function are not per default installed in php core. You have to add them using pecl, see php manual for a good description: http://de1.php.net/manual/en/ssh2.installation.php

ssh_* 函数不是默认安装在 php 核心中。您必须使用 pecl 添加它们,请参阅 php 手册以获得很好的描述:http://de1.php.net/manual/en/ssh2.installation.php