【问题标题】:pull remote from github with phpseclib使用 phpseclib 从 github 远程拉取
【发布时间】:2018-03-19 05:50:05
【问题描述】:

我正在尝试从远程 git repo 自动更新网站。因为它在共享主机上,所以我在 github 和 phpseclib 1.0 上使用 webhook。这是我的代码:

$ssh = new Net_SSH2(SITE_DOMAIN);
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('/home/'.SSH_USERNAME.'/.ssh/'.KEYPAIR_NAME));
if (!$ssh->login(SSH_USERNAME, $key)) {
 throw new Exception('Failed to login');
 exit('Login Failed');
}
echo $ssh->write("cd ~/source\n");
echo $ssh->write("git pull origin master\n");

git 命令不会运行。但是当我从终端手动执行 git pull 时,它可以工作 感谢您的帮助

【问题讨论】:

    标签: github ssh phpseclib


    【解决方案1】:

    来自您的代码:

    echo $ssh->write("cd ~/source\n");
    echo $ssh->write("git pull origin master\n");
    

    通常使用交互式 shell,您需要在发送下一个命令之前等待提示。试试这样的:

    $ssh->read('#[prompt]#');
    echo $ssh->write("cd ~/source\n");
    $ssh->read('#[prompt]#');
    echo $ssh->write("git pull origin master\n");
    

    有时识别提示是什么很容易 - 有时不太容易。就像提示中有彩色文本一样,这意味着提示不是您认为的那样。

    【讨论】:

      【解决方案2】:

      这是对我有用的解决方案

      $ssh->setTimeout(2);
      echo $ssh->write("cd ~/source\n");
      $ssh->read();
      echo $ssh->write("git pull origin master\n");
      

      【讨论】:

        猜你喜欢
        • 2020-08-10
        • 1970-01-01
        • 1970-01-01
        • 2014-08-30
        • 2011-06-14
        • 1970-01-01
        • 2011-12-11
        • 2016-11-11
        相关资源
        最近更新 更多