MRV LX-5250 switched PDU telnet control python script

Notice: Page may contain affiliate links for which we may earn a small commission through services like Amazon Affiliates or Skimlinks.

tinfoil3d

QSFP28
May 11, 2020
883
407
63
Japan
So I have this very simple PDU unit and I thought I'd be happily driving it over SNMP but no. First time powering it, aaaaand It doesn't have SNMP, HTTP or SSH. Nothing but telnet.
Okay, so I wrote this script, hope it helps someone looking for automation.
LX-5250 default username and password are admn:admn
In order for the it to work, you need to first rename all outlets with "set outlet name" or something. Otherwise you have to append ".A" to the number.
Symlink "mrv-on", "mrv-off" and "mrv-status" to this script and you'll have a busybox-like multicall tool. What you also may want is rename -status to -istat and add the -status to correspond to actual telnet commands.

CC0 license, no guarantees.

Python:
#!/usr/bin/env python3
import sys
import argparse

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

import telnetlib

host = "192.168.0.9"
port = 23
#user = "admn\r\n" # this is default user:pass
user = "admin\r\n"
    
def main():
    parser = argparse.ArgumentParser(description="multicall script, mrv-<status|on|off> [outlet-no-if-on-or-off-selected]")
    parser.add_argument("outlet", nargs="?", type=int, help="Outlet number (1-8)")

    args = parser.parse_args()

    if args.outlet is None:
        # Handle the case where argv[0] ends with "-status"
        if sys.argv[0].endswith("-status"):
            command = "istat"
        else:
            print("Error: Outlet number is required.")
            parser.print_usage()
            sys.exit(1)
    else:
        outlet = args.outlet

        if not 1 <= outlet <= 8:
            print("Outlet number must be an integer between 1 and 8.")
            sys.exit(1)

        # Derive the command from the script name
        script_name = sys.argv[0]
        if script_name.endswith("-on"):
            command = "on"
        elif script_name.endswith("-off"):
            command = "off"
        else:
            print("Script name must end with '-on' or '-off'.")
            sys.exit(1)

    try:
        tn = telnetlib.Telnet(host,port)
        tn.read_until(b'Username:',timeout=5)
        tn.write(user.encode('ascii'))
        tn.read_until(b'Password:',timeout=5)
        tn.write(user.encode('ascii'))
        tn.read_until(b'LX: ',timeout=5)
        
        if args.outlet is not None:
            command_to_send = f"{command} {outlet}\r\n"
        else:
            command_to_send = f"{command}\r\n"
        tn.write(command_to_send.encode('ascii'))
        
        # Read the response after sending the command
        response = tn.read_until(b"Command successful", timeout=5)
        if args.outlet is None:
            print(response.decode('ascii'))
        sys.exit(0)

    except Exception as e:
        print(f"An error occurred: {str(e)}")

    finally:
        tn.close()

if __name__ == "__main__":
    main()
    sys.exit(1)
 
  • Like
Reactions: vudu