I am not able to pull a file remotely from git server through php. I wrote the following BAT code and am trying to execute it via php.
the batch file git1.bat is a follows:
cd C:\repos\rep2 && git pull origin master 2>&1
the php code:
<?php
echo shell_exec("C:\\xampp\htdocs\AS-otg\\git1.bat");
?>
the output I get:
However, I get the required result when I do the same directly from cmd.
I tried some other git commands like log etc. which work just fine.
I need to do this via php only... please help.
log is a local command that does not need to talk with a remote host. pull does a fetch first. It seems you are running the PHP script under another user than when you run the script manually. If you run it manually you authenticate to the remote server with your SSH key and when the PHP script runs the script, the effective user does not have that SSH key to authenticate with.
Btw. you should keep in mind that a pull is not suited for being done non-interactively. When doing a pull you can easily get conflicts if the incoming changes are not fast-forward.
Related
I am using apache server under xampp. I have some matlab exe file that I want to execute. I used this template
$tmp = exec($command, $output, $return_var);
while $command contains the exact command to execute the file using cmd.
What is happening is the page hangs and when I debug, I found that the server hang while calling this exec command.
I searched the web and tried many things like run the Apache service and my user account and give the user all administrator privileges, but unfortunately it still stuck.
Any help or advice would be appreciated.
The problem with my code was because the Matlab code is accessing file system and also reads an excel sheet. So, it was all about permissions.
The solution for this problem has 2 parts:
1. Apache should run as a a windows service with checking allow service to interact with desktop in the properties.
2. I found that there is no way to access windows COM objects to read excel in normal way. So, instead we should use xlsread in basic mode.
That's all.
I've managed from each pull my server send me a notification. My php script, execute a shell script who is responsible to update my repos, but I'm getting this message
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
These are my files:
PHP: https://gist.github.com/rodreego/567176913cbe9d925907
Shell: https://gist.github.com/rodreego/964bd48287bff4eafc8a
I've tried to log my user who's executing this shell script, but he's coming blank. Is there something to do with it? I mean, If I run my scripts from my prompt shell, work fine, but when I attempt deploy from server notification, doesn't.
This is the first time I've tried to use the GitHub service hooks to automatically deploy to my server; and after slowly working my way over every obstacle it looks like the very last one is the one that has done me in.
The initial clone of my git repository onto my web server was done through SSH, and I'm pretty sure that has something to do with my problems now. My current script is the basic:
<?php
echo(shell_exec('git pull 2>&1'));
?>
But it returns "Permission denied (publickey). fatal: The remote end hung up unexpectedly"; which I know is where the script is failing to put in the SSH pass phrase I created. I tried doing research on how to maybe get past it, but I don't even know if those would be the correct/securest things to do.
So can this be modified to work from here or do I need to backtrack a few steps and step away from using SSH to begin with?
Thanks
Can you try to replace (in your git.config file )
ssh://github.com/<username>/<project>.git
or
https://github.com/<username>/<project>.git
with
git#github.com:<username>/<project>
Apache is probably running as a different user than the one you normally run git with via the CLI. You'll probably need to copy your .ssh/id_rsa file from /home/regularuser/ to /home/webuser/.
I have a CentOS 5.5 server running a local telnet daemon (which is only accessible from localhost) which prints out a list of active users on our accounting system when sent the correct commands through telnet. I need to make this information available on our intranet website which uses PHP.
I've written a Python script using the telnetlib modules to run locally on the CentOS server which simply captures the output of the telnet command and prints them to stdout. I've setup key based ssh between the web server and the CentOS server to run the python script remotely from the web server. I can execute the script successfully from the web server using ssh and it prints the list of users to stdout on the webserver. I was hoping to be able to execute this SSH command and capture the results into a variable to display on the website.
However, I can't get exec(), shell_exec(), passthru() or any other PHP exec function to display the data. I'm well aware that I may be approaching this from the totally wrong angle! What would be the best way to capture the output from ssh?
Why don't you redirect your stdout to a file. Then use your php website framework to read the file and display the results
I have a github account set up to my EC2 server with no issues. When i try to run a bash script to 'git pull' it wont do it. I will do a 'git status' and many other commands. Here is my sh file
cd /var/www/html/TDS/;
ls -la;
type git;
git status;
git remote -v;
git pull origin master;
echo "hello world";
All lines work except the git pull. I have tried git pull, git pull origin master, git fetch, git fetch origin master. I have ruled out all possibilities like permission issues and privileges.
This sh file is executed by hitting a PHP page, the PHP page looks like this
<?php
$output = shell_exec('/bin/sh /var/www/html/TDS/git.sh');
print_r("<pre>$output</pre>");
?>
Very simple and it works minus the Pull request. Any help would be amazing, I'm so close to getting this to work.
For a git pull to work, the user running it must have write permissions to the git repo's index (under .git/). Make sure the user under which the script is run (Apache?) has those rights.
...does PHP (www-data) have permissions? Is it the owner of the file?
Is this an ssh URL to the origin repository? Do you have ssh-agent running when you do it manually? Have you provided ssh agent access to the shell script (hint, the answers are Yes, Yes, No. Probably.)
So we have determined it is ssh access that is the problem. You then have two choices: getting ssh-agent credentials into the php process and allowing the php script access to ssh credentials without requiring a password. Both are problematic one way or another.
To get assh-agent credentials into the php process, copy the $SSH_AUTH_SOCK environmental variable from a shell into your php/shell script SSH_AUTH_SOCK=/tmp/ssh-wScioBA10361/agent.10361 git pull. Then assuming the php script has sufficient privs to access that file, git pull will work. This is problematic because you need to ssh into the system to get the auth sock, change the program to use the new socket (or write a program to find the current socket), and leave everything running. Log out, reboot, etc and you will lose git pull functionality.
The other option is to create ssh credentials for the php/shell user who is running git pull. Find the home directory, create .ssh, and ssh-keygen new keys for that user. You can set up the private key to not have a password so that anyone who can access this file (security risk!!) can ssh using those credentials. Add the public key to the authorized keys of the account who has access to the git repo (gitolite would allow you to restrict what privileges that account might have).