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.




4297 posts

Uber Geek

Trusted

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

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 the product of my own brain and don't necessarily represent the position or opinions of my employer, previous employers, colleagues, friends or pets.


Create new topic
316 posts

Ultimate Geek


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

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.


1708 posts

Uber Geek

Subscriber

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

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?


 
 
 
 


/dev/null
9024 posts

Uber Geek

Moderator
Trusted
Lifetime subscriber

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

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.






4297 posts

Uber Geek

Trusted

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

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 the product of my own brain and don't necessarily represent the position or opinions of my employer, previous employers, colleagues, friends or pets.


4322 posts

Uber Geek


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

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.




4297 posts

Uber Geek

Trusted

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

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 the product of my own brain and don't necessarily represent the position or opinions of my employer, previous employers, colleagues, friends or pets.


'That VDSL Cat'
11504 posts

Uber Geek

Trusted
Spark
Subscriber

  # 2362410 28-Nov-2019 20:05
One person supports this post
Send private message quote this post

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.


 
 
 
 


142 posts

Master Geek


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

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



Twitter and LinkedIn »



Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Vodafone integrates eSIM into device and wearable roadmap
Posted 17-Jan-2020 09:45


Do you need this camera app? Group investigates privacy implications
Posted 16-Jan-2020 03:30


JBL launches headphones range designed for gaming
Posted 13-Jan-2020 09:59


Withings introduces ScanWatch wearable combining ECG and sleep apnea detection
Posted 9-Jan-2020 18:34


NZ Police releases public app
Posted 8-Jan-2020 11:43


Suunto 7 combine sports and smart features on new smartwatch generation
Posted 7-Jan-2020 16:06


Intel brings innovation with technology spanning the cloud, network, edge and PC
Posted 7-Jan-2020 15:54


AMD announces high performance desktop and ultrathin laptop processors
Posted 7-Jan-2020 15:42


AMD unveils four new desktop and mobile GPUs including AMD Radeon RX 5600
Posted 7-Jan-2020 15:32


Consolidation in video streaming market with Spark selling Lightbox to Sky
Posted 19-Dec-2019 09:09


Intel introduces cryogenic control chip to enable quantum computers
Posted 10-Dec-2019 21:32


Vodafone 5G service live in four cities
Posted 10-Dec-2019 08:30


Samsung Galaxy Fold now available in New Zealand
Posted 6-Dec-2019 00:01


NZ company oDocs awarded US$ 100,000 Dubai World Expo grant
Posted 5-Dec-2019 16:00


New Zealand Rugby Selects AWS-Powered Analytics for Deeper Game Insights
Posted 5-Dec-2019 11:33



Geekzone Live »

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


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



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.

Alternatively, you can receive a daily email with Geekzone updates.