#StackBounty: #python #telnet #pexpect APC PDU CLI automation over telnet

Bounty: 100

I have seen numerous posts online about logging into an APC PDU over telnet using Python pexpect, PHP, etc. However, all of the examples are like this:

Python – telnet – automation APC PDU


They log in using the Control Console, which I don’t think offers as many metrics. I’m looking for a way to automate the command line interface and send various commands to check metrics.

I’ve learned this is possible by appending -c to your password at the login prompt:

APC_DEFAULT_USER     = 'apc'
self.child.expect('User Name : ')
self.child.send(APC_DEFAULT_USER + 'rn')
self.child.expect('Password  : ')
self.child.send(APC_DEFAULT_PASSWORD + 'rn')

But this is where I get stuck. I’d like a generic solution to allow me to specify different PDU commands as command line args like this:

--command power
--command current

I am using argparse to get the arguments from the user, so args.command looks like this:

parser.add_argument('--command', action='store', default='',
                    help='The command to run')

I also have no strong feelings about doing this in Python. I’m more than happy to use another language if it’s more intuitive. I’m just trying to base my script on the quankenbush/APC script.

I’ve tried doing expects for the APC> prompt like this:

self.info("Attempting to run command: {}".format(self.command))
self.child.expect('APC> ')
self.child.expect(' OK')
self.child.expect('APC> ')

But I end up getting

“Command does not exist”

errors, even though I know the “power” and “current” commands do exist.

For completeness, my end goal is to run this script every minute or few minutes and write the data to a file to track power usage over time and eventually create some graph of this data.

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.