Hi Guys,

I had a requirement for running commands directly on the device. Would be 
great if I could get your inputs on better ways to achieve this.

For now, I have made changes to the openwisp-config code to monitor if a 
particular file(which contains a ash script) is downloaded or not. This 
happens everytime there is a configuration download on the device. I've 
attached the change patchfile(to be applied on openwisp-config) for 
reference.

>From the openwisp-server, I'm creating a script in the following format
#!/bin/ash
#<current epoch timestamp>
<command>

eg. 
[image: Inline image 1]

I also tried the crontab way, but could not get the command to run only 
once and then disappear.
Although this works, is there a better way to do this?



-- 
You received this message because you are subscribed to the Google Groups 
"OpenWISP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
--- a/openwisp-config/files/openwisp.agent
+++ b/openwisp-config/files/openwisp.agent
@@ -229,6 +229,8 @@ apply_configuration() {
                       -p daemon.crit
                return 1
        fi
+       #running commands
+       /usr/sbin/openwisp-executecommand-config
        # restore unmanaged configurations
        /usr/sbin/openwisp-restore-unmanaged
        # call pre-reload-hook
--- /dev/null
+++ b/openwisp-config/files/sbin/openwisp-executecommand-config
@@ -0,0 +1,28 @@
+#!/bin/ash
+
+COMMAND_FILE=/tmp/openwisp_remote_commands
+SAFE_SECONDS=120
+
+#check if command file exists
+if [ -f "$COMMAND_FILE" ]
+then
+	#get the timestamp
+       SCRIPT_TIME=`sed '2q;d' $COMMAND_FILE | awk -F'#' '{print $2}'`
+       CURRENT_TIME=`date +%s`
+	#if some problem with NTP
+       if [ "$CURRENT_TIME" -gt "$SCRIPT_TIME" ]
+       then
+               TIME_DIFF=`expr $CURRENT_TIME - $SCRIPT_TIME`
+		#check to make sure this script is not a historical one(ie > SAFE SECONDS)
+               if [ "$SAFE_SECONDS" -gt "$TIME_DIFF" ]
+               then
+                       ash $COMMAND_FILE               
+               else
+                       logger "TIME TO EXECUTE HAS SURPASSED" -t openwisp -p daemon.info
+               fi
+       else
+               logger "CANNOT EXECUTE SINCE TIME IS NOT SET" -t openwisp -p daemon.info
+       fi
+else
+       logger "NOTHING TO EXECUTE" -t openwisp -p daemon.info
+fi

Reply via email to