Forum

Full Version: Start/stop XBMC from web gui
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was looking for a way to run bash scripts from my phone or Ipad in a user friendly way. Main reason for this is that I want to be able to start XBMC and pause all downloads in one click, and vice versa.
I wasn't able to find anything that suited my needs so I decided to 'hack' something together. I say 'hack' because my knowledge on doing this in a neat way is limited.
The result for now is a web gui with two buttons:

[attachment=123]

Start XBMC button:
starts XBMC
pauses nzbget downloads, nzb scans and processing
pauses all downloads in transmission
shuts down couchpotato
shuts down sickbeard


Stop XBMC button:
stops XBMC
resumes nzbget downloads, nzb scans and processing
resumes all downloads in transmission
starts couchpotato
starts sickbeard



How did I put this together [guide/tutorial if you want]:

[1]Create bash scripts
Created the bash scripts:
Code:
sudo nano startxbmc.sh

Inserted the following lines:
Code:
#!/bin/bash
sudo /etc/init.d/xbmc start
sudo nzbget -c /usr/local/etc/nzbget/nzbget.conf -P D2
sudo nzbget -c /usr/local/etc/nzbget/nzbget.conf -P O
sudo nzbget -c /usr/local/etc/nzbget/nzbget.conf -P S
sudo transmission-remote --auth=xbian:raspberry --torrent all --stop
sudo /etc/init.d/couchpotato stop
sudo /etc/init.d/sickbeard stop

Made it executable:
Code:
sudo chmod +x startxbmc.sh

Did the same for the stopxbmc.sh script which contains the following:

Code:
#!/bin/bash
sudo /etc/init.d/xbmc stop
sudo /etc/init.d/sickbeard start
sudo /etc/init.d/couchpotato start
sudo nzbget -c /usr/local/etc/nzbget/nzbget.conf -U D2
sudo nzbget -c /usr/local/etc/nzbget/nzbget.conf -U O
sudo nzbget -c /usr/local/etc/nzbget/nzbget.conf -U S
sudo transmission-remote --auth=xbian:raspberry --torrent all --start

[2] Set up PHP webserver
Now we are going to need a webserver with php support to run the bash files from a web gui. I used the following guide for this:

http://www.simonthepiman.com/how_to_setup_a_web_server_with_php_support.php

In the step where the lighttpd.conf is edited you can also change the port if you would like.

[3] Design a web GUI
Now we have to build a basic web page with the buttons by changing index.php. I have set this up very simple with some basic formatting. The php code is below, I haven’t commented it yet sorry for that. It roughly contains 3 parts, the first part contains the php code that is called when a button is pushed. The second part contains the styling for the buttons en the final part is the html code for the web page.

PHP Code:
<?php
    
if (isset($_POST['startbutton']))
    {
        
exec('sudo /path/to/your/scriptfolder/startupscript.sh > /dev/null 2>&1 &');
    }

    if (isset(
$_POST['stopbutton']))
    {
        
exec('sudo /path/to/your/scriptfolder/shutdownscript.sh > /dev/null 2>&1 &');
    }
?>
<html>
<head>
<title>XBMC Control</title>

<style type="text/css">
.button_style1 {
    -moz-box-shadow:inset 0px 1px 0px 0px #97c4fe;
    -webkit-box-shadow:inset 0px 1px 0px 0px #97c4fe;
    box-shadow:inset 0px 1px 0px 0px #97c4fe;
    background-color:#3d94f6;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
    border:1px solid #337fed;
    display:inline-block;
    color:#ffffff;
    font-family:arial;
    font-size:8.0em;
    font-weight:bold;
    padding:6px 24px;
    text-decoration:none;
    text-shadow:1px 1px 0px #1570cd;
}.button_style1:hover {
    background-color:#1e62d0;
}.button_style1:active {
    position:relative;
    top:1px;
}
/* This imageless css button was generated by CSSButtonGenerator.com */
</style>

</head>
<body style="background-color:#000000">
<center>
<br>
    <form method="post">
    <p>
        <button name="startbutton" class="button_style1">Start XBMC</button>
    </p>
    </form>
<br>
    <form method="post">
    <p>
        <button name="stopbutton" class="button_style1">Stop XBMC</button>
    </p>
    </form>
</center>
</body>
</html> 

[4] Create nopasswd exceptions for bash scripts
Because there are some sudo commands in our website we want to make some exceptions so they can be executed without the need of a password. For this I implemented the following solution.
I have absolutely no idea how safe or unsafe this method is.

Code:
sudo visudo

Add the following line at the end:

Code:
www-data ALL=NOPASSWD: /path/to/your/scriptfolder/startupscript.sh, path/to/your/scriptfolder/shutdownscript.sh

[5] Push your buttons
Go to the website:

Code:
http://yourraspberrypi-IP

Or if you have changed the port in step 2

Code:
http://yourraspberrypi-IP:portnumber



What’s next:

[1] First of all I would like to know if someone knows there is already something similar to this (I was not able to find it), which is probably better than this. If there is not then:
[2] I would like to know if you think this method is safe and if not, how to implement this better.
[3] I would like to make the web gui better looking but I have close to none experience with this, I had the following things in mind:
- Adjust it automatically to screensize (so the buttons are not to small on a phone and not to big on an Ipad or computerscreen)
- Add some feedback to it (XBMC started successfully etc.)
- Disable the buttons for a minute after they are pressed.

So suggestions and recommendations on this and also on other aspects are more than welcome. I have put this together starting with close to no knowledge of php, html and linux, so there will be enough room for improvement.
Do you have an android or ios device?
I have an android phone and an Ipad.
On android you can use Coversal with the SSH plugin, there is also ScriptKitty among others.

Not knocking your work, just letting you know of these two great apps that will allow you to start/stop XBMC from your device.

I'm sure there are equivalent iOS applications but I don't know.

With ScriptKitty you can use per command widgets and you get a nice output screen:
Thanks I will look in to that. I just made this because I couldn't find something (for free) that could do this and it's a fun way of learning how this stuff works.
Nothing wrong with that Smile
Does anybody know Apps for iOS like ScriptKitty or Coversal for Android? Can't find anything in the Web or the App Store...
(11th Apr, 2013 12:21 AM)wuschl Wrote: [ -> ]Does anybody know Apps for iOS like ScriptKitty or Coversal for Android? Can't find anything in the Web or the App Store...

PilotSSH seems to be the closest ios ScriptKitty alternative, although the scripts would reside on the pi instead of locally.
Thx for your answer! I'll give it a try
Reference URL's