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.
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'm trying to execute a command on my Raspberry Pi via SSH and get the result of it in my PHP script on my Windows machine. Currently I can execute the command on my RasPi, but I do not get any results back into the PHP script.
The code I'm Using for this:
<?php
$cmd = "C:\\path_to_putty\\putty.exe -ssh pi#RasPiIP -pw raspberry -m C:\\path_to_test.txt\\test.txt";
$result = shell_exec($cmd);
echo $result;
?>
For sending commands to my RasPi the code works. I have tested multiple times by as example changing test.txt to sudo reboot and it worked as intended.
I'm using PuTTY to send my command (test.txt is currently nfc-list which returns connected Scanners etc not important right here) to the RasPi.
What I want to achieve is that $result contains the returned data when my command is executed.
Is it even possible to do that? If yes how (any help appreciated). If no, are they maybe other ways to approach this?
Addressing the possible duplicate: I am using a Windows Machine and also I'm trying to get the result (of the one command) to reuse in my PHP script. In the other question, user is trying to save the full console log and save it to another file.
First, do not use PuTTY. PuTTY is a GUI application intended for an interactive use. Use Plink, which is command-line/console equivalent of PuTTY intended for command automation. Being a console application, it has a standard output, which can be read in PHP (PuTTY as a GUI application does not have standard output).
With Plink, you can also specify the command on Plink command line, so you do not need to create the test.txt command file.
In any case, there's no way to make PuTTY or Plink separate an output of command only (at least not from a command-line).
But what you can do, is to print some header/trailer to distinguish the start and end of the command output, like:
plink.exe -ssh pi#RasPiIP -pw raspberry "echo start-of-command && command && echo end-of-command"
And then in PHP, you can look for the start-of-command and end-of-command to identify what part of Plink output is really the command output.
In any case, you better use a PHP SSH library to achieve what you want, rather then driving an external application. For example phpseclib. But that's a completely different question.
In a terminal window running alert 9 gives me a popup saying 9
However, running alert 9 in shell_exec gives nothing.
What should I be aware of?
Just to mention: other commands work neither that interact with the GUI. I am using gnome 3.16.3
UPDATE
My concrete goal is to run an xdotool command using the gnome environment. ( xdotool key ctrl+shift+u )
alert is not a command, but an alias for notify-send. Aliases are loaded only in interactive shell, so it is not the best choice to test shel_exec.
These 2 command work well with php-cli:
$ php -r "shell_exec('notify-send 9');"
$ php -r "shell_exec('xdotool key ctrl+shift+u');"
The first one pops the 9 in notification area, and the last one output underscored 'u'.
Apache with mod_php is a daemon, subprocess of init, which knows nothing about xserver. If, for example, you run php built-in web server from a terminal within xserver like $ php -S 0:8080 it works as expected.
alert is not a standard command, at least on RedHat, Solaris and AIX, so you have to mention it in your question that it's a proprietary command/program
Your alert script/program does'nt consider to flush text data in standard or file output, so you can't do any redirection using pipe |, or redirect >, and finally, the php function shell_exec() can catch anything
Maybe you can redirect graphic data (i'm not sure) but generally, it's used by the graphic server
I believe PHP is launching a shell that isn't aware of your X11 environment and you need to set the $DISPLAY environment variable. Try the following modification to your shell_exec:
shell_exec("DISPLAY=\":0\" alert 9");
I would like to be able to either launch php in interactive mode via phing or via PHP (worst case scenario, phing can run an adhoc task with the desired code).
I've gotten this far:
<?php
$cmd = 'php -d auto_prepend_file=bootstrap.php -a';
passthru($cmd)
And the above almost gets me what I want. I enter a shell I can interact with, but the prompts are gone (the php > at the start of each line), all meta-commands (e.g., \>) totally fail, and typing exit; does nothing instead of exit the shell. So, as you can see, this isn't the ideal shell. Is this even possible without installing phpsh?
Thanks in advance.
I think PsySH will give you want you want. It's a PHP REPL that gives you the option to configure it to automatically include a bootstrap file.
I am having an issue using the PHP function shell_exec().
I have an application which I can run from the linux command line perfectly fine. The application takes several hours to run, so I am trying to spawn a new instance using shell_exec() to manage better. However, when I run the exact same command (which works on the command line) through shell_exec(), it returns an empty string, and it doesn't look like any new processes were started. Plus it completes almost instantly. shell_exec() is suppose to wait until the command has finished correct?
I have also tried variations of exec() with the same outcome.
Does anyone have any idea what could be going on here?
There are no symbolic links or anything funky in the command: just the path to the application and a few command line parametes.
Some thing with you env
See output of env from cli (command line interface) and php script
Also see what your shell interpreter?
And does script and cli application runs from one user?
If so, se option safe_mode
Make sure the user apache is running on (probably www-data) has access to the files and that they are executable (ls -la). A simple chmod 777 [filename] would fix that.
By default PHP will timeout after 30 sec. You can disable the limit like this:
<?php
set_time_limit(0);
?>
Edit:
Also consider this: http://www.rabbitmq.com/