Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


Talkiet

4793 posts

Uber Geek

Trusted

#261441 27-Nov-2019 21:42
Send private message

I am competent at several things, website development is NOT one of those things and google is throwing up a LOT of results and I don't even know enough about how to intelligently narrow the search...

 

I am trying to find a way to create a webpage on a linux machine that reads a file of several remote machines that are either online, or not - and presents through the browser a list of these, coded (by colour/icon) to show whether each is currently online or offline (That's easy enough for me to create a CSV or even HTML I guess with a cron job or something)... The major bit I am wondering where to start is that each line should have a few buttons beside it and each button should kick off a command on the webserver. (In this case the command/script will be to remotely execute a job on the remote machine through SSH)...

 

 

 

All the "hard" work of the underlying capability is done - this is about the presentation so someone doesn't need to log into a machine and remember/enter CLI commands to kick off the commands.

 

I've heard people say "just use php or cgi" but that's a bit generic for me at the moment.

 

Server is Apache, I have full control of it.

 

Any starting point (ideally an example of how to click on an icon on a web page and execute a command on a server) would be greatly appreciated.

 

Cheers - N

 

 





Please note all comments are from my own brain and don't necessarily represent the position or opinions of my employer, previous employers, colleagues, friends or pets.


Create new topic
marpada
476 posts

Ultimate Geek


  #2361869 27-Nov-2019 22:18
Send private message

It's easy enough to do it from scratch using any programming language, but I'd recommend you to use any existing tool for managing remote servers like RunDeck or Ansible Tower/AWX.




itxtme
2102 posts

Uber Geek


  #2361879 28-Nov-2019 00:09
Send private message

When you say "click on an icon on a web page and execute a command" what method of communication are you referring to?  I get that you want to run the webpage locally but how do you plan on executing the command remotely?  Are you having HTTP servers on the remote computers too or are you planning on SSH'ng in from the local webpage?


michaelmurfy
meow
13263 posts

Uber Geek

Moderator
ID Verified
Trusted
Lifetime subscriber

  #2361885 28-Nov-2019 06:23
Send private message

You can essentially use anything. Python3 + Flask or NodeJS come to mind here.

Just use Apache mod_proxy to proxy through to the web app.




Michael Murphy | https://murfy.nz
Referral Links: Quic Broadband (use R122101E7CV7Q for free setup)

Are you happy with what you get from Geekzone? Please consider supporting us by subscribing.
Opinions are my own and not the views of my employer.




Talkiet

4793 posts

Uber Geek

Trusted

  #2362027 28-Nov-2019 09:10
Send private message

itxtme:

 

When you say "click on an icon on a web page and execute a command" what method of communication are you referring to?  I get that you want to run the webpage locally but how do you plan on executing the command remotely?  Are you having HTTP servers on the remote computers too or are you planning on SSH'ng in from the local webpage?

 

 

By having the webserver run a remote bash script via SSH on the target machine...

 

The only thing exposed on the remote machines is SSH, and that is only exposed via an openvpn tunnel back to the main machine. They are not open to the real world at all for inbound connections.

 

Cheers - N





Please note all comments are from my own brain and don't necessarily represent the position or opinions of my employer, previous employers, colleagues, friends or pets.


chevrolux
4962 posts

Uber Geek
Inactive user


  #2362078 28-Nov-2019 09:38
Send private message

Noob PHP guy here....

 

So I would say, the 'buttons' on your page (oh and use Bootstrap CSS to make everything look nice) are just hyperlinks to the appropriate PHP script that triggers the shell script. I would guess the same script is used for all the servers, just with different variables?

 

The URL of the buttons would be, for example, 'https://talkiet.co.nz/shell_script.php?server=host1&action=reboot'

 

Then in 'shell_script.php', you can can just pull out that info from the $_GET variable by referencing whatever key you used in the URL.... $_GET['server'] or $_GET['action']

 

Finally to trigger the shell script... there is the php command 'exec()' or 'shell_exec()' which runs commands on the system... I've used exec() successfully, not sure of the benefits of exec vs shell_exec.

 

Lastly, just stick "header('Location: /home');"... substituting your index page of course for location, so the php script take you back to the main page after running. Good thing about exec is that's it's non-blocking so you don't need to wait for it to run.

 

Edit: I guess there are security concerns running shell scripts from web pages, but I assumed you already realise that and the page won't be publicly accessible.


Talkiet

4793 posts

Uber Geek

Trusted

  #2362112 28-Nov-2019 09:51
Send private message

chevrolux:

 

Noob PHP guy here....

 

So I would say, the 'buttons' on your page (oh and use Bootstrap CSS to make everything look nice) are just hyperlinks to the appropriate PHP script that triggers the shell script. I would guess the same script is used for all the servers, just with different variables?

 

The URL of the buttons would be, for example, 'https://talkiet.co.nz/shell_script.php?server=host1&action=reboot'

 

Then in 'shell_script.php', you can can just pull out that info from the $_GET variable by referencing whatever key you used in the URL.... $_GET['server'] or $_GET['action']

 

Finally to trigger the shell script... there is the php command 'exec()' or 'shell_exec()' which runs commands on the system... I've used exec() successfully, not sure of the benefits of exec vs shell_exec.

 

Lastly, just stick "header('Location: /home');"... substituting your index page of course for location, so the php script take you back to the main page after running. Good thing about exec is that's it's non-blocking so you don't need to wait for it to run.

 

Edit: I guess there are security concerns running shell scripts from web pages, but I assumed you already realise that and the page won't be publicly accessible.

 

 

Many thanks! That post has given me a good starting point to look at. And yeah - there are no real security concerns. The system is closed off and only available on LAN. The server and all remote clients are on VPN.

 

 

 

Cheers - N





Please note all comments are from my own brain and don't necessarily represent the position or opinions of my employer, previous employers, colleagues, friends or pets.


hio77
12999 posts

Uber Geek

ID Verified
Trusted
Lizard Networks

  #2362410 28-Nov-2019 20:05
Send private message

michaelmurfy: You can essentially use anything. Python3 + Flask or NodeJS come to mind here.

Just use Apache mod_proxy to proxy through to the web app.

 

/shudders now your talking like our bot teams!

 

 

 

few options here, will reach out to ya neil :)





#include <std_disclaimer>

 

Any comments made are personal opinion and do not reflect directly on the position my current or past employers may have.

 

 


 
 
 

Trade NZ and US shares and funds with Sharesies (affiliate link).
Hibino
166 posts

Master Geek


  #2363387 30-Nov-2019 11:54
Send private message

You can have 3 php files to do it. The exec() function in php can catch reply from the command it runs, so you can use the output of your script to make it extendable.

 

since all calls are ssh so you will need to have private/public key auth, you won't be able to enter password for ssh in exec().

 

 

 

a config php file, contains ip address of servers and commands so you can easily change later

 

 

//ssh username
$username = 'abcd';

 

// script to check server status, the script can output a digit for different status
$status_cmd = '/a/b/c/s.sh'; 
// colors for the output of server status script 
$status_color = array(
    '' => '#000000', // error connect or nothing from the script
    '1' => '#ff0000',
    '2' => '#00ff00',
    '3' => '#0000ff',
);

 

// list of servers
$servers = array(
    'name_1' => '1.1.1.1',
    'name_2' => '2.2.2.2',
    'name_3' => '3.3.3.3',
);

 

// commands for each server
$cmds = array(
    'btn_1' => '/a/b/c/d/a.sh',
    'btn_2' => '/a/b/c/d/b.sh',
    'btn_3' => '/a/b/c/d/c.sh',
);

 

 

 

 

a server list php file, loop through the server and commands display on page

 

 

require_once( 'config.php' );

 

// get all status, you can use ajax calls so it won't block page displaying, but it is LAN so this should be fast enough
$server_status = array();
foreach( $servers as $s_name => $ip ){
    $output = array();
    exec( "ssh {$username}@{$ip} {$status_cmd}", $output );
    $server_status[$s_name] = !empty( $output ) ? $output[0] : '';
}

 

// display a table of all servers
echo '<table width="100%">';
foreach( $servers as $s_name => $ip ){
    echo '<tr>',
         '<td>', $s_name, '</td>',
         '<td>', $ip, '</td>'.
         '<td bgcolor="' ,$status_color[$server_status[$s_name]], '">&nbsp;</td>',
         '<td>';
    foreach( $cmds as $btn_name => $cmd ){
        // commands are run in new window/tab, you can use ajax or iframe to have result in same page
        echo '<a href="run.php?server=' .$s_name. '&btn=' .$btn_name. '" target="blank"></a>, ';
    }
    echo '</td>',
         '</tr>';
}
echo '</table>';

 

 

 

 

a php to run the command from button press

 

 

require_once( 'config.php' );

 

$s_name = $_GET['s_name'];
$btn_name = $_GET['btn_name'];
$output = array();
exec( "ssh {$username}@{$servers[$s_name]} {$cmds[$btn_name]}", $output );
if( !empty( $output ) ){
    echo( implode( '<br>', $output ) );
}else{
    echo 'error with command';
}

 


Create new topic





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.