PHP SSH2 SSH2_连接
因此,我从php.net中选取了一个示例来了解php ssh2的工作原理 所以代码是PHP SSH2 SSH2_连接,php,ssh,libssh2,Php,Ssh,Libssh2,因此,我从php.net中选取了一个示例来了解php ssh2的工作原理 所以代码是 <?php $connection = ssh2_connect('shell.example.com', 22); if (ssh2_auth_password($connection, 'username', 'secret')) { echo "Authentication Successful!\n"; } else { die('Authentication Failed...'); } ?&
<?php
$connection = ssh2_connect('shell.example.com', 22);
if (ssh2_auth_password($connection, 'username', 'secret')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
?>
但是,当我输入正确的信息时,它不会给出任何错误。问题是shell.example.com实际上不存在,对
ssh2\u connect()
的调用无法返回资源
您的代码应该先检查ssh2\u connect()
是否成功建立连接并返回资源,然后再尝试使用ssh2\u auth\u password()
的资源
我想它告诉我ip没有退出,这就是它给我问题的原因,因为当我给一个正确的ip时,它没有给我错误。尝试在不正确的主机上使用上面的代码,您也会得到一条更有用的错误消息。不,我现在只使用了一个空白页,它没有说“无法连接到服务器”。如果不希望请求失败,您必须在某个时候捕获异常。如果您的代码无法连接,您将希望在页面上显示该连接,当然,跳过将要使用该连接的任何代码。您在哪个操作系统上运行该连接?
Warning: ssh2_connect(): php_network_getaddresses: getaddrinfo failed: Name not known in
/var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/ssh2.php on line 2 Warning: ssh2_connect(): Unable to connect to
shell.example.com on port 22 in /var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php on
line 2 Warning: ssh2_connect(): Unable to connect to shell.example.com in
/var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php on line 2 Warning:
ssh2_auth_password() expects parameter 1 to be resource, boolean given in /var/zpanel/hostdata/zadmin/public_html/whothehellcareswhatisthis/login/ssh2.php
on line 4 Authentication Failed...
$connection = ssh2_connect('some.valid.ssh.host', 22);
if (!$connection) {
throw new Exception("Could not connect to server.");
}
if (!ssh2_auth_password($connection, 'name', 'password')) {
throw new Exception("Authentication failed!");
}
// SSH connection authenticated and ready to be used.