How to exit shell mode in system() - php

I have a php script which should use some preset system aliases. I.e. "alias ll=ls -l"
In terminal "ll" works but from php system("ll") outputs
sh: ll: command not found
How do I exit "sh" and execute my terminal commands?
P.S.: May be I missunderstood the basic linux components shell and bash. In this case, please correct me/the post

The PHP docs aren't clear about this, but presumably PHP's system is a reflection of Standard C's system(3), which hands the argument command to the command interpreter sh(1). If you want to avoid the shell, you'll need to use another feature of PHP besides system (like explicit fork/exec). That's context, but it won't help you solve your problem.
In your case it seems you just want the aliases in an rcfile. Scripts invoked by system calls aren't going to read your rcfile unless you take extraordinary steps to make that happen, and even if you do, it's going to be non-obvious to the maintenance programmer. I'd strongly advise you to put the alias in the script or command argument itself, or simply call the command explicitly (ls -al).
You can also manually source the rcfile from your script, or call system(csh -i 'yourcommands') to force csh to be invoked as an interactive shell (which should cause your rcfile to be read). I think this is a bad idea because it is effectively forcing the shell to behave inconsistently with its environment, but I'm including it here for completeness.
Most of the above I got from a quick read through the Startup and shutdown section of the csh manual on my Mac (Mavericks). There are other potential solutions there that I haven't laid out, as well.

Related

How to import custom bash functions to be used in PHP exec/system/etc?

I'm writing a command-line application that will substitute a bunch of bash functions and manual work made by a team of developers. Currently, half of what we usually do is inside a ~/.functions file that is sourced in the ~/.bash_profile of each developer.
My command-line application is written in PHP, and for a while I would need to run some of those functions from inside my application. However, the following code would not work, the output says it cannot find the given function:
exec('bash my_legacy_functions.sh');
exec('my_custom_legacy_function param1 param2');
I may be wrong, but I could understand that every exec() call runs a command in a separate process, meaning the functions would not be available for subsequents exec() calls. Is this right, and if yes, would it be possible to override this behaviour without having to bundle everything into one call?
In the end it turns out the default shell was not bash, and on top of that source is not a common command in bash. I found by this other question's answer that the solution is something like:
function run($cmd) {
exec("bash -c 'source my_legacy_functions.sh; $cmd'");
}

Unable to delete Linux user using PHP and bash script

I'm able to delete linux users directly from the shell using the command ./del_user.sh
del_user.sh is as below
#!/bin/sh
userdel username
When I run this script from a PHP page, it doesn't work.
echo shell_exec('./del_user.sh');
Both the files are in the same directory.
What could be the problem?
del_user.sh does not take an argument so you're running the script on whatever hardcoded user is in the script. This won't work unless you modify the script to allow a command line argument.
While this task can be accomplished by PHP, this is certainly not a primary function and should probably be delegated to a more suitable application.
As an aside, you haven't stated what your goal is or what this script is supposed to do. Is this part of some user management application or is it simply a one-off for some small task?
Answer:
To enable this, you'd have to give Apache the ability to sudo so it can gain temporarily raised priveleges. You may have to do some googling on how to do this depending on your OS but this link provides some direction on how to do it in Ubuntu: Ubuntu Forums
I would also recommend against using a bash script as its not really necessary to do this. You could use a PHP script that accepts a command line argument. So in your main script you could have something like this: shell_exec('php /path/to/del_user.php username');
Then in del_user.php you'd have something like shell_exec('userdel '.$argv[1]);. More info on commandline arguments can be found here: $argv
Lastly, you could put it directly into your main script instead of using shell_exec twice. In other words, just use shell_exec('userdel '.$username); instead of calling a script. Since Apache will be able to sudo, it should be able to execute this directly.

What are the differences of system(), exec() and shell_exec() in PHP?

It is possible to run an external command by three PHP functions of
system();
exec();
shell_exec();
but what are their differences? In spite of their specific applications, in most cases, the can be equally used. I am curious to know which is preferred one when they can be equally used. For example, for unzipping a file or compressing a folder (with tar command), which one is preferred (probably from performance point of view)?
UPDATE: In another question, I found a very useful link describing different aspects for these functions. I share the link here, as other may use to better understand security issues and other aspects.
exec — Execute an external program
system — Execute an external program and display the output
shell_exec — Execute command via shell and return the complete output as a string
so if you don't need the output, I would go with exec.
Further details:
http://php.net/manual/en/function.exec.php
http://php.net/manual/en/function.system.php
http://php.net/shell_exec

How to open an application via php and perl?

I am trying to print generated forms / receipts through PHP (the printers will be installed on the server, I am not attempting to print to a user's local printer). I have decided to try the following methodology:
IN PHP:
Generate a PDF file and save it on the server.
Call a perl script to print said PDF file.
IN perl:
Use system() to "open" Reader and print the given PDF silently.
What works:
I can generate PDFs in PHP.
I can call a perl script.
If the script has errors, they report to the browser window. ie: If I purposely change file paths it fails, and reports the appropriate reason.
functions such as printf seem to work fine as the output displays in the browser.
The exact same perl script (with the "non-functioning" line mentioned below) works properly when executed from the command line or the IDE.
What doesn't work:
In perl: system('"C:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe" /N /T "C:\\test.pdf" 0-XEROX');
What happens:
NOTHING! I get no errors. It just flat out refuses to open Adobe Reader. All code below this line seems to run fine. It's like the function is being ignored. I am at a loss as to why, but I did try a few things.
What I've tried:
Changed permissions of the AcroRd32.exe to Everyone - Full Control.
Output the $? after the system() call. It is 1, but I don't know what 1 means in this case.
Verified that there are no disable_functions listed in php (though I think this is unrelated as shell_exec seems to be working, since some of the perl code is ran).
Various other configurations that at least got me to the point where I can confirm that PHP is in fact calling the perl script, it just isn't running the system() call.
Other info:
Apache 2.2.1.7
PHP 5.35
Perl 5.12.3 built for MSWin32-x86-multi-thread
WampServer 2.1
I'm at a loss here, and while it seems like this is an Apache / permissions problem, I cannot be sure. My experience with Apache is limited, and most of what I find online is linux commands that don't work in my environment.
Try this:
my #args = ('C:/Program Files (x86)/Adobe/Reader 10.0/Reader/AcroRd32.exe');
if (system(#args) != 0) {
# Can't run acroread. Oh Noes!!!
die "Unable to launch acrobat reader!\n";
}
The thing about system() is that it does two different things
depending on the number and type(s) of argument it gets. If the
argument is an array or if there are multiple arguments, Perl assumes
the first is the program to run with the rest as its arguments and it
launches the program itself.
If, however it's just one string, Perl handles it differently. It
runs your command-line interpreter (typically CMD.EXE on Windows) on
the string and lets it do what it wants with it. This becomes
problematic pretty quickly.
Firstly, both Perl and the shell do various kinds of interpolation on
the string (e.g. replace '//' with '/', tokenize by space, etc.) and
it gets very easy to lose track of what does what. I'm not at all
surprised that your command doesn't work--there are just so many
things that can go wrong.
Secondly, it's hard to know for sure what shell actually gets run on
Windows or what changes Perl makes to it first. On Unix, it usually doesn't matter--every shell does more or
less the same with simple commands. But on Windows, you could be
running raw CMD.EXE, GNU Bash or some intermediate program that
provides Unix-shell-like behaviour. And since there are several
different ports of Perl to Windows, it could well change if you
switch.
But if you use the array form, it all stays in Perl and nothing else
happens under the hood.
By the way, the documentation for system() and $? can be found here and here. It's well worth reading.

How do you run a command line program (like lame or svn) with PHP?

Specifically, I need to automate the encoding of audio files into mp3 with LAME. You don't need to know LAME to answer this, I could be talking about svn or some other program..
I know how to use LAME on the command line to do this, for one file at a time.
I would like to do this via a php script however, so I can convert a bunch at once (for instance, all the files in a directory)
So what I am confused about, is how I should invoke the program, LAME. I could definitely use
shell_exec()
http://php.net/manual/en/function.shell-exec.php
But is that a "screwy" way to do it, since I am going through the shell?
Should I be using lame_enc.dll somehow instead, instead of lame.exe?
It seems like I could somehow do it with exec() also http://php.net/manual/en/function.exec.php
But in that case, how would I supply the arguments?
Or is there a better way to do it, maybe a .bat file? I am running windows
Should I be using lame_enc.dll instead of lame.exe somehow?
You can use exec() and specify arguments just like you would on the command line. Other options are outlined on the Program Execution manual page for PHP.
It's possible to do it with PHP. Not a typical use case scenario but it can be done. Since you are on Windows, a bat file would be better suited since then you don't need the PHP parser to run the script.
Put the same commands you would run in the console to convert your audio files with LAME in a *.bat. Then run the bat as if it was a regular executable file.

Categories