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我的本机是可以的 不过我是密码登录

chowjiawei
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
chowjiawei
最佳答案
 try {
            // 读取私钥文件内容
            $key = Storage::get('id_rsa');

            // 使用 PublicKeyLoader 加载私钥
            $rsa = PublicKeyLoader::load($key);

            // 创建 SSH 客户端实例
            $ssh = new SSH2('192.168.2.203', 22110);

            // 使用私钥登录
            if (!$ssh->login('root', $rsa)) {
                die('无法登录到主机');
            }
            // 执行命令
            $output = $ssh->exec('ls');
            // 输出命令执行结果
            echo $output;
            // 关闭 SSH 连接
            $ssh->disconnect();
        } catch (Exception $e) {
            echo '发生异常:' . $e->getMessage();
        }

哎 要用 $rsa = PublicKeyLoader::load($key);

4个月前 评论
讨论数量: 1
chowjiawei
 try {
            // 读取私钥文件内容
            $key = Storage::get('id_rsa');

            // 使用 PublicKeyLoader 加载私钥
            $rsa = PublicKeyLoader::load($key);

            // 创建 SSH 客户端实例
            $ssh = new SSH2('192.168.2.203', 22110);

            // 使用私钥登录
            if (!$ssh->login('root', $rsa)) {
                die('无法登录到主机');
            }
            // 执行命令
            $output = $ssh->exec('ls');
            // 输出命令执行结果
            echo $output;
            // 关闭 SSH 连接
            $ssh->disconnect();
        } catch (Exception $e) {
            echo '发生异常:' . $e->getMessage();
        }

哎 要用 $rsa = PublicKeyLoader::load($key);

4个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!