Hi,
Attached a quick python script I created to powercycle a POE port on my Ubiquity EdgeSwitch in case BlueIris reports a "signal lost".
You need python 3.x to run it.
Usage: python.exe ubntpowercycle.py --ip <ip of switch> --username <username> --password <password> --interfaces <intf>
Example (will cycle interface 0/12 and 0/14):
python.exe ubntpowercycle.py --ip 192.168.0.2 --username admin --password admin --interfaces 0/12 0/14
Attached a quick python script I created to powercycle a POE port on my Ubiquity EdgeSwitch in case BlueIris reports a "signal lost".
You need python 3.x to run it.
Usage: python.exe ubntpowercycle.py --ip <ip of switch> --username <username> --password <password> --interfaces <intf>
Example (will cycle interface 0/12 and 0/14):
python.exe ubntpowercycle.py --ip 192.168.0.2 --username admin --password admin --interfaces 0/12 0/14
Code:
import telnetlib
import re
# https://community.ubnt.com/t5/UniFi-Routing-Switching/Power-Cycle-POE-port-on-UniFi-Switch-remotely/td-p/1723120
def powercycle(ip, username, password, interfaces):
tn = telnetlib.Telnet(ip)
def encode(s):
return s.encode('utf-8')
def write_line_and_read_until(cmd, expected):
tn.write(encode(cmd + "\n"))
tn.read_until(encode(expected))
def write_line_and_expect(cmd, expect, timeout=1):
tn.write(encode(cmd + "\n"))
tn.expect(expect, timeout)
tn.read_until(encode("User:"))
write_line_and_read_until(username, "Password:")
write_line_and_expect(password, [re.compile(encode(".*>"))])
write_line_and_expect("en", [re.compile(encode(".*#"))])
write_line_and_expect("config", [re.compile(encode("\(Config\)#"))])
for intf in interfaces:
print("Cycling intf {}".format(intf))
r = re.compile(encode("\(Interface {}\)#".format(intf)))
write_line_and_expect("interface {}".format(intf), [r])
write_line_and_expect("poe opmode shutdown", [r])
write_line_and_expect("poe opmode auto", [r])
write_line_and_expect("exit", [re.compile(encode("\(Config\)#"))])
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Power cycle UBNT POE port.')
parser.add_argument('--username', type=str, required=True)
parser.add_argument('--password', type=str, required=True)
parser.add_argument('--ip', type=str, required=True)
parser.add_argument('--interfaces', type=str, required=True, nargs="+")
args = parser.parse_args()
powercycle(args.ip, args.username, args.password, args.interfaces)