title: Automated git clone on remote server via ssh, with agent forwarding
body: <p>I have a script which uses the ssh agent to login to a remote server. It than tries to clone a private git repository. However, I can't get github to accept the login.</p>
  
  <pre><code>&lt;?php
  $ssh_agent = new \phpseclib\System\SSH\Agent();
  $connection = new \phpseclib\Net\SSH2('www.example.com');
  $connection-&gt;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-&gt;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-&gt;exec(implode('; ', $commands));
  </code></pre>
  
  <p>The last one <em>does</em> work. But it requires me to have the key on the server as well, which I rather would prevent.</p>
  
  <p>When I do this manually, it works.</p>
  
  <pre><code>$ ssh -A example-username@www.example.com
  $ cd ~/example-path/
  $ git clone git@github.com:github-username/repo-name.git
  </code></pre>
  
  <p>It is the forwarding that makes this work, if I skip the <code>-A</code>, the clone fails.</p>
  
  <p>In the script, I have verified that:</p>
  
  <ul>
  <li>the agent (used by the script) holds the key (<code>ssh-add -l</code> lists it);</li>
  <li>github accepts cloning the specific repo via a deploy key (which is the key I'm using);</li>
  <li>I'm not logging in with another key, i.e. I've temporarily removed my normal personal key from my github account;</li>
  </ul>
  
  <p>But the script probably doesn't use the forwarding. It is a limitation of phpseclib? Can I set up the local ssh agent in such a way that it uses the forwarding?</p>