import telnetlib import re def get_baytech_status_outlet(hostname,port): try: # Create a Telnet object and connect to the host tn = telnetlib.Telnet(hostname, port) # Send the command you want to execute command = "status" tn.write(command.encode('ascii') + b"\r\n") # Read the output until you receive the prompt or a timeout occurs output = tn.read_until(b"", timeout=5).decode('ascii') # Close the Telnet connection tn.close() pattern = r"(\d+)\)\.\.\.Outlet\s+(\d+)\s+:\s+(\w+)" outlets = {} matches = re.findall(pattern, output) for match in matches: number = int(match[1]) status = match[2] outlets[number] = status return outlets except: return None