ssh登录主机失败问题
import paramiko
try:
# 读取私钥文件内容
with open('id_rsa', 'r') as key_file:
private_key = paramiko.RSAKey.from_private_key(key_file)
# 创建 SSH 客户端实例
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接 SSH 主机
ssh_client.connect('192.168.2.203', port=22110, username='root', pkey=private_key)
# 执行命令
stdin, stdout, stderr = ssh_client.exec_command('ls')
output = stdout.read().decode('utf-8')
# 输出命令执行结果
print(output)
# 关闭 SSH 连接
ssh_client.close()
except paramiko.AuthenticationException as auth_exception:
print('认证失败:', auth_exception)
except paramiko.SSHException as ssh_exception:
print('SSH 连接失败:', ssh_exception)
except Exception as e:
print('发生异常:', e)
try {
$key = Storage::get('id_rsa');
$ssh = new SSH2('192.168.2.203', 22110);
if (!$ssh->login('root', $key)) {
return '无法登录到主机';
}
$command = 'ls'; // 运行的命令
$output = $ssh->exec($command);
return $output;
} catch (\Exception $e) {
return 'SSH 连接异常: ' . $e->getMessage();
}
凭什么python代码可以连接 php不能连接呢
我是docker环境 如果是ssh我的本机是可以的 不过我是密码登录
哎 要用 $rsa = PublicKeyLoader::load($key);