Is this possible?
What I want to do is send:
run_app.exe -param 'test' -name 'tester'
to a windows cmd line from PHP.
Is this possible or do I need to write a windows service that is somehow triggered by the application?
You can use exec() for that.
Have you tried exec?
Or you can use:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run(strCommand, [intWindowStyle], [bWaitOnReturn]);
Here you can find the run method params: http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.85%29.aspx
And here is the COM class doc: http://www.php.net/manual/en/class.com.php
With this method you can do so much more in windows :).
I used it becaus of the [bWaitOnReturn] parameter which I couldn't do using any other method.
Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');
$strCmd1 = 'run_app.exe -param "test" -name "tester"';
$return1 = $shellObj->exeCmd($strCmd1);
The return will give you the command return OR error from cmd, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.
Related
I am trying to run kubectl virt commands to manage my virtual machine via PHP. First, I log in to my server with phpseclib with the following code:
$ssh = new SSH2('localhost');
if (!$ssh->login('root', 'rootPassword')) {
throw new \Exception('Login failed');
}
This part works fine, and when I try to run $ssh->exec('whoami && echo $PATH'), I get the following output:
root
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
But, whenever I try to run kubectl virt via PHP, I get the following output:
error: unknown command "virt" for "kubectl"
kubectl and kubectl virt work perfectly fine when I run them via terminal but somehow do not work with PHP exec(). I also tried to check the $PATH via terminal and I get a different output:
/root/.krew/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I thought that it may be because of $PATH but the interesting part is when I try to run sudo kubectl virt via terminal I also get the same error:
error: unknown command "virt" for "kubectl"
At that point, I am completely lost and don't even know where to look for a problem. I am thankful for all the answers.
When you are issuing ad-hoc ssh commands, you are not using interactive shell, and depending on your default shell behavior it may or may not load your .bashrc file . See https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command and Running command via ssh also runs .bashrc? for more details.
So by default, krew modifies your PATH variable, and appends it's bin path to it, i.e. my config contains export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH". But what exactly is kubectl plugin? Usually it's just a single binary, with kubectl-plugin_name name. So by invoking which kubectl-virt you can easily know where is your virt binary located and invoke it directly, so something like
$ssh->exec('~/.krew/bin/kubectl-virt')
should work
The other way is to modify PATH all by yourself, setting PATH=$PATH:~/.krew/bin should make it work, at least in my case
ssh localhost 'PATH=$PATH:~/.krew/bin kubectl virt'
worked nicely.
You can try to force loading of .bashrc in your shell configuration, but personally i think it's a bad practice, and ssh commands are not usually loading rc files for a reason, command execution speed and consistency between systems are the first things that come to mind.
Regarding sudo, it's actually not that surprising, because without -E or -i flags it won't load your current environment / won't start interactive shell. See https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc for more info
I am trying to call a python script from php which have proper execution permission but the script contains some of the commands for which only the root has the permission. So how can i make sure that those commands runs properly from the webservice???
I followed this link: Running a Python script from PHP but could not understand how to do it. If someone could explain, it will be a great help.
I will give you the usual warning regarding having PHP anywhere near root. I only do this because you mention this is a webservice (public facing?).
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//Setting the second argument in getShell():
//true will return a shell with root
//false will return a shell with the php execution user
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('python /full/path/to/python_script.py');
I have the following problem:
I hava a xampp sever running and I want it to execute a powershell. A php triggers a .bat file which contains the following code:
#echo
cd C:\OpenBR\bin
start /WAIT br -algorithm FaceRecognition -compare C:\xampp\htdocs\upload C:\xampp\htdocs\DP C:\xampp\htdocs\results\result.csv
start /WAIT C:\xampp\htdocs\CSVconvert\sortieren.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\Removedouble.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\remove_path.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\remove_foo.ps1
start C:\xampp\htdocs\CSVconvert\remove_quoatation.ps1
The first part works fine, up until the point when i want to exec the powershell "sortieren.ps1". When I run the batch manually, it executes and does the job, when triggered via php, it doesn't.
I set "Set-ExecutionPolicy Unrestricted" in both x86 and x64 shells.
I am just confused because the normal command line works and powershell doesn't, even after setting it on unrestricted.
I viewed
executing a Powershell script from php
and
PowerShell on Windows 7: Set-ExecutionPolicy for regular users
but couldn't solve the problem.
What did i miss?
The session you are running those commands in doesn't have the same environment variables as when you are using PowerShell to run them manually. You'll have to specify the absolute path to the powershell executeable and the scripts that you want to run so that they will be found.
start /WAIT C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\xampp\htdocs\CSVconvert\sortieren.ps1
Since the problem was the environment i thought you might benefit from a package that handles that aspect automatically. Here is a project that allows PHP to obtain and interact dynamically with a real Powershell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');
$strCmd1 = 'first command from first script';
$return1 = $shellObj->exeCmd($strCmd1);
$strCmd2 = 'second command from first script';
$return2 = $shellObj->exeCmd($strCmd2);
Instead of triggering a single script you can just trigger each command individually and handle the return. You can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script.
I'm using GREE Labs' Dbus PHP Extension in my attempts to make a PHP class that is able to create desktop notifications.
$dbus = $dbus = dbus_bus_get(DBUS_BUS_SESSION);
$message = new \DBusMessage(DBUS_MESSAGE_TYPE_SIGNAL);
$message->setDestination("org.freedesktop.DBus");
$message->setAutoStart(true);
$dbus->sendWithReplyAndBlock($message, 1);
When my code is run I get the following error:
Warning: dbus_bus_get() [function.dbus-bus-get]: failed to create dbus
connection object [Unable to autolaunch a dbus-daemon without a
$DISPLAY for X11] in [...COI/GTK/Notify.php on line 39
This is the first time I've used anything related to dbus, and am rather lost.
I'm aiming for an effect similar to what occurs when one executes the following in a terminal (on Ubuntu 11.10):
/usr/bin/notify-send -t 2000 'title' 'message'
I did initially use notify-send & exec, but switched when I found the GREE Dbus extension as I thought it may provide a cleaner interface. Also notify-send would only work properly if I changed my apache user to be the same as the user I'm currently logged in as - not an ideal solution.
Would anyone be able to either tell me what modifications are required to achieve my desired result, or alternatively tell me if what I want to do is in fact impossible?
Or, is there another way I should be doing this?
Dbus does not like being run while in a command line environment, without X. It's frustrating, but this is what I wrote in python to override that. It comes down to setting two environmental variables.
def run(self):
os.environ['DBUS_SESSION_BUS_ADDRESS'] = "unix:path=/run/dbus/system_bus_socket"
os.environ["DISPLAY"] = ":0"
try:
bus_name = dbus.service.BusName(INTERFACE,
bus = dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name,
'/com/your/path/here')
gobject.MainLoop().run()
except Exception, E:
f = file('/tmp/bus.log', 'a')
f.write(str(E))
f.close()
EDIT: I forgot another very useful way to run dbus on the command line
eval 'dbus-launch --auto-syntax' [command]
I use it on a raspberry pi to run my custom dbus deamons. dbus-launch --auto-syntax is a command that outputs environmental variables and files applicable to dbus in bash. The eval command will take that output and evaluate it so your command will run with those environmental variables.
A simple test would be to run something like this:
eval 'dbus-launch --auto-syntax' python /usr/bin/my-dbus-daemon.py
eval 'dbus-launch --auto-syntax' qdbus org.dbus.method /org/dbus/method/test
Use dbus-launch in the script that starts your web server in order to start an appropriate DBus daemon at the same time. See the dbus-launch(1) man page for details.
Is it possible to execute cmd commands in Windows OS with PHP exec() function?
I tried this:
<?php
try {
echo exec(
'O:\test\pdftk.exe O:\test\outputs\OPP\out.pdf O:\test\outputs\OPP\out2.pdf cat output O:\test\123.pdf'
);
} catch (Exception $e) {
echo $e->getMessage();
}
Basically, I'm trying to merge two pdf files with the pdftk program. If I just write the same exact command to the cmd by hand, it works and the O:\test\123.pdf file is created. But when I execute the above PHP file, nothing happens (blank page, the file is not created).
Can your PHP user access cmd.exe? You might find the tools at Microsoft's Sysinternals very useful; particularly the process monitor.
Try escaping the directory separator:
exec("O:\\test\\pdftk.exe O:\\test\\outputs\\OPP\\out.pdf O:\\test\\outputs\\OPP\\out2.pdf cat output O:\\test\\123.pdf");
Or even better, use single quotes instead:
exec('O:\test\pdftk.exe O:\test\outputs\OPP\out.pdf O:\test\outputs\OPP\out2.pdf cat output O:\test\123.pdf');
Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');
$strCmd1 = 'O:\\test\\pdftk.exe O:\\test\\outputs\\OPP\\out.pdf O:\\test\\outputs\\OPP\\out2.pdf cat output O:\\test\\123.pdf';
$return1 = $shellObj->exeCmd($strCmd1);
The return will give you the command return OR error from cmd, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.
try Executing using the Admin Privileges for command prompt