Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute program in php

Is there a way to execute a program on the server, Example: Firefox, Gimp, Etc.

My concept is to be able to log into my local LAMP server on my phone, type in a command on a text box (eg: /usr/bin/firefox) and press a button which sends the command string using the post method to another PHP script that starts the program on my server. This would be good so when I'm on the road and want to start "firefox http://www.blahblah.net" and come back to my computer with firefox opened up. Yes, I am aware of the security issues but it is just a proof-of-concept.

I've tried the exec() and system() commands but they don't seem to work.... Am I doing something wrong?

#!/usr/bin/env ruby

require 'rubygems'
require 'sinatra'

disable :protection # not needed on something this simple
set :port, 11111    # think 1APPX

get '/' do
# this handles both apps (via ?app=...) and files (via ?file=...)

if params[:app] then
    # as a side effect, this also happens to actually run the app, which is
    # pretty much what we wanted in the first place
    @fn = params[:app][1..-2];
    `/usr/bin/env #{@fn}`
elsif params[:file] then
    # bugfix, remove quotes...
    @fn = params[:file][1..-2];
    # xdg-open anyone?
    puts "DEBUG /usr/bin/env xdg-open #{@fn}";
    `"/usr/bin/env xdg-open #{@fn}"`
else
    # nothing...
    404
end

end

not_found do
status 404
"Application #{@fn} not found. Usage: /?app=\"[appname]\" or /?file=\"[filename]\""
end
like image 269
joshumax Avatar asked Jan 27 '26 10:01

joshumax


2 Answers

Don't use a web application for this. The security hurdles are many, and would require extensive knowledge of Linux systems administration to make this happen.

My advice is to get an SSH client for your phone - they exist for Android and iOS, and connect to your computer via SSH. You can also get VNC clients for both those platforms so that you could operate directly on the GUI without having to trick X11 into launching applications on a display you're not actually viewing.

like image 91
Michael Berkowski Avatar answered Jan 29 '26 03:01

Michael Berkowski


In first place, you should running your apache server with the user which will be log. Next, you should to execute:

<?php
system("export DISPLAY=:0; /usr/bin/firefox;");

I tested here and worked

like image 34
Ismael Vacco Avatar answered Jan 29 '26 01:01

Ismael Vacco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!