What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Shell scripts from WebUI - is it possible, advice, ideas

kstamand

Occasional Visitor
Is it possible to issue cli command from the router WebUI and return the results?
If yes, is there a script available where this is done already?​
If not, any advice and pointers of where I might start to learn how to do this (I've been looking at existing scripts like YazFi, scMerlin, and others for ideas but no luck finding one yet)?​

Background - I often ssh to the router to issue some quick status commands (brctl show, ethctl phy-map, iptables -vnl CHAIN). I'm familiar with Asuswrt user scripts and third party add-on API (I have a working model for adding a new user tab but I'm stuck there on how to proceed with .asp / .js code, hence my question). I've also successfully modified Main_LogStatus_Content.asp previously to allow for filtering returned syslog records >> https://www.snbforums.com/threads/c...abilities-working-prototype.87357/post-936846

Goal - from the Router WebUI, have a new Userx tab that allows for entering a CLI command with an "execute" button, which is then executed in background and then results returned to WebUI.

Edit: @jacklul - I was concerned that it may be a security concern, so thanks for that feedback and I definitely want to avoid those kind of issues.
@thelonelycoder - I look forward to your thoughtful feedback. FWIW, I only have a handful of commands I’m interested in. So I’m rethinking of a fixed list of command “names” that could be selected from the WebUI that I could then call a corresponding sh script might avoid the security concerns JackLul pointed out.
Thank you both for the feedback.
 
Last edited:
Sounds like terrible idea security-wise
It would be acceptable if these commands were hardcoded so the user can't just enter ANY command
 
Is it possible to issue cli command from the router WebUI and return the results?
If yes, is there a script available where this is done already?​
If not, any advice and pointers of where I might start to learn how to do this (I've been looking at existing scripts like YazFi, scMerlin, and others for ideas but no luck finding one yet)?​

Background - I often ssh to the router to issue some quick status commands (brctl show, ethctl phy-map, iptables -vnl CHAIN). I'm familiar with Asuswrt user scripts and third party add-on API (I have a working model for adding a new user tab but I'm stuck there on how to proceed with .asp / .js code, hence my question). I've also successfully modified Main_LogStatus_Content.asp previously to allow for filtering returned syslog records >> https://www.snbforums.com/threads/c...abilities-working-prototype.87357/post-936846

Goal - from the Router WebUI, have a new Userx tab that allows for entering a CLI command with an "execute" button, which is then executed in background and then results returned to WebUI.
There is a way, but it‘s not safe in my opinion.
However, I like your idea. A limited SSH tab is doable as it is more or less how we interact between the WebUI and our shell scripts.
Let me ponder this this Weekend.
 
This was a feature removed from the router 11 years ago.
Code:
  - REMOVED: Removed the Run Cmd page as it was a security
             risk.  This is also needed to keep in line with
             recent security fixes Asus applied to the
             httpd backend to limit what external processes
             it can run, otherwise any malicious page could
             run arbitrary commands on your router if you
             were currently logged on a separate tab.

 
Sounds like terrible idea security-wise
It would be acceptable if these commands were hardcoded so the user can't just enter ANY command
Like in a SSH terminal? All third party scripts require this interaction. I see no problem giving users the choice.
 
Something like Webmin that opens a terminal in its GUI? Am sure that Webmin would not do it if it wasn't secure...
 
However, I like your idea. A limited SSH tab is doable as it is more or less how we interact between the WebUI and our shell scripts.
There used to be one, and it was removed many years ago due to many obvious security issues with that.

Code:
commit c989700896de539d76a674930105c07902539501
Author: Eric Sauvageau <rmerl@lostrealm.ca>
Date:   Thu May 7 17:50:33 2015 -0400

    webui: completely remove the run command page to ensure no xsite scripting could exploit it
 
@jacklul, @dave14305, @thelonelycoder, @RMerlin would the following alternate approach still pose security concerns

- Custom user tab that has a hardcoded list of Names (prototype screen shot below)
- When clicking on one of the names, it somehow (I still don't know how to do that) runs a corresponding predefined script stored under /jffs someplace and the script doesn't accept any parameters
- The script runs and writes it's results to a file
- The custom user tab .asp reads the contents of the results file and displays it in a text box

If the above approach avoids the security concerns, can anyone point me to an example or reference on how to run a predefined script from .asp and wait until that shell script completes? I think I have the rest of the .asp code worked out, minus that one part.
1743174712043.png
 

Attachments

  • 1743174517149.png
    1743174517149.png
    65.4 KB · Views: 5
can anyone point me to an example or reference on how to run a predefined script from .asp and wait until that shell script completes?

You would pass the script name to be run from the ASP custom_settings variable and have a service-event script listen for that trigger and invoke your script. It’s the crux of the add-on API. Get the basics working then we can see how to make it look more responsive fetching the results via ajax calls.
 
I had a better and save way in mind to approach this.
Thanks for freeing up time this weekend ;)
 
I had a better and save way in mind to approach this.
Thanks for freeing up time this weekend ;)
Care to share the better and safe way, at least in outline form? I'm not in any hurry, just hungry to learn
 
Just use SSH. It's secure. it's sane, and it works just fine.

The web daemon security has been one of the primary source of router exploitation over the years. Don't make it worse than it already is.
 
Just use SSH. It's secure. it's sane, and it works just fine.

The web daemon security has been one of the primary source of router exploitation over the years. Don't make it worse than it already is.
Got it - thanks for the feedback
 
Consider using something like iOS Shortcuts to SSH into the router, run a command and popup the output. Example:
I do something similar with Android. Termux has a widget that lists the scripts I want to run. If I want to see the results, I put a read command at the end.
 
I am familiar with and have used iOS Shortcut for Diversion, very nice. My goal was to have a similar ability from the WebUI, so I can do everything in one place and not have to jump to different interfaces (SSH) or devices (iOS/Android).

All good feedback tho and much appreciated
 

You would pass the script name to be run from the ASP custom_settings variable and have a service-event script listen for that trigger and invoke your script. It’s the crux of the add-on API. Get the basics working then we can see how to make it look more responsive fetching the results via ajax calls.
I have the "bones" of this approach working, updating custom_settings.txt from the asp page, a services script firing when service-event is triggered and a script running which writes results to a file. Not sure how to go about reading those results back into the webpage textarea when the script completes / coordinated with the asp that triggered it tho.
 
Not sure how to go about reading those results back into the webpage textarea when the script completes / coordinated with the asp that triggered it tho.
Write the file under /www/ext/ and read it via ajax as text. See some examples in uiScribe.
 
Write the file under /www/ext/ and read it via ajax as text. See some examples in uiScribe.
What am I missing / doing working (code snippet below)? I keep getting 404 Not found with my ajax call for the URL reference.

I've tried '/ext/results.txt', '/www/ext/results.txt', and 'results.txt' in the url with no luck. I'm assuming the path I am using is what's wrong, but I'm not sure how to debug nor what the correct format is. I have confirmed the file resides at /www/ext/results.txt

Thanks for any assistance.

Code:
function get_results_data(){
 24     var h = 0;
 25     $.ajax({
 26         url: '/www/ext/results.txt',
 27         timeout: 2000,
 28         dataType: 'text',
 29         error: function(xhr){
 30             alert('ajax read results.txt status: ' + xhr.status + ' Status Text: ' + xhr.statusText);
 31         },
 32         success: function(response){
 33             var resultString = htmlEnDeCode.htmlEncode(response.toString());
 34             var _string = String.split('\n');
 35             h = $("#textarea").scrollTop();
 36             document.getElementById("textarea").innerHTML = _string;
 37             $("#textarea").animate({ scrollTop: 9999999 }, "slow");
 38         }
 39     }
 40 )}
 

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!

Staff online

Back
Top