Daniel290499 Posted June 17, 2020 Share Posted June 17, 2020 Hello all I'm new to this forum. I've been struggling on this problem for a few days now. I’m writing a script to automatically push code to a git server. I’m using php and running the script via the command line. After asking the user to enter the branch and commit message, the following code runs: exec(‘git config user.name "’ . $userName . ‘"’); exec(‘git config user.email "’ . $userEmail . ‘"’); exec('git checkout -b ’ . $branch); exec(‘git add --all’); exec(‘git commit -m "’ . $message . ‘"’); exec('git push origin ’ . $branch); When running the last command, the script stops and asks for a user name, then a password. How can I enter that username and password via the script? I don’t want to have to type in the user name and password each time. As I said, I've been stuck on this for 2 days now. I tried other forums, searching the net. I'm frustrated... Thanks in advance. Have a lovely day. Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/ Share on other sites More sharing options...
requinix Posted June 17, 2020 Share Posted June 17, 2020 Entering a username and password is not the right approach. Set up the SSH settings on the machine to use proper authentication with a private key. This can happen automatically without any prompting. Note that this is a general git/SSH issue. Nothing to do with PHP. Just make sure that you're applying these settings to the same user that is running PHP - which is probably not the usual user you're used to but instead "apache" or "nginx" or "www-data" or something else. Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/#findComment-1578953 Share on other sites More sharing options...
Daniel290499 Posted June 17, 2020 Author Share Posted June 17, 2020 Hi requinix Security isn't an issue, as the git server is on my local machine. At this point, it's really more about trying to figure out how to do this. It's an itch I need to scratch. There must be a way to automatically fill in a response, from a response of an executed command. You know what I mean? Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/#findComment-1578958 Share on other sites More sharing options...
requinix Posted June 17, 2020 Share Posted June 17, 2020 Yes, and I'm telling you that's not the right answer. The right answer is to set up a private key and modify your SSH configuration so the whole prompting problem completely goes away. Unfortunately if you search for stuff like "git ssh" you'll find a number of places that don't give the best answer. Generate your private key (that you can search for), associate it with the Git account, then follow this article on how to set it up with SSH. But like I said, make sure you do this for the correct user account on the machine. Which is probably not your personal account. When done right, you (and by "you" I mean whatever the correct user account was) can tell git to clone from github.com, or pull from it, or push to it, or whatever else, and the authentication will be completely handled for you. No prompts. Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/#findComment-1578961 Share on other sites More sharing options...
Daniel290499 Posted June 17, 2020 Author Share Posted June 17, 2020 Thanks for all that. I'll check out the SSH stuff. What if it's another command and not git? Let's say 'x-command' that you run via exec() requires an input; how would you input that data via a script? Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/#findComment-1578962 Share on other sites More sharing options...
requinix Posted June 17, 2020 Share Posted June 17, 2020 Then you would use the proc_* functions. They're complicated to understand and use (much more than this SSH thing I'm telling you about), but that is the best way to start up a process and send input to it (if you don't have any better alternatives). Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/#findComment-1578964 Share on other sites More sharing options...
Daniel290499 Posted June 17, 2020 Author Share Posted June 17, 2020 Yeah, I tried proc_. I couldn't get it to work. Have you got a working example? This is why I say, it's not about git. I'll setup my git properly, no problem. But this now is about learning something new That's why the title is about entering data in an executed command. It just so happens that I stumbled upon this, while fiddling with a local git server. Regardless of whether I use this for git, I want to add more knowledge to my PHP tool belt. You get what I'm saying? Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/#findComment-1578965 Share on other sites More sharing options...
gizmola Posted June 17, 2020 Share Posted June 17, 2020 One traditional way to get around this issue is to use Expect. Expect is one of the first automation languages I am aware of and is great for handling user input prompts that come from utility programs that might be used in a shell script. It's been around for a long time and there is a version for both for linux and windows. Are you using windows btw? All the system and process functions are closely tied to the OS you are using. Quote Link to comment https://forums.phpfreaks.com/topic/310949-php-command-line-enter-data-into-executed-function/#findComment-1578975 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.