2

I am trying to auto update a website from remote git repo. As it's on a shared hosting i use a webhook on github & phpseclib 1.0. Here is my code:

$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");

the git command won't run. but when i do a git pull manually from terminal it works Thanks for your help

neubert
  • 15,947
  • 24
  • 120
  • 212
user3344311
  • 653
  • 1
  • 5
  • 6

2 Answers2

0

From your code:

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

Usually with interactive shells you need to wait for the prompt before you send the next command. Try something like this:

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

Sometimes identifying what the prompt is is easy - sometimes it's less so. Like if the prompt has colored text in it that would mean that the prompt is not what you think it is.

neubert
  • 15,947
  • 24
  • 120
  • 212
0

Here is the solution that worked for me

$ssh->setTimeout(2);
echo $ssh->write("cd ~/source\n");
$ssh->read();
echo $ssh->write("git pull origin master\n");
user3344311
  • 653
  • 1
  • 5
  • 6