Remote control with PHP and Python

Spread the word!

I will assume you have some basic knowledge of Python, PHP and Linux. My goal was to make a remote control of a linux based computer from any (android) mobile connected to through internet. You can find all the files on GitHub.

HOW DOES IT WORKS

How to realize a solution without consuming too much internet connection?
To solve this point I chose to take a single screenshot instead of streaming more of them in a video. Then I also lowered the quality of the images with PHP before printing it to the screen.

The .txt files
I created the following .txt files:

  1. “commando.txt” which contains what kind of function has to be executed on the remote computer
  2. “x.txt” which contains the x-position of the mouse if we want to click somewhere. If we want to write something in our remote computer “x.txt” will contain the text to write.
  3. “y.txt” which contains the y-position of the mouse.

The python files

  1. “foo.py” contains functions like taking the screenshot, click or writing something. You are free to add other customized functions there.
  2. “reader.py” calls periodically the functions from “foo.py”. This should run on the remote computer in order to let our mobile to control it.
  3. “cam.py” is a script taken from codeplasma.com for the webcam control.

The php files

  1. “index.php” contains the user interface. Pay always attention whenever you need to change address of any file if placed differently.
  2. “exec.php” receives the commands from index.php by JQuery/Ajax

The httpd.conf file

Search where your httpd.conf file is placed. In my case the path was /opt/lampp/etc/httpd.conf. There you have to give the permission in order to let you connect from your mobile to your server (the remote computer). Open the file as root and search for the section between <Directory “/your/path”> and </Directory> whereby in my case /your/path was /opt/lampp/htdocs (apache main directory location).

From Apache 2.4 you can write in this section the new line “Require all granted” to allow everybody to see your server. Or you can write “Require ip 127.0.0.1” (with some public ip address instead of 127.0.0.1) to allow the access just to that device. If this is not enough to reach your server, than you must also set the permissions from your router page.

Router permission

This procedure can be different according to your internet provider, but the basic idea is that you have to tell your router which device can access to localhost at some port (or port range) you can choose, for example port 81. Then you associate this port to another port on your computer, according to the service you want to use (in our case we are dealing with http request, for which the reserved port is 80). This process is called port forwarding. I reached the router setting by entering the address http://speedport.ip

speedport.ip

So we can finally access from our mobile device to http://my.server.public.ip.address:81

PROCEDURE

After downloading the files  from GitHub…

  1. Change any path inside the code if different from the real one
  2. Check if httpd.conf is correctly set as I explained before
  3. Run reader.py in your computer
  4. Run apache

Remember!
Give always the right permissions to write in the root of your apache server if needed.
If the python scripts give you errors it’s probably because some of the modules are not installed. I found pip useful to easily install some missing modules. You can install ist by giving:

For example the following lines will directly install pymouse and pyautogui modules used in our functions:

but you can always just google it whenever a module is missing 🙂

HOW TO USE IT
If I access from my mobile to my homepage at the port given in the setting of my router this is the result:

screenshot

You can just update the page to see the updated screenshot of the remote computer and use “LEFT”,”UP”,”DOWN”,”RIGHT” to scroll it. You can also write some text in the field above and then push “Write” to order your remote computer to write its content. The “Webcam” button will order the computer to take a screenshot: if you’ve never took a screenshot you need to push it two times, since by clicking “Webcam” you will visualize the penultimate (and not the last) screenshot taken from your computer. You can modify it to make it working better (and I’m sure I didn’t do the best job ever…), but I was just interested at its functionality and I wanted to keep the code as simple as possible.

If you click on the screenshot image some additional button will appear like “Click”, “Double Click”, “Shift+Click” and “Right Click” and by pushing one of them the corresponding action will be performed at the point where you clicked.

FUTURE DEVELOPMENTS

Some problems are still to be solved, but I was not really so interested in implementing its solutions. However I will write about it in the following section.

How to make the connection if both devices (mobile and remote) change their ip address dinamically?

I know there are solutions like signing up to some DNS services etc., but I would prefer here to avoid their use and just exploit the Google Drive service I already have (Yeah.. let’s trust Google :)). This will be the mechanism: on the mobile side I check my ip, copy it to a file in Google Drive (Ip1.txt). On the remote computer a python script will periodically check any difference between the actual ip address and the old one written in that file. If they are different python will update the permissions inside the httpd.conf file (allowing me to connect from the mobile) and also update periodically the ip address of the remote computer in another file whenever it changes (Ip2.txt).

Google Drive

Using Google Drive to handle with dynamic ips with ip1.txt (containing the updated public ip of the user) and ip2.txt containing the updated public ip of the remote computer. See Google Drive API and PyDrive for more details.

Be the first to comment

Leave a Reply

Your email address will not be published.


*