Posted: Thu Jan 20, 2022 13:21 Post subject: Script that works ok from the cli, but not from the webUI.
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 &
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 &
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...
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? _________________ TP-Link - TL-WR1043ND v1.8 - Atheros 9132
Joined: 16 Nov 2015 Posts: 6447 Location: UK, London, just across the river..
Posted: Thu Jan 20, 2022 18:47 Post subject:
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
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.
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
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).