Python – telnet – automation APC PDU

Question:

I am attempting to automation turning off connected devices to an APC PDU using python’s built in telnet functionality. I believe I am able to enter in the username and password, but I cannot proceed after that. My code is as follows:

    telnet_session = telnetlib.Telnet(host=self.apc_ip)
    self.logger.info("Entering APC Username")
    telnet_session.read_until("User Name :")
    telnet_session.write(self.apc_username + "n")
    self.logger.info("Entering APC Password")
    telnet_session.read_until("Password  :")
    telnet_session.write(self.apc_password + "n")
    sleep(2)
    print telnet_session.read_all()

I’m running windows so I believe that https://github.com/quackenbush/APC is not an option as I cannot install pexpect.

Any help would be appreciated.

Thanks,

Parth

Asked By: Parth

||

Answers:

The issue was I wasn’t sending the correct end line.

In windows it’s rn, not n

Answered By: Parth

Hello to all happy APC Switched and Metered PDU owners and admins! 🙂

Last weekend I needed to automate outlet switching in my home lab. Here’s what I’ve got for ap7901 & ap7902 PDUs:
https://github.com/MyElectrons/PDU-Commander

A sample command, executed from bash command line or a script:

./pdu-commander.py -a 192.168.7.242 -u device -p goodpassword on:1-2,8 delay:3 off:2 get:power

Script’s output:

Address: 192.168.7.242
Unit ID: ap7902
Outlets: 16
('on', '1-2,8')
1: Outlet 1       : Outlet Turned On
2: Outlet 2       : Outlet Turned On
8: Outlet 8       : Outlet Turned On
('delay', '3')
('off', '2')
2: Outlet 2       : Outlet Turned Off
('get', 'power')
168 VA
168 Watts

There’s logging, error handling, and a big potential for growth in that little project.
New features, improvement suggestions, and pull requests are very welcome!

Cheers,

  • Serge

P.S. By the way Telnet seems to be always sending rn regardless the OS. I had to resort to Wireshark to believe it 🙂

P.P.S I posted this answer in the thread mentioned above too. Just in a hope it might still help someone…

Answered By: MyElectrons
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.