Sunday, May 28, 2017

Well it has been a while since I put anything up on the blog and thought I needed to share some more, so hopefully this is a new beginning and I will be adding more and more as I go (and remember some of what I have done already).

So Today I though I would share a recent issue that came up with a problematic device on a network connected to a Ubiquiti Unifi switch (www.wbnt.com/products/#unifi), the device (a camera) randomly seems to lock up can work for a day a week or a month at times without issue but when it locks up either you have to unplug the cable and plug it back in or power cycle the port (which can be done from the web UI).

However there is currenly no watchdog capability with these devices, so off I go to figure out how to turn a single port off and on again with the Vyatta (linux) OS with its current tools since it is not easy (maybe not possible) to add others to it.

as it turns out I find that you can ssh into the device then telnet localhost and then run some commands to turn off whatever port you want then others to turn it back on again, basically you have to telnet in then:

enable -> configure -> interface 0/1
of course if you are familiar with cisco there are similarites here, enable puts you in super user mode, configure (you guessed it puts you into configure mode) then interface 0/1 (in my case) tells it to go to interface 1 (or read Port 1) now you are ready to turn power on, off , or passive24v .

so to turn off the power:
poe opmode shutdown

to turn on power:
poe opmode passive24v . ( in my case I needed this, if you need standard switch poe like for poe VoIP phone you would use: poe opmode auto)

so to recap you have to ssh in to the switch, then telnet (itself) localhost then enable, configure, interface 0/(whatever port you are dealing with) and finally run poe opmode shutdown,auto,passive24v depending on what you are doing.

So I needed this in a script and for the script to check the ip address of the camera to test if it was reachable (its not when it locks up) if not turn power off and on again (the bane of IT People everywhere!).

Also I had some previous experience with Ubiquiti equipment so I already knew I could put a rc.poststart file in /etc/persistent and make it executable (chmod 755 /etc/persistent/rc.poststart)

so now to create the script!  (added below for your review and use! no reason you have to beat your head on a table figuring it all out when it is done!)

*****<code>*****
#!/bin/sh

#declare url variable and set to ip address of item to be restarted if unreachable
url=192.168.0.60

#start while loop
while true
do
#ping once to ip to see if reachable and output to blackhole
ping -c 1 $url >/dev/null 2>&1

#test if ping was successful (0) or unsuccessful (1)
if [ $? == 1 ]

#if unsuccessful then login to telnet on switch and shut power off to the port
then
(echo "enable" ; echo "configure" ; echo "interface 0/1" ; echo "poe opmode shutdown" ; echo "exit" ; echo "exit"; ec

#wait 15 seconds before turning power back on to port
sleep 15

#turn power back on to port
(echo "enable" ; echo "configure" ; echo "interface 0/1" ; echo "poe opmode passive24v" ; echo "exit" ; echo "exit";

else
#do nothing basically and output to blackhole
echo "its good" >/dev/null 2>&1

fi
#wait 60 seconds before checking again
sleep 60
done


*****</code>*****

So there it is put into a unifi switch made executable and either reboot the switch or run it ./rc.poststart &

Hope this helps someone else trying to do the same thing I did, it took me a while to be able to get this working with the unifi switch as everyone on the web kept trying to get me to use "expect" however Ubiquiti did not include that so it really was not an option!

Feel free to leave comments or if you have a better way let me know! I am always looking to improve!

3 comments:

  1. Does the script automatically run after reboot?

    Everything I've read says that /etc/persistent/rc.poststart scripts are ignored by the Unifi line (not actually run on startup)

    ReplyDelete
  2. I've tested this and it doesn't work. The Unifi switch doesn't automatically run the rc.poststart file after a reboot.

    However, you can at least save the script and persist the script through reboots if you run the "save" command (without quotes). Otherwise the script will disappear after a reboot.

    ReplyDelete
  3. wow had forgotten about this until now! it did work then but that switch has been replaced and the camera that was having issues also, so I do not have anything to test with now to see if it works differently now so I will take you at your word.

    glad someone else is finding it somewhat useful.

    ReplyDelete