Script that works ok from the cli, but not from the webUI.

Post new topic   Reply to topic    DD-WRT Forum Index -> General Questions
Goto page 1, 2  Next
Author Message
denik
DD-WRT Novice


Joined: 08 Dec 2011
Posts: 41

PostPosted: Thu Jan 20, 2022 13:21    Post subject: Script that works ok from the cli, but not from the webUI. Reply with quote
Hello guys!

I created a small script based on one that raph321 published here, so that my router sends an email through gmail every time the wan ip address changes, after installing Entware and curl.

My problem is that when I run the script directly from the command line, it works like a charm, even if I run it in the background with the '&' at the end. However, when I call it from WebUI=>Administration=>Commands, the script runs but doesn't send the e-mail.

I've been doing some testing with sections of the script to try and debug it, and I'm suspecting that the lines that aren't working are the 'grep' and 'curl' ones, but only when the script runs from the WebUI.

Code:
#!/bin/sh
###################################################################################################
# Config area
INTERVAL=300
SERVER=smtps://smtp.gmail.com:465
USER=sender_mailbox@gmail.com
FROM_NAME='My Router'
PASS=MyPass
TO=dest_mailbox@yahoo.com.ar
TO_NAME='This Guy'
SUBJECT='WAN IP changed.'
MESSAGE=/tmp/mnt/sda2/dd-wrt_scripts/startup_scripts/.ipmailer_tmp.txt
###################################################################################################
# Does the nvram var wan_ipaddr_last exist?
last_exist() {
   if ! nvram show | grep -q last_wan_ipaddr; then
      nvram set last_wan_ipaddr='Unknown'
      nvram commit
    fi
   }
# Creates e-mail message
write_message() {
   echo 'From: "'$FROM_NAME'" <'$USER'>' > $MESSAGE
    echo 'To: "'$TO_NAME'" <'$TO'>' >> $MESSAGE
   echo 'Subject: '$SUBJECT >> $MESSAGE
    echo ''   >> $MESSAGE
    echo 'Current WAN IP address is:' $CURR_WAN_IPADDR >> $MESSAGE
    echo 'Last known WAN IP address is:   ' $LAST_WAN_IPADDR >> $MESSAGE
   }
# Sends the e-mail message
send() {
   curl --ssl-reqd --url $SERVER --user $USER:$PASS --mail-from $USER --mail-rcpt $TO --upload-file $MESSAGE
   }
###################################################################################################
# The "main" loop
while sleep $INTERVAL
do
   last_exist
   CURR_WAN_IPADDR=`nvram get wan_ipaddr`
   LAST_WAN_IPADDR=`nvram get last_wan_ipaddr`
# Check if vars mismatch
   if [ $CURR_WAN_IPADDR != $LAST_WAN_IPADDR ]; then
      write_message
      send
      # Update the nvram variable to the current IP-Address
      nvram set last_wan_ipaddr=$CURR_WAN_IPADDR
      nvram commit
   fi
done


EDIT:
Quote:
Just to clarify, the way I'm calling the script and it's not working is just putting the following line in the command box (I'm not putting the whole script in the box):
Code:
sh /mnt/sda2/dd-wrt_scripts/startup_scripts/ipmailer.startup &



Does anyone have an idea what could be happening?

Thank you very much for any help you can give me!


Denik

_________________
TP-Link - TL-WR1043ND v1.8 - Atheros 9132


Last edited by denik on Thu Jan 20, 2022 14:33; edited 3 times in total
Sponsor
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Thu Jan 20, 2022 13:40    Post subject: Reply with quote
You can only run simple commands from the command box.

I think the command box uses eval to run the commands meaning it does not work for multi line calls and you have to escape special characters.

Bottom line do not do what you are doing Smile

If you want to test scripts just make the script copy it with WinSCP to /tmp and run it from there with:
sh -x /tmp/myscript.sh

the -x uses the debug mode

_________________
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
denik
DD-WRT Novice


Joined: 08 Dec 2011
Posts: 41

PostPosted: Thu Jan 20, 2022 13:52    Post subject: Reply with quote
Thanks for the quick response and advice, egc!

Just to clarify, the way I'm calling the script and it's not working is just putting the following line in the command box (I'm not putting the whole script in the box):

Code:
sh /mnt/sda2/dd-wrt_scripts/startup_scripts/ipmailer.startup &


I'll try adding -x to see if I get anything new.

Thanks.

_________________
TP-Link - TL-WR1043ND v1.8 - Atheros 9132
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Thu Jan 20, 2022 14:36    Post subject: Reply with quote
Maybe not going to work.

You should do this from the CLI (telnet/Putty)

_________________
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
denik
DD-WRT Novice


Joined: 08 Dec 2011
Posts: 41

PostPosted: Thu Jan 20, 2022 14:41    Post subject: Reply with quote
Yeah... I see...
The thing is that putting the same line in the cli the script works perfectly...
Code:
sh /mnt/sda2/dd-wrt_scripts/startup_scripts/ipmailer.startup &


Is there any way to see the output of what the webUI command box is running? I'll try redirecting the output to a file and see if it works.

UPDATE:
Mmmm no. It didn't work. I tried putting in the box:
Code:
sh -x /mnt/sda2/dd-wrt_scripts/startup_scripts/ipmailer.startup >>/mnt/sda2/dd-wrt_scripts/startup_scripts/output.txt &

and it generated the output file but it is blank.

_________________
TP-Link - TL-WR1043ND v1.8 - Atheros 9132
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Thu Jan 20, 2022 15:06    Post subject: Reply with quote
Just run from the CLI with sh -x and without the &
_________________
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
denik
DD-WRT Novice


Joined: 08 Dec 2011
Posts: 41

PostPosted: Thu Jan 20, 2022 15:46    Post subject: Reply with quote
Yes, I did it that way and the script runs normally the same way every time I run it from the cli. Not much to debug there... Crying or Very sad

Code:
Router:~# sh -x /mnt/sda2/dd-wrt_scripts/startup_scripts/ipmailer.startup
+ INTERVAL=30
+ SERVER=smtps://smtp.gmail.com:465
+ USER=##################@gmail.com
+ FROM_NAME='My Router'
+ PASS=##################
+ TO=##################
+ TO_NAME='This Guy'
+ SUBJECT='WAN IP changed.'
+ MESSAGE=/tmp/mnt/sda2/dd-wrt_scripts/startup_scripts/.ipmailer_tmp.txt
+ sleep 30
+ last_exist
+ grep -q last_wan_ipaddr
+ nvram show
size: 33394 bytes (32142 left)
+ nvram get wan_ipaddr
+ CURR_WAN_IPADDR=##################
+ nvram get last_wan_ipaddr
+ LAST_WAN_IPADDR=THIS_IS_AN_OLD_IPADDR
+ '[' ################## '!=' THIS_IS_AN_OLD_IPADDR ]
+ write_message
+ echo 'From: "My' 'Router" <##################@gmail.com>'
+ echo 'To: "This' 'Guy" <##################>'
+ echo 'Subject: WAN' IP changed.
+ echo
+ echo 'Current WAN IP address is:           ' ##################
+ echo 'Last known WAN IP address is:   ' THIS_IS_AN_OLD_IPADDR
+ send
+ curl --ssl-reqd --url smtps://smtp.gmail.com:465 --user ##################@gmail.com:################## --mail-from ##################@gmail.com --mail-rcpt ################## --upload-file /tmp/mnt/sda2/dd-wrt_scripts/startup_scripts/.ipmailer_tmp.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   218    0     0  100   218      0     71  0:00:03  0:00:03 --:--:--    72
+ nvram set 'last_wan_ipaddr=##################'
+ nvram commit
+ sleep 30
+ last_exist
+ grep -q last_wan_ipaddr
+ nvram show
size: 33385 bytes (32151 left)
+ nvram get wan_ipaddr
+ CURR_WAN_IPADDR=##################
+ nvram get last_wan_ipaddr
+ LAST_WAN_IPADDR=##################
+ '[' ################## '!=' ################## ]
+ sleep 30


I would like some way to do something similar but with the output of the webUI command box. Would there be any? Confused

_________________
TP-Link - TL-WR1043ND v1.8 - Atheros 9132
kernel-panic69
DD-WRT Guru


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

PostPosted: Thu Jan 20, 2022 16:39    Post subject: Reply with quote
Probably going to have to call it from a cron job... is this script saved as a USB script?
_________________
"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
denik
DD-WRT Novice


Joined: 08 Dec 2011
Posts: 41

PostPosted: Thu Jan 20, 2022 17:16    Post subject: Reply with quote
kernel-panic69 wrote:
Probably going to have to call it from a cron job... is this script saved as a USB script?


Yes, that's it @kernel-panic69, '/mnt/sda2' is an USB filesystem.

_________________
TP-Link - TL-WR1043ND v1.8 - Atheros 9132
Alozaros
DD-WRT Guru


Joined: 16 Nov 2015
Posts: 6437
Location: UK, London, just across the river..

PostPosted: Thu Jan 20, 2022 18:47    Post subject: Reply with quote
y not save it at jffs or opt and call it from there, or save it at custom script and call it from start script...of course you have to make the script executable...

or like this
1. install entware
2. install nano
3. run nano with the default path you want to place the script like this example 'nano /opt/etc/init.d/S61stubby.sh' (no quotes) and whatever name or destination you may need
4. save the script and make it executable like this example chmod +x /opt/etc/init.d/S61stubby.sh
5. call it at the start up or custom script...

_________________
Atheros
TP-Link WR740Nv1 ---DD-WRT 55630 WAP
TP-Link WR1043NDv2 -DD-WRT 55723 Gateway/DoT,Forced DNS,Ad-Block,Firewall,x4VLAN,VPN
TP-Link WR1043NDv2 -Gargoyle OS 1.15.x AP,DNS,QoS,Quotas
Qualcomm-Atheros
Netgear XR500 --DD-WRT 55779 Gateway/DoH,Forced DNS,AP Isolation,4VLAN,Ad-Block,Firewall,Vanilla
Netgear R7800 --DD-WRT 55819 Gateway/DoT,AD-Block,Forced DNS,AP&Net Isolation,x3VLAN,Firewall,Vanilla
Netgear R9000 --DD-WRT 55779 Gateway/DoT,AD-Block,AP Isolation,Firewall,Forced DNS,x2VLAN,Vanilla
Broadcom
Netgear R7000 --DD-WRT 55460 Gateway/SmartDNS/DoH,AD-Block,Firewall,Forced DNS,x3VLAN,VPN
NOT USING 5Ghz ANYWHERE
------------------------------------------------------
Stubby DNS over TLS I DNSCrypt v2 by mac913
kernel-panic69
DD-WRT Guru


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

PostPosted: Thu Jan 20, 2022 21:13    Post subject: Reply with quote
denik wrote:
kernel-panic69 wrote:
Probably going to have to call it from a cron job... is this script saved as a USB script?


Yes, that's it @kernel-panic69, '/mnt/sda2' is an USB filesystem.

Just so we are clear, did you click "Save USB":

https://ibb.co/xFH7KwR



@Alozaros: The ONE thing we can take away from your post is why not call it from the startup script.

_________________
"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
denik
DD-WRT Novice


Joined: 08 Dec 2011
Posts: 41

PostPosted: Fri Jan 21, 2022 13:17    Post subject: Reply with quote
Hello everyone! Thank you all for your time and advice.

@kernel-panic69, I'm afraid that there is no such button in my firmware. My router is a TP-Link TL-WR1043ND and the version it's running is v3.0-r36104 std, which is the latest I could get it to work due to RAM space.



So far, I have tried to run it as a cron job and I was able to verify that the task is running, but it continues without sending the email.

I also tried adding it as a custom script but the behavior remains the same as before: it works and sends the email if I call the custom script from the CLI, but it doesn't work if I call it from the command box as a startup script. Confused

I tried calling the custom script as a cron job too, but that doesn't work either.

_________________
TP-Link - TL-WR1043ND v1.8 - Atheros 9132
eibgrad
DD-WRT Guru


Joined: 18 Sep 2010
Posts: 9157

PostPosted: Fri Jan 21, 2022 15:08    Post subject: Reply with quote
If you want, I can take a closer look at the script and perhaps make a few changes, because there are a number of issues w/ it that should really be corrected. They *might* be contributing to your problems in getting this running at bootup.

1. I see no reason this script requires the USB drive. All it's doing is creating a text file there to be sent via curl to the email server, when it could simply be created in /tmp. Creating a dependency on the USB drive is just a bad idea UNLESS you had some intention of saving these messages. But as far I can see, that's NOT the case. Each new message overwrites the previous.

2. It's NOT a good idea to use nvram for storing variables UNLESS you need to make them persistent across a reboot. Each nvram commit reduces the lifetime of the router's flash memory. You should be using a program variable instead. The use of nvram may be contributing to the problem too. Upon reboot, it has the same WAN ip as before the reboot. And if it happens the router ends up being assigned the same WAN ip again, then it doesn't send the email! I don't know if that is or isn't your intention. If it is, perhaps that explains the use of nvram. But you're complaining about it NOT sending the email, as if you expect it to be sent each time it reboots. So again, it's NOT clear your intentions here, and therefore what should be expected. If you want/need that kind of persistence, then use the USB drive!

3. You should create the script in such a way that it's capable of being easily debugged, which for my own scripts means writing the output to a log file (/tmp or even the USB drive) or the syslog (or both). It's not enough to assume that just because it works when run from the CLI, it will run successfully from the startup script. But you have no means at the moment to detect any errors during startup.

4. It might be better to treat this as a wanup script rather than a startup script. Unfortunately, the GUI doesn't offer that option (something I think should be corrected). But if the script is saved in /jffs/etc/config w/ the .wanup extension, the router will automatically run it each time the WAN is (re)initialized, perhaps eliminating the need for the loop, or cron. IOW, let the fact the router triggered the WAN event be the triggering mechanism for the script, and then decide if the WAN ip changed and take appropriate actions (and store your persistent program variable(s) in /tmp).

Normally if I see scripts like this, even w/ all their issues, and it runs to the satisfaction of the OP, I say nothing. The imperfections are typically not that big a deal. But when you run into problems, sometimes you need to make some changes that will not only improve it, but more importantly, your ability to debug it. And as of the moment, it seems to me we've reached that point. But it would help if we knew *precisely* the way the OP expects this script to behave. All I can do at the moment is draw some reasonable inferences from what I seen so far, which may not be valid. That nvram variable and use of the USB drive in particular raises some concerns.

_________________
ddwrt-ovpn-split-basic.sh (UPDATED!) * ddwrt-ovpn-split-advanced.sh (UPDATED!) * ddwrt-ovpn-client-killswitch.sh * ddwrt-ovpn-client-watchdog.sh * ddwrt-ovpn-remote-access.sh * ddwrt-ovpn-client-backup.sh * ddwrt-mount-usb-drives.sh * ddwrt-blacklist-domains.sh * ddwrt-wol-port-forward.sh * ddwrt-dns-monitor.sh (NEW!)
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12889
Location: Netherlands

PostPosted: Fri Jan 21, 2022 17:15    Post subject: Reply with quote
You are using a very old build which has known security issues, consider upgrading.

Currently we are on 48141 Smile

If you want to start a script from USB, you have to wait till the USB is online.

Just add:
sleep 30
before you call the script

But newer builds can save the commands instead as Startup as USB those commands are executed after the USB is up.

But as @eibgrad already elaborated you do not need an USB stick at all Smile

_________________
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
kernel-panic69
DD-WRT Guru


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

PostPosted: Fri Jan 21, 2022 18:45    Post subject: Reply with quote
x2 on considering upgrading. Although, if it is the v1, it is problematic, to say the least (I am sure there are more threads):

https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=62217
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=323882&postdays=0&postorder=asc&start=51
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=323955
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=325423
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=326065
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=328142
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=328496
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=329442
https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=330064

_________________
"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
Goto page 1, 2  Next Display posts from previous:    Page 1 of 2
Post new topic   Reply to topic    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