Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Executing shell script from web sever via URL/button?

    Hey guys,

    I am trying to execute a shell script to shutdown a FreeNAS serve from my web server. I want to be able to click a button or just type in a url to execute the script from my raspberry pi that will shutdown a different computer (freenas).

    Currently, I have setup ssh keys and I can connect from my raspberry pi to freenas fine through ssh.

    This is my command that successfully turns off my freenas when executing it from terminal/putty whilst already sshed into my raspberry pi:

    Code:
    pi@raspberrypi ~ $ sudo ssh root@192.168.2.141 -i /etc/ssh/freenas-root_rsa 'shutdown -p now'
    Shutdown NOW!
    shutdown: [pid 16089]
    Shutdown NOW!
    ^^^^ this works.

    Now onto the next step which I am having trouble with.

    I created a .sh script e.g. shutdown.sh and put this into /usr/lib/cgi-bin directory. Script is as below. Permissions set to 755 so it's executable.

    Code:
    #!/bin/bash
     
    sudo ssh root@192.168.2.141 -i /etc/ssh/freenas-root_rsa 'shutdown -p now'
    Now when I try to execute it by url:

    Code:
    http://192.168.2.11/cgi-bin/shutdown.sh
    nothing happens to my frenas server.

    I do get a server 500 error but i assume this is because there is no html for the page to load.

    How can I get this script to work when executing it from a html <a>, or <button> button or just from entering a URL as above.

    Thanks

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    I think you have to use the exec() function instead to run the script. You can't run .sh remotely via url.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hey tangoforce,

    I have also tried that.

    I have a php page e.g. index.php that does this:
    <?php
    if ($_GET['run']) {
    # This code will run if ?run=true is set.
    exec("/cgi-bin/shutdown.sh");
    }
    ?>

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    </head>

    <body>

    <!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
    <a href="?run=true">FreeNAS Shutdown</a>

    </body>
    </html>
    but this still does not shutdown my freenas.

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    On a Linux machine a server is a 'service' i.e. a daemon. For instance to start and stop Apache, which is designated as httpd, the d is for daemon, you just use

    service httpd start

    or service httpd stop

    Using backslash notation on a php page it would be

    `service httpd stop`

    I'm quite sure that if you find the right designation for freenas you could just make the substitution.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •