[SOLVED] How to write to syslog via shell script from webUI?

Post new topic   This topic is locked: you cannot edit posts or make replies.    DD-WRT Forum Index -> General Questions
Goto page 1, 2  Next
Author Message
nomsy
DD-WRT Novice


Joined: 24 May 2024
Posts: 6

PostPosted: Fri May 24, 2024 22:32    Post subject: [SOLVED] How to write to syslog via shell script from webUI? Reply with quote
TL;DR: How do you debug log anything?! Mod edit: "solution" link removed. Your 'solution' is incorrect.

I wrote my bash/sh script, I tested it locally in a sh/bash commandline, everything is great, then I copy pasta'd it in dd-wrt > Administration > Commands > Command Shell Commands

Clicked Run Commands.

Naturally, nothing happened...

So time to debug. But how to debug? Obviously the docs don't even mention it: https://wiki.dd-wrt.com/wiki/index.php/Logs

Did massive amounts of internet searches.
- doesn't work: echo "#################"
- echo + this system log diversion doesn't work: exec 1> >(logger -s -t $(basename $0)) 2>&1
- doesn't work: logger "#################"
- doesn't work: logger -s -t "#################"

I see a mention or two in these forums of using "logger" -- from where? from Commands? It doesn't show up in Status > Syslog.

Is the Commands box and the Custom Script box just placebo? Smile No, because I found others able to use it: (similar to what I'm doing: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=308836), but nobody prints to log so idk how I can see what isn't working..

The WORST thing about this, is it's not even reporting that "HEY A SCRIPT TRIED TO RUN AND THREW ERRORS" --- Nothing.

Please, anyone Very Happy

---------------------------

SSH is grayed out for me in Administration > Management. But I could connect via Telnet, after accidentally discovering you should enter "root" as user even though your user is "admin".

Could I upload and run a script via Telnet instead of the web ui if it's so buggy?
But either way, I need this script to write its status to the syslog.


Last edited by nomsy on Sat May 25, 2024 12:58; edited 2 times in total
Sponsor
kernel-panic69
DD-WRT Guru


Joined: 08 May 2018
Posts: 14486
Location: Texas, USA

PostPosted: Fri May 24, 2024 23:55    Post subject: Reply with quote
Administration -> Management is for remote (via WAN, not LAN) access to webUI, telnet, ssh. Nothing there should be enabled unless you know specifically what you are doing. Services is where you will find Telnet and SSH for LAN side. Administration -> Commands requires specific syntax input to run anything, but you can save whatever you enter as startup, firewall, custom, and USB scripts...

Also, the wiki is a never-ending work in progress. And you looked at the wiki for logs, but not scripting. Happy hunting.

_________________
"Life is but a fleeting moment, a vapor that vanishes quickly; All is vanity"
Contribute To DD-WRT
Pogo - A minimal level of ability is expected and needed...
DD-WRT Releases 2023 (PolitePol)
DD-WRT Releases 2023 (RSS Everything)

----------------------
Linux User #377467 counter.li.org / linuxcounter.net
mwchang
DD-WRT Guru


Joined: 26 Mar 2013
Posts: 1889
Location: Hung Hom, Hong Kong

PostPosted: Sat May 25, 2024 2:48    Post subject: Re: How do you write to SysLog from a Command sh script? Reply with quote
nomsy wrote:
TL;DR: How do you debug log anything?!

I wrote my bash script, I tested it locally in a bash commandline, everything is great, then I copy pasta'd it in dd-wrt > Administration > Commands > Command Shell Commands

Clicked Run Commands. Naturally, nothing happened...

DD-WRT (and OpenWRT) uses Busybox's Ash Shell(#!/bin/sh), not the standalone BASH shell (#!/bin/bash) found in most Linux distributions. Some of their syntax are different.

Unless you install Optware, which has BASH.

You can keep your BASH script. Just copy, rename, then re-write it a little bit using Ash shell syntax. I don't know whether there is online translator.

With luck, you might be able to run your BASH script by explicitly calling /bin/sh:
Code:
/bin/sh /path/to/your_bash_script

Quote:
Did massive amounts of internet searches.
- doesn't work: echo "#################"
- echo + this system log diversion doesn't work: exec 1> >(logger -s -t $(basename $0)) 2>&1
- doesn't work: logger "#################"
- doesn't work: logger -s -t "#################"

Here is a working example for logger:
Code:
logger -s -t my_label "[$$]: starting my script"


Lastly, your problem is shell script syntax. So DD-WRT wiki could not have helped. You can always use Google Search. Smile

_________________
Router: Asus RT-N18U (rev. A1)

Drink, Blink, Stretch! Live long and prosper! May the Force and farces be with you!

Facebook: https://www.facebook.com/changmanwai
Website: https://sites.google.com/site/changmw
SETI@Home profile: http://setiathome.berkeley.edu/view_profile.php?userid=211832
GitHub: https://github.com/changmw/changmw
nomsy
DD-WRT Novice


Joined: 24 May 2024
Posts: 6

PostPosted: Sat May 25, 2024 10:32    Post subject: Reply with quote
kernel-panic69 wrote:
Administration -> Management is for remote (via WAN, not LAN) access to webUI, telnet, ssh. Nothing there should be enabled unless you know specifically what you are doing. Services is where you will find Telnet and SSH for LAN side.


Thank you! I give you 100 points for this one^. I wish plain english was written in the actual web UI and it had links from e.g. the wan SSH setting to the lan ssh setting so you can discover all that 101 getting off the ground stuff.

kernel-panic69 wrote:

Administration -> Commands requires specific syntax input to run anything, but you can save whatever you enter as startup, firewall, custom, and USB scripts...

Also, the wiki is a never-ending work in progress. And you looked at the wiki for logs, but not scripting. Happy hunting.


Aand I subtract 99 points for this one^. It says nothing and points to "go hunt yourself".
You mean in this scripting wiki: https://wiki.dd-wrt.com/wiki/index.php/Useful_Scripts? Where is this "*:・゚✧*:・゚✧ specific syntax *:・゚✧*:・゚✧" you speak of?

I see various uses of logger, like `logger "auto-repair: gateway down, restarting WAN"` which according to Mwchang is the wrong syntax. And either way, it doesn't say how to log to SysLog; I tried all variations and nothing from/about my code comes up in the SysLog.

I put in the Command Shell > Commands box:
Code:
logger -s -t my_label "[$$]: #####################################"


clicked Run Commands, the brief loading screen popped up,

went to Stats > SysLog

And there's no mention of commands being run or my #################################

Waited 1 minute, 2 minutes, 5 minutes, nothing about it.


Last edited by nomsy on Sat May 25, 2024 10:41; edited 1 time in total
nomsy
DD-WRT Novice


Joined: 24 May 2024
Posts: 6

PostPosted: Sat May 25, 2024 10:39    Post subject: Re: How do you write to SysLog from a Command sh script? Reply with quote
mwchang wrote:

DD-WRT (and OpenWRT) uses Busybox's Ash Shell(#!/bin/sh), not the standalone BASH shell (#!/bin/bash) found in most Linux distributions. Some of their syntax are different.


Well, thanks for the info, but my script is actually #!/bin/sh, and is fairly short/conservative, but who knows what line could be wrong. I need to debug in the dd-wrt environment. Perhaps I can `vi` it in via telnet and run it via that command line and it will echo something.... But man that's such effort for what should have been a few minutes of work.

mwchang wrote:

Here is a working example for logger:
Code:
logger -s -t my_label "[$$]: starting my script"



Have you tried pasting your line in the Administration > Commands > Command Shell Commands box and click Run Commands? Because guess what, it doesn't print anything to Status > Syslog. I have a v3 version of DD-WRT from this year.
nomsy
DD-WRT Novice


Joined: 24 May 2024
Posts: 6

PostPosted: Sat May 25, 2024 10:52    Post subject: Reply with quote
PS: I also saved my #!/bin/sh script in the Custom Script box, the script starts with this line and continues with other code that might be wrong in the dd-wrt environment who knows:

Code:

#!/bin/sh
logger -s -t wtf "[$$]:############CUSTOM###########"
#other code etc


Then I added it to the cron list like this (I saw this in another post. (https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=308836)):

Code:

* * * * * root /bin/sh /tmp/custom.sh


And Saved, Applied, rebooted router.

NOTHING --- the worst thing is, as far as I can read, it's not telling you "HEY WARNING I FAILED TO RUN A CUSTOM SCRIPT YOU SHOULD LOOK AT THAT!!!!", or "hey I ran a custom script at this timestamp"

This is crazy

----------------

Went in via Telnet, and it appears there's no such script as /tmp/custom.sh? If so, then why the heck is the SysLog not yelling about this? "hey you're trying to cron a missing script!!"

Code:

0;root@DD-WRT: /tmproot@DD-WRT:/tmp# ls
TZ                 crontab            igmpproxy.conf     oet                var                www
ath10k-board.bin   dnsmasq.conf       loginfail          ppp                wlan0_configured
blocklist          dnsmasq.leases     loginprompt        resolv.conf        wlan0_hostap.conf
board1.bin         eap_identities     mnt                resolv.dnsmasq     wlan1_configured
bw.db              etc                nvram              root               wlan1_hostap.conf
cron.d             hosts              nvstate            services           wlan2_configured
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 13094
Location: Netherlands

PostPosted: Sat May 25, 2024 11:14    Post subject: Reply with quote
With regards to scripts DDWRT is just a Linux box but instead of bash it uses ash which is for 99% the same.

So you make, run and debug just like on any other Linux box, this is not DDWRT specific

For some interesting scripts see:
https://pastebin.com/u/eibgrad
https://github.com/egc112/ddwrt/blob/main/wireguard-companion.sh
https://github.com/egc112/ddwrt/tree/main/adblock

Oh and you cannot run complex commands from Administration > Commands, just use the shell

P.S. nothing "crazy" just a lack of knowledge Wink

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
nomsy
DD-WRT Novice


Joined: 24 May 2024
Posts: 6

PostPosted: Sat May 25, 2024 11:35    Post subject: Reply with quote
Mod note: Do not follow this procedure, it is invalid and this note is for future reference.

No. This is insane. I found SOMETHING (beyond awkward) that gives feedback, and works*:

Telnet with user root into the router via commandline.

Create a .sh file on the router like this:
Code:

cd /tmp
ls
vi my_custom_script.sh

now paste in here your script, and check this ancient sorcery to remember how to save in goddam vi: https://linuxize.com/post/how-to-save-file-in-vim-quit-editor/
now chmod and run your code to test it:
Code:

chmod +x my_custom_script.sh
/bin/sh ./my_custom_script.sh


^ This will print all `echo`'s to your current command line AND ALSO LOGGER LINES FROM HERE DO REACH SYSLOG (e.g. these lines: `logger -s -t wtf "[$$]: ###############"`)

Turns out my script was working fine all along!


* WARNING: this is an intermediary step for debug purposes, because: The DD-WRT filesystem is either readonly or does NOT persist after reboot. You can't create files outside of /tmp/, and everything inside /tmp/ will be GONE on a reboot / power outage.

So what you want to do is some more f****d up work (because the Custom Script box doesn't actually create a file?!): create a startup script in Administration > Commands that procedurally creates your script file and saves it into /tmp/ every time the router starts. (from this post: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=289949)
Then reference it in cron if you need to like every 5 mintes: `*/5 * * * * root /bin/sh /tmp/my_custom_script.sh`

So, SOLUTION: here is how to procedurally create a sh script via start box; tested, works:
Code:

SCRIPT="/tmp/my_custom_script.sh"
cat << "EOF" > $SCRIPT
#!/bin/sh
echo "This is your script. It gets created on router startup (and deleted automatically on shutdown)."
logger -s -t wtfwtf "[$$]:###### it running ######"
EOF
chmod +x $SCRIPT

(If you want to also run it right away: `/bin/sh /tmp/my_custom_script.sh`)


The QUESTIONS Remain:
1. Why the heck does RUN COMMANDS button do NOTHING?
2. Why the heck can't I find the contents of the Custom Script box under /tmp/custom.sh? Where is it?! According to the forums that's where it is: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=308836 Except it isn't.
3. WHY for the love of god, does SysLog NOT PRINT WHEN SOMETHING FAILED?! You NEED to tell us when a script is missing or can't run!! (or have a verbose debug mode you can enable)

I was supposed to just come in, paste a sh script (which has 0 errors btw) in the designated box, click apply and be done. Now 48h later I am in narnina and I have to fight with the snarf snarf box....


Last edited by nomsy on Sat May 25, 2024 13:05; edited 19 times in total
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 13094
Location: Netherlands

PostPosted: Sat May 25, 2024 11:51    Post subject: Reply with quote
What you describe is just how it works on any other linux system.

On embedded linux systems the file system is read only to protect your flash and firmware.

Settings are saved in nvram, this is where the script is also saved and extracted to its normal script form and placed in /tmp (which is part of your RAM) where it can be executed.

This is standard operation for many embedded systems.

Not crazy not awkward just a lack of knowledge on your side

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
nomsy
DD-WRT Novice


Joined: 24 May 2024
Posts: 6

PostPosted: Sat May 25, 2024 11:58    Post subject: Reply with quote
Mod edited to remove invalid 'solution' link.
egc wrote:
What you describe is just how it works on any other linux system.

On embedded linux systems the file system is read only to protect your flash and firmware.

Settings are saved in nvram, this is where the script is also saved and extracted to its normal script form and placed in /tmp (which is part of your RAM) where it can be executed.

This is standard operation for many embedded systems.

Not crazy not awkward just a lack of knowledge on your side


Please stop talking. The readonly file system I am 1000% on board with. That is off topic. Have you read my actual complaint points of specific things you can try that don't work? Like running a suggested command via the designated input box, that fails with 0 feedback? PLEASE DON'T RESPOND because you are just making everything worse. Your help is not needed in my thread. Thank you.
ho1Aetoo
DD-WRT Guru


Joined: 19 Feb 2019
Posts: 3104
Location: Germany

PostPosted: Sat May 25, 2024 14:08    Post subject: Reply with quote
everything works here and you have absolutely no clue
And you can save your unobjective shouting - didn't your parents teach you any manners?

And let me prove to you how little knowledge you have.
With the command “ls” you will never find the scripts because they are hidden files Smile

If you don't like it here then look for another forum - you are certainly not giving our moderators any instructions.

noob!!!!! Laughing



7869158725.jpg
 Description:
 Filesize:  175.58 KB
 Viewed:  618 Time(s)

7869158725.jpg



_________________
Quickstart guides:
use Pi-Hole as simple DNS-Server with DD-WRT
VLAN configuration via GUI - 1 CPU port
VLAN configuration via GUI - 2 CPU ports (R7800, EA8500 etc)

Routers
Marvell OCTEON TX2 - QHora-322 - OpenWrt 23.05.3 - Gateway
Qualcomm IPQ8065 - R7800 - DD-WRT - WAP
IONK
DD-WRT Guru


Joined: 19 Aug 2011
Posts: 1009

PostPosted: Sat May 25, 2024 14:42    Post subject: Reply with quote
  • I see too much cursing that almost every post needs a TL;DR to summarize the ranting, but I'm in the mood to get shouted at, so here I am, posting a reply without bothering to check the details.
  • Some wiki pages here are not that well-maintained due to lack of man-power and constantly changing firmware, so the learning curve can be quite steep. You can spend all day here to complain about the wiki pages/docs, but probably nobody cares. I see that you're new here.
  • Basics when requesting help/reporting issue: router, model, hardware version, firmware version, configurations, steps to reproduce, screenshots, screen recordings, anything that allow others to understand your final goal, your steps, and your problems.
  • There are many software/hardware/network/security/product experts here that can fix all your problems if you post in the correct forum, ask the correct questions, provide the needed details, and get your hands dirty to dive into the problems.
  • Retry? [ Y / N ]

_________________
▫ RSS feed for DD-WRT releases (2024): https://rsseverything.com/feed/7d36ab68-7733-46c3-bd8a-9c54c5cef08c.xml
easyddup - A simple upgrade/downgrade utility by yoyoma2 --- as featured in 📌 Reference Links, stickies retired back to forum (Moderator's Pick 🌟)
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 13094
Location: Netherlands

PostPosted: Sat May 25, 2024 14:53    Post subject: Reply with quote
IONK wrote:
  • I see too much cursing that almost every post needs a TL;DR to summarize the ranting, but I'm in the mood to get shouted at, so here I am, posting a reply without bothering to check the details.
  • Some wiki pages here are not that well-maintained due to lack of man-power and constantly changing firmware, so the learning curve can be quite steep. You can spend all day here to complain about the wiki pages/docs, but probably nobody cares. I see that you're new here.
  • Basics when requesting help/reporting issue: router, model, hardware version, firmware version, configurations, steps to reproduce, screenshots, screen recordings, anything that allow others to understand your final goal, your steps, and your problems.
  • There are many software/hardware/network/security/product experts here that can fix all your problems if you post in the correct forum, ask the correct questions, provide the needed details, and get your hands dirty to dive into the problems.
  • Retry? [ Y / N ]


👍

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
mwchang
DD-WRT Guru


Joined: 26 Mar 2013
Posts: 1889
Location: Hung Hom, Hong Kong

PostPosted: Sat May 25, 2024 15:09    Post subject: Re: How do you write to SysLog from a Command sh script? Reply with quote
nomsy wrote:

mwchang wrote:

Here is a working example for logger:
Code:
logger -s -t my_label "[$$]: starting my script"



Have you tried pasting your line in the Administration > Commands > Command Shell Commands box and click Run Commands? Because guess what, it doesn't print anything to Status > Syslog. I have a v3 version of DD-WRT from this year.

Edit: Maybe irrelevant, as the logger command should be unaffected by Logging Level. Smile

Then it's another problem: have you enabled system logging? Properly?

You have two places to look at, one of which is in firewall security, where you need to set Logging Level to Medium. I know this sounds weird. And I think the Wiki might need an update, a cross-reference.

I have been using that similar command all the time in the past 10 years. Smile

Logging with DD-WRT - DD-WRT Wiki
https://wiki.dd-wrt.com/wiki/index.php/Logging_with_DD-WRT

DD-WRT :: View topic - Documentation for Firewall Log Level
https://forum.dd-wrt.com/phpBB2/viewtopic.php?p=688568

Logging is disabled by default to save resources. DD-WRT is very ancient. Back then, very old routers don't have that much memory, nvram storage and processor power.


(BTW, everyone just take it easy about forum rules. There are always misunderstanding and oversights for new users of DD-WRT. And the Wiki... um... )

_________________
Router: Asus RT-N18U (rev. A1)

Drink, Blink, Stretch! Live long and prosper! May the Force and farces be with you!

Facebook: https://www.facebook.com/changmanwai
Website: https://sites.google.com/site/changmw
SETI@Home profile: http://setiathome.berkeley.edu/view_profile.php?userid=211832
GitHub: https://github.com/changmw/changmw


Last edited by mwchang on Sat May 25, 2024 15:39; edited 1 time in total
mwchang
DD-WRT Guru


Joined: 26 Mar 2013
Posts: 1889
Location: Hung Hom, Hong Kong

PostPosted: Sat May 25, 2024 15:38    Post subject: Reply with quote
nomsy wrote:

Then I added it to the cron list like this (I saw this in another post. (https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=308836)):
Code:

* * * * * root /bin/sh /tmp/custom.sh


And Saved, Applied, rebooted router.

Did you enable CRON? I am not sure whether CRON is enabled by default, cannot quite remember. You can check if Cron is running by:
Code:
ps | grep -i cron

Also, after altering /tmp/crontab, you need to restart Cron so that crontab is re-read. You do NOT have to reboot.

When testing Cron, try not to use a script like custom.sh in the first attempt. Try just echo some text to /var/log/messages first.
Code:
# >> means appending text not overwriting
* * * * * root echo haha >> /var/log/messages

You are trying to do a lot of things at the same time. That can confuse you. Make it step by step.

_________________
Router: Asus RT-N18U (rev. A1)

Drink, Blink, Stretch! Live long and prosper! May the Force and farces be with you!

Facebook: https://www.facebook.com/changmanwai
Website: https://sites.google.com/site/changmw
SETI@Home profile: http://setiathome.berkeley.edu/view_profile.php?userid=211832
GitHub: https://github.com/changmw/changmw
Goto page 1, 2  Next Display posts from previous:    Page 1 of 2
Post new topic   This topic is locked: you cannot edit posts or make replies.    DD-WRT Forum Index -> General Questions All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum