简体   繁体   English

在CodeIgniter控制器中建立SSH连接

[英]Establishing an SSH connection in a CodeIgniter controller

Recently I came across this library, which allows me to connect via ssh to a remote machine. 最近,我遇到了这个库,它使我可以通过ssh连接到远程计算机。 ( this case, a fake 123.45.67.890 . The library is shukydvir . (本例中为假冒123.45.67.890 。该库为shukydvir

I followed the instructions given in the overview tab, however, as shown below: 但是,我遵循“ overview选项卡中给出的指示,如下所示:

public function deploy_test()
{
    $params = array( 
                'hostname' => "123.45.67.890",
                'port' => 23,
                'username' => "username",
                'password' => "password"
                );

    if(!($con = $this->ssh->connect($params))) {
        echo json_encode("fail: unable to establish connection\n");
    }
    else {
        echo json_encode("helloworld");
    }
}

when what I got back in the webpage is: 当我在网页上看到的内容是:

fail: unable to establish connection

Thus, tired of that, I installed ssh2 library, and I restated my server: 因此,厌倦了这一点,我安装了ssh2库,并重述了服务器:

public function deploy_test() {
    $connection = ssh2_connect("123.45.67.890", 23);

    if(!$connection) {
       echo json_encode('Connection failed');
    }

    ssh2_auth_password($connection, "username", "password");
    $stream = ssh2_exec($connection, "ls -l");
    stream_get_blocking($stream, true);
    $output = "";
    while($line = fgets($stream)) {
        $output .= $line;
    }
    echo json_encode($line);
}

The results are not encouraging: 结果并不令人鼓舞:

<p>Severity: Warning</p>
<p>Message:  ssh2_connect(): Unable to connect to 123.45.67.890 on port 23</p>
<p>Filename: controllers/automation.php</p>
<p>Line Number: 70</p>

However, when I execute the same script from my test.php file, I get: 但是,当我从test.php文件执行相同的脚本时,我得到:

02/13/2013  10:41 PM              .
02/13/2013  10:41 PM              ..
02/13/2013  10:41 PM               952 FreeSSHd.lnk
               1 File(s)            952 bytes
               2 Dir(s)  55,343,263,744 bytes free

The other server has freSSHD installed, and it's ready to do connections. 另一台服务器已安装freSSHD ,并且已准备好进行连接。 I have tried everything I can. 我已经尽力了。 could someone please help me? 有人可以帮我吗?

CodeIgniter-SSH2库正在使用php的本机函数ssh2_connect(),该函数是基于sftp的安全Shell登录,并且基本上连接到端口22。尝试更改端口:

$connection = ssh2_connect("123.45.67.890", 22);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM