OpenWrt Forum Archive

Topic: how to run script on certain condition

The content of this topic has been archived on 17 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Is there any tools or script example to do this :
if ping has zero reply from certain IP than run this script.

My objective is to shutdown remote ssh server (eg :ssh root@192.168.1.203 -pw=xxxx halt), when there is 0 ping reply from certain IP (eg:192.168.1.104)

kind of modified watchcat ( but i dont know to modify the shutdown command to sending remote command )

thanks.
Im on BB 14.07

(Last edited by uabuner on 12 Feb 2017, 03:48)

Yes, it is trivial task. Something like:

n=$(ping -c 10 | grep -o -E '\d+ packets r' | grep -o -E '\d+')
if [ "$n" -eq 0 ]; then

fi

You can wrap it in something like:

while sleep 100; do

done

and add to /etc/rc.local to autostart:

/etc/script.sh &

You can also run the script via crontab.

(Last edited by ulmwind on 11 Feb 2017, 14:08)

yes something like that....here is my result script, im planning it on 10" cronjob

#!/bin/bash

if ping -c 5 192.168.1.104&> /dev/null
then
  : # colon is a null and is required
else
   ssh root@192.168.1.203 poweroff
fi

one problem still
>> ssh root@192.168.1.203 poweroff << need to be automatically executed with no password prompt, and I cant get generate rsa key to server ( it will be gone after reboot) and I cant find sshpass on openwrt, I dont understand except using also

Could somebody kindly enough compile sshpass for openwrt and share it.
better yet submit it to package so i can just opkg install it...
I really dont understand how to make ipkg ...
My only workstation is windows
Im on 14.07 BCM6358 (Huawei EchoLife HG553)

Thank You

(Last edited by uabuner on 12 Feb 2017, 09:27)

@tunk
I think its too complicated for me big_smile
..so I need to install netcat on my openwrt and issued the command ?
Still need interaction though ...cmiiw

I found working sshpass on my openwrt
so here is my step to shutdown remote server automatically with no password prompt...its insecure but for now its enough for me
--download sshpass here https://www.dropbox.com/s/bl6vyhd5jpim6zm/sshpass, copy to your /bin directory using scp or ssh, reboot your router
--write simple script for : if the ip abc is dead then poweroff the xyz vi /path/to/your/script.sh

#!/bin/bash

if ping -c 5 192.168.1.abc&> /dev/null
then
  : # colon is a null and is required
else
  sshpass -p yourpassword ssh username@192.168.1.xyz poweroff
fi

--scheduled it to run @10 minutes on scheduled task
*/10 * * * *  /path/to/your/script.sh


thank you all

The discussion might have continued from here.