Using perl to automate a common telnet session
Recent blog posts
- Google doc Drupal blog managment update
- Happy Memorial Day!
- Configure Twitter alerts from Zenoss
- Iron Man 2 Review
- 2 weeks with Dropbox
- Blackberry Desktop Manager error 4238 while running a intellisync operation
- Virustotal.com scan any file with 20+ antivirus engines for free
- Using Google Docs to write Drupal Content and Blogs
- RedHat Enterprise Linux 6 in Virtualbox woes
- Fixing a broken Active Directory DNS Server Walkthrough
Submitted by admin on Thu, 09/25/2008 - 08:38
I have been fighting with a server that is running out of resources at the data center, and it is impossible for me to access the machine remotely and kill the offending process some of the times. Since the machine is connected to a managed power distribution unit, I wanted to find a way that I could script of the power cycling of the outlet. My first thought was to see if I could do it using IPSentry the server alert and monitoring system that we have in place right now, which is basically a Windows version of Nagios but not near as flexible. I found that none of the add-ins that were available could provide this functionality, so I needed to script it out somehow.
So off to scripting land I go. I decided to script it in perl because it's going to be quick and easy and I already have ActivePerl installed on the monitoring server. After a little bit of research the Telnet function that is native in perl is unable to pipe commands through it. So I sat back and was like crap, what am I going to do. So after a little googling, I found a perl module Net-Telnet which fit my needs. You can read up on it more here. So now I needed to map out what I needed to have the script do. It was pretty simple, connect to the APC PDU, login, go through a key sequence and close.
Here is the Script that I have came up with
use Net:Telnet
$telnet = new Net:Telnet ( Timeout=>2, Errmode=>'die');
# Connecting to APC PDU via telnet
$telnet->open('111.111.111.111');
# Supply login credentials
$telnet->waitfor('User Name');
$telnet->print('apc');
$telnet->waitfor('Password');
$telnet->print('yourpassword');
# Reboot sequence start
$telnet->waitfor('Device Manager');
$telnet->print('1');
$telnet->waitfor('Outlet Management');
$telnet->print('2');
$telnet->waitfor('Outlet Control/Configuration');
$telnet->print('1');
$telnet->waitfor('Outlet Name you want to control');
$telnet->print('the outlet number here');
$telnet-waitfor('Control Outlet');
$telnet-print('1');
$telnet-waitfor('Immediate Reboot');
$telnet-print('3');
# Reboot confirmation
$telnet-waitfor('Yes');
$telnet-print('yes');
# Reboot sequence end
# Close Telnet Session




