1

我有一个使用 ssh 代理登录到远程服务器的脚本。它会尝试克隆一个私有 git 存储库。但是,我无法让 github 接受登录。

<?php
$ssh_agent = new \phpseclib\System\SSH\Agent();
$connection = new \phpseclib\Net\SSH2('www.example.com');
$connection->login('example-username', $ssh_agent);

// the clone fails
$commands = array(
    'cd ~/example-path/',
    'ls -la',
    'git clone git@github.com:github-username/repo-name.git'
);
echo $connection->exec(implode('; ', $commands));

// this *does* work
$commands = array(
    'eval $(ssh-agent)',
    'ssh-add ~/.ssh/local-key-copied-on-the-server',
    'cd ~/example-path/',
    'ls -la',
    'git clone git@github.com:github-username/repo-name.git'
);
echo $connection->exec(implode('; ', $commands));

最后一个确实有效。但它也要求我在服务器上拥有密钥,我宁愿阻止。

当我手动执行此操作时,它可以工作。

$ ssh -A example-username@www.example.com
$ cd ~/example-path/
$ git clone git@github.com:github-username/repo-name.git

这是使这项工作起作用的转发,如果我跳过-A,则克隆失败。

在脚本中,我已经验证了:

  • 代理(由脚本使用)持有密钥(ssh-add -l列出它);
  • github 接受通过部署密钥(这是我正在使用的密钥)克隆特定的 repo;
  • 我没有使用其他密钥登录,即我暂时从我的 github 帐户中删除了我的普通个人密钥;

但脚本可能不使用转发。这是phpseclib的限制吗?我可以以使用转发的方式设置本地 ssh 代理吗?

4

0 回答 0