Skip to main content
Pulling APC RMS data via SSH

Pulling APC RMS data via SSH

·185 words·1 min
Mike Curtis
Author
Mike Curtis
Dedicated to Technology
Table of Contents

Pulling APC RMS data via SSH
#

this is a simple script to pull RMS data from an APC AP7901.

import paramiko
import time
import sys
import re

def get():
    # 1- Device Manager\n  ->  1- Phase Management\n
    commandlist = ['1\r','1\r']
    # VARIABLES THAT NEED CHANGED
    ip = ''
    username = ''
    password = ''
    rms = ''
    try:
        # Create instance of SSHClient object
        remote_conn_pre = paramiko.SSHClient()

        # Automatically add untrusted hosts (make sure okay for security policy in your environment)
        remote_conn_pre.set_missing_host_key_policy(
             paramiko.AutoAddPolicy())
        print ('connecting to '+ ip)
        # initiate SSH connection
        remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False)
        print( "SSH connection established to %s" % ip)

        # Use invoke_shell to establish an 'interactive session'
        remote_conn = remote_conn_pre.invoke_shell()
        print ("Interactive SSH session established")

        # Strip the initial router prompt
        output = remote_conn.recv(1000)
        data = ''
        # See what we have
        #print (output)
        #remote_conn.send("\n")
        for c in commandlist:
            print('Sending '+c)
            remote_conn.send(c)
            time.sleep(2)
            if remote_conn.recv_ready():
                output = remote_conn.recv(5000)
                data += str(output)
        #regex
        #Phase Load :  [0-9].[0-9]
        v = re.search(r"Phase Load :  [0-9].[0-9]", data)
        rms = v.group(0)
        rms = rms.replace('Phase Load :  ','')
    except:
        rms = "0.0"
    return rms

Related