60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
import os
|
|
import subprocess
|
|
import time
|
|
from dotenv import load_dotenv
|
|
import sys
|
|
from multiprocessing import Pool
|
|
import logging
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s [INFO] root: %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S'
|
|
)
|
|
|
|
# Load environment variables
|
|
load_dotenv() # Load variables from .env file
|
|
|
|
# Credentials
|
|
IDRAC_USER, IDRAC_PASS = os.getenv("IDRAC_USER"), os.getenv("IDRAC_PASS")
|
|
|
|
# Load IP addresses from file
|
|
def load_ip_file(ip_file_path):
|
|
if not os.path.isfile(ip_file_path):
|
|
logging.error(f"IP file {ip_file_path} does not exist.")
|
|
sys.exit(1)
|
|
with open(ip_file_path, "r") as file:
|
|
return [line.strip() for line in file if line.strip()]
|
|
|
|
# Power on the server for given iDRAC IP
|
|
def poweron_idrac_server(idrac_ip):
|
|
logging.info(f"Powering on server for iDRAC IP: {idrac_ip}")
|
|
try:
|
|
result = subprocess.run(
|
|
["racadm", "-r", idrac_ip, "-u", IDRAC_USER, "-p", IDRAC_PASS, "serveraction", "powerup"],
|
|
capture_output=True,
|
|
text=True
|
|
)
|
|
if result.returncode == 0:
|
|
logging.info(f"Successfully powered on server for {idrac_ip}")
|
|
else:
|
|
logging.error(f"Failed to power on server for {idrac_ip}: {result.stderr.strip()}")
|
|
|
|
except Exception as e:
|
|
logging.error(f"Exception occurred for {idrac_ip}: {e}")
|
|
|
|
# Main function
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
sys.exit("Usage: python script.py <ip_file>")
|
|
|
|
ip_list = load_ip_file(sys.argv[1])
|
|
start_time = time.time()
|
|
|
|
# Execute in parallel using Pool (max 10 simultaneous processes)
|
|
with Pool() as pool:
|
|
pool.map(poweron_idrac_server, ip_list)
|
|
|
|
elapsed_time = time.time() - start_time
|
|
logging.info(f"Server Power On 완료. 완료 시간: {int(elapsed_time // 3600)} 시간, {int((elapsed_time % 3600) // 60)} 분, {int(elapsed_time % 60)} 초.") |