s-celles / APC

APC power switch automation
MIT License
30 stars 9 forks source link

Version 6 Support #8

Open cmccluskey opened 6 years ago

cmccluskey commented 6 years ago

I was looking into getting telnet running before looking into SSH.

But noticed some issues, in that the Schneider Electric updates to the PDUs have a very different command structure (see below).

I looks like the original expect code was around version 3 and forward. But between 2008 and 2015 the UI was revised (not sure if this was in 5.x or 6.x version of the RPDU software).

Any interest/advise in getting 6.x support into the expect interface?

Thank you, Chris


Escape character is '^]'.

User Name : XXX Password : *****

Schneider Electric Network Management Card AOS v6.4.0 (c) Copyright 2015 All Rights Reserved RPDU 2g v6.4.0

Name : XXXXXXXXXXXXXXX Date : 04/03/2018 Contact : Unknown Time : 19:35:23 Location : Unknown User : Super User Up Time : 74 Days 19 Hours 44 Minutes Stat : P+ N4+ N6+ A+

Type ? for command listing Use tcpip command for IP address(-i), subnet(-s), and gateway(-g)

apc>?

System Commands:

For command help: command ?

? about alarmcount boot bye cd
clrrst console date delete dir dns
email eventlog exit firewall format ftp
help lang lastrst ledblink logzip netstat
ntp ping portspeed prompt pwd quit
radius reboot resetToDef session smtp snmp
snmptrap snmpv3 system tcpip tcpip6 user
userdflt web whoami xferINI xferStatus

Device Commands:

devLowLoad devNearOver devOverLoad devReading devPeakLoad devStartDly dispID humLow humMin humHyst humReading lcd
lcdBlink olAssignUsr olCancelCmd olDlyOff olDlyOn olDlyReboot olGroups olName olOff olOffDelay olOn olOnDelay
olRbootTime olReboot olStatus olUnasgnUsr phLowLoad phNearOver
phOverLoad phReading phPeakCurr phRestrictn prodInfo sensorName
tempHigh tempMax tempHyst tempReading userAdd userDelete
userList userPasswd energyWise


Even an "outlet only" user (a reduced set of CLI commands) abandons the numerical menu interface) --

apc>?

System Commands:

For command help: command ?

? about alarmcount bye cd dir
eventlog exit help lang pwd quit
whoami

Device Commands:

olCancelCmd olDlyOff olDlyOn olDlyReboot olName olOff
olOffDelay olOn olOnDelay olRbootTime olReboot olStatus
prodInfo userList

s-celles commented 6 years ago

I owned an AP7900 (not B version), I'm not sure this hardware is compatible with version 6 (I think it's not).

Although PR is welcome if it doesn't break compatibility with previous firmware version.

deftdawg commented 4 years ago

I started messing with this before I realised it was only for power control (rather than UPS status, which I guess is only available via the snmp)

In any case, I did a little debugging and got it to login to my BG500 running v6.0.4... This patch may help someone else who wants to pursue this one further in the future.

--- utility.py  2020-02-28 13:10:42.391549836 -0500
+++ /usr/local/lib/python3.6/dist-packages/apc/utility.py       2020-02-28 13:25:03.655440381 -0500
@@ -11,6 +11,7 @@
 import re
 import time
 import pexpect
+import sys
 from apc.lockfile import FilesystemLock
 from apc.outlet import Outlet, Outlets

@@ -20,7 +21,7 @@
 APC_YES    = 'YES'
 APC_LOGOUT = '4'

-APC_VERSION_PATTERN = re.compile(r' v(\d+\.\d+\.\d+)')
+APC_VERSION_PATTERN = re.compile(r'.*v(\d+\.\d+\.\d+)')

 APC_DEFAULT_HOST     = os.environ.get('APC_HOST',     '192.168.1.2')
 APC_DEFAULT_USER     = os.environ.get('APC_USER',     'apc')
@@ -75,18 +76,21 @@
         if verbose:
             print("Running '%s'" % commandline)
         child = pexpect.spawn(commandline)
+        # Debugging uncomment the following line:
+        # child.logfile = sys.stdout.buffer

         child.timeout = 10
         child.setecho(True)

         child.expect('User Name : ')
-        child.send(user + '\r\n')
+        child.send(user+'\r')
         child.before
         child.expect('Password  : ')
-        child.send(password + '\r\n')
-
-        child.expect('Communication Established')
+        child.send(password+'\r')

+        # child.expect('Communication Established')
+        # FIXME: implement "or" match w/ expect for previous line and this one 
+        child.expect('apc>') 
         header = child.before

         match = APC_VERSION_PATTERN.search(str(header))
@@ -97,13 +101,17 @@
         version = match.group(1)

         self.info('Logged in as user %s, version %s'
-                  % (user, version))
+                    % (user, version))

         if version[0] == '3':
             apc = APC3(host, verbose, quiet)
-        else:
+        elif version[0] == '2':
             apc = APC2(host, verbose, quiet)
-
+        elif version[0] == '6' or version[0] == '5':
+            # FIXME: Implement this...
+            # apc = APC6(host, verbose, quiet)
+            raise NotImplementedError
+            
         apc.child = child
         apc.version = version
         apc.lock = self.lock

Here's a session with all of the commands on the BG500 for reference:

Trying 192.168.1.19...
Connected to 192.168.1.19.
Escape character is '^]'.

User Name : apc
Password  : **********

American Power Conversion               Network Management Card AOS      v6.0.1
(c) Copyright 2011 All Rights Reserved  Back-UPS APP                     v6.0.4
-------------------------------------------------------------------------------
Name      : BG500                                     Date : 2020-02-28
Contact   : Unknown                                   Time : 13:27:23
Location  : Unknown                                   User : Administrator
Up Time   : 0 Days 18 Hours 47 Minutes                Stat : P+ N4+ N6+ A+

Type ? for command listing
Use tcpip command for IP address(-i), subnet(-s), and gateway(-g)

apc>?

System Commands:
---------------------------------------------------------------------------
For command help: command ? 

?           about       alarmcount  boot        cd          clrrst      
console     date        delete      dir         dns         email       
eventlog    exit        firewall    format      ftp         help        
lang        lastrst     ledblink    logzip      netstat     ntp         
ping        portspeed   prompt      pwd         quit        radius      
reboot      resetToDef  session     smtp        snmp        snmptrap    
snmpv3      system      tcpip       tcpip6      user        userdflt    
web         whoami      xferINI     xferStatus