php使用ssh2通过账密连接服务器失败
问题描述:
php直接通过ssh2拓展链接服务器,结果发生了catch
//建立ssh2连接
$ssh2 = ssh2_connect($public_ip, 22);
if (!$ssh2) {
Log::error('连接服务器失败');
//重新建立ssh2连接
sleep(10);
$ssh2 = ssh2_connect($public_ip, 22);
if (!$ssh2) {
Log::error('再次连接服务器失败');
return;
}
}
Log::info('成功连接上了服务器');
$root_pwd = config('elastic_service.root_pwd');
if (!ssh2_auth_password($ssh2, 'root', $root_pwd)) {
Log::error('登录失败');
return false;
}
报错如下:
ssh2_auth_password(): Authentication failed for root using password
解决:修服务器的sshd的配置,再重启即可.
注:对于弹性创建的服务器,使用ssh2连接时,即使有了IP地址,就直接连接服务器会报:
ssh2_connect(): Unable to connect to 139.xxx.xxx.99 on port 22.
解决:由于系统需要时间反应,故最好等待几十秒再通过ssh2链接,就能正常链接上了

