How can I create my own script?
In category Routers .
There are two possibilities how to create a script.
1. You can create the script on your pc in some text editor and then copy it to router via FTP (SFTP). Be careful while you are using Windows system because routers are based on Linux system (EOL - End Of Line). Windows system uses LF+CR unlike Linux system which uses only LF at end of line (EOL). The script can be copied to root folder what is in flash memory or to /var/data folder which is in MRAM. This is good for testing purpose.
2. Second possibility is to create the script from "Startup Script" and it is technique what we recommend. There are exist two techniques that you can use for creating a script from script.
- you can use cat with A here document, see the example below
Startup Script
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here. cat > /var/scripts/smsan.sh << EOF #!/bin/sh PHONE=+420734875410 an1thrshold=500 an2thrshold=500 an1txt="Activation an1" an2txt="Activation an2" OLD1=0 OLD2=0 while true do VAL1=\`/usr/bin/io get an1\` if [ \$VAL1 -gt \$an1thrshold ]; then POMVAL1=1 else POMVAL1=0 fi if [ "\$POMVAL1" != "\$OLD1" ]; then [ "\$POMVAL1" = "1" ] && gsmsms \$PHONE "\$an1txt" OLD1=\$POMVAL1 fi VAL2=\`/usr/bin/io get an2\` if [ \$VAL2 -gt \$an2thrshold ]; then POMVAL2=1 else POMVAL2=0 fi if [ "\$POMVAL2" != "\$OLD2" ]; then [ "\$POMVAL2" = "1" ] && gsmsms \$PHONE "\$an2txt" OLD2=\$POMVAL2 fi sleep 1 done EOF
Then the script will be created in folder /var/scripts and named smsan.sh. It will look like script below.
The script /var/scripts/smsan.sh sends SMS when input an1 or an2 reachs defined threshold.
#!/bin/sh PHONE=+420734875410 an1thrshold=500 an2thrshold=500 an1txt="Aktivace an1" an2txt="Aktivace an2" OLD1=0 OLD2=0 while true do VAL1=`/usr/bin/io get an1` if [ $VAL1 -gt $an1thrshold ]; then POMVAL1=1 else POMVAL1=0 fi if [ "$POMVAL1" != "$OLD1" ]; then [ "$POMVAL1" = "1" ] && gsmsms $PHONE "$an1txt" OLD1=$POMVAL1 fi VAL2=`/usr/bin/io get an2` if [ $VAL2 -gt $an2thrshold ]; then POMVAL2=1 else POMVAL2=0 fi if [ "$POMVAL2" != "$OLD2" ]; then [ "$POMVAL2" = "1" ] && gsmsms $PHONE "$an2txt" OLD2=$POMVAL2 fi sleep 1 done EOF
- or you can use echo for that, see below
Startup Script
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here. echo -en "#!/bin/sh" \ "\nPHONE=123456789" \ "\nMESSAGE=\"BIN0 is active\"" \ "\nwhile true" \ "\ndo" \ "\n /usr/bin/io get bin0" \ "\n VAL=\$?" \ "\n if [ \"\$VAL\" != \"\$OLD\" ]; then" \ "\n [ \"\$VAL\" = \"0\" ] && gsmsms \$PHONE \$MESSAGE" \ "\n OLD=\$VAL" \ "\n fi" \ "\nsleep 1" \ "\ndone" >> /var/scripts/my_test.sh
Then the script will be created in folder /var/scripts and named my_test.sh. It will look like script below.
The script /var/scripts/my_test.sh sends SMS when bin0 is active.
#!/bin/sh PHONE=123456789 MESSAGE="BIN0 is active" while true do /usr/bin/io get bin0 VAL=$? if [ "$VAL" != "$OLD" ]; then [ "$VAL" = "0" ] && gsmsms $PHONE $MESSAGE OLD=$VAL fi sleep 1 done
3. Finally you need prepare the script for running and run it. You can start it via cron or from also Startup Script1 or UP/Down Script2. Example below shows starting the script directly from Startup Script.
Startup Script or Up/Down Script
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here. echo -en "#!/bin/sh" \ . . # Here is body of your script. . . "\ndone" >> /var/scripts/my_test.sh chmod 777 /var/scripts/my_test.sh sh /var/scripts/my_test.sh &
NOTE:
1 If you create the script from Startup Script then it will be done after each start up router. This technique supports to save it in configuration file and is retained after re-flash the router.
2 E.g. once you use never ending loop in your script and then run the script from "Up Script" you must finish the script from "Down Script" otherwise the script will be run multiple times (with each start WAN).