-
-
Save emaadmanzoor/23268681cd47f8ef07d9 to your computer and use it in GitHub Desktop.
Use IPMI to power cycle a server. This application uses a simple curses UI for taking inputs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import curses | |
import pyipmi | |
from pyipmi import make_bmc | |
from pyipmi.bmc import LanBMC | |
from pyipmi.server import Server | |
def get_param( prompt_string ): | |
''' Takes a single input from the user after printing the prompt string. ''' | |
screen.clear() | |
screen.border( 0 ) | |
screen.addstr( 2, 2, prompt_string ) | |
screen.refresh() | |
input = screen.getstr( 2, 2 + len( prompt_string ), 60 ) | |
return input | |
while True: | |
screen = curses.initscr() | |
screen.clear() | |
screen.border( 0 ) | |
screen.addstr( 2, 4, "Press 1 to power on the machine" ) | |
screen.addstr( 3, 4, "Press 2 to power off the machine" ) | |
screen.addstr( 4, 4, "Press q to exit this program." ) | |
screen.refresh() | |
x = screen.getch() | |
if x == ord( 'q' ): | |
break | |
mode = 0 | |
mode = 0 if x == ord( '1' ) else 1 | |
hostname = get_param( "Enter the server's IP address: " ) | |
username = get_param( "Enter your username: " ) | |
password = get_param( "Enter your password:" ) | |
bmc = make_bmc( LanBMC, hostname=hostname, username=username, password=password ) | |
server = Server( bmc ) | |
try: | |
if mode: | |
screen.addstr( 10, 10, "Powering off the server." ) | |
server.power_off() | |
else: | |
screen.addstr( 10, 10, "Powering on the server." ) | |
server.power_on() | |
except pyipmi.IpmiError, e: | |
# Ensuring that the program exists gracefully even if | |
# the command is not executed succesfully. | |
curses.endwin() | |
print "A problem occurred while communicating to the server. " \ | |
"Is IPMI configured correctly on it?" | |
break | |
curses.endwin() | |
print "The server is in state: %s" % server.is_powered |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment