iLo server power on Python script for Telegram Bot

Having a Python Telegram bot with the function to power on servers through HP iLO can be convenient for several reasons:

  1. Remote management: If you have servers located in different physical locations, it can be challenging to manage them efficiently. With a Telegram bot that can power on servers through HP iLO, you can easily manage your servers from anywhere in the world. This can save you time and money by reducing the need for on-site support.
  2. Time-saving: Powering on servers manually can be a time-consuming task, especially if you have to power on multiple servers at once. With a Telegram bot that can automate this process, you can save a significant amount of time.
  3. Cost-effective: By using a Telegram bot to power on servers, you can reduce the need for physical access to the server room, which can save you money on travel expenses and on-site support.
  4. User-friendly interface: Using a Telegram bot is usually a straightforward process. Once you set it up, you can use a simple command to power on your servers. This can be more user-friendly than other methods that require technical knowledge.
See also  How to separate menu between ordinary and privileged users in Django

Overall, having a Telegram bot that can power on servers through HP iLO can be a convenient and efficient way to manage your servers remotely. It can save you time and money, reduce the need for physical access to the server room, and provide a user-friendly interface.

Earlier, I talked about how I created a bot that can turn off the ESXI server. Now I decided to add a shutdown function. For different generations, they differ.

 

import requests
#SERVER POWER ON FUNCTION BY ILO REST API
def ServerPowerOn(ilo_ip, ilo_user, ilo_pass):
    # Set up connection parameters
    ilo_url = f'https://{ilo_ip}/rest/v1/Systems/1/Actions/ComputerSystem.Reset'
    username = ilo_user
    password = ilo_pass

    # Set up request headers
    headers = {
        'Content-Type': 'application/json',
        'Connection': 'keep-alive'
    }

    # Set up request payload
    payload = {
        "Action": "Reset",
        "ResetType": "On"
    }

    # Send HTTP POST request to power on the server
    response = requests.post(ilo_url, verify=False, headers=headers, auth=(username, password), json=payload)

    if response.status_code == 200:
        return "Server is powering on..."
    else:
        print(f"Error rest: {response.status_code} - {response.text}")
        return "Error"
        
#SERVER POWER ON FUNCTION BY REDFISH FOR ILO5

def ServerPowerOnRedfish(ilo_ip, ilo_user, ilo_pass):
    # Set up connection to iLO server
    url = f"https://{ilo_ip}/redfish/v1/Systems/1/Actions/ComputerSystem.Reset"
    payload = {
        "ResetType": "On"
    }
    auth = (ilo_user, ilo_pass)
    # Set up request headers
    headers = {
        'Content-Type': 'application/json',
        'Connection': 'keep-alive'
    }

    # Send POST request to power on the server
    response = requests.post(url, headers=headers, json=payload, auth=auth, verify=False)

    # Check the response
    if response.status_code == 200:
        return "Server is powering on..."
    else:
        print(f"Error: {response.status_code} - {response.text}")
        return "Error"

 

See also  How to insert and implement datalist to Django Forms

Let me describe the two functions in more detail:

  1. ServerPowerOn(ilo_ip, ilo_user, ilo_pass): This function uses the HP iLO REST API to send a POST request to the iLO server to power on the server. It takes three parameters: ilo_ip, ilo_user, and ilo_pass, which represent the IP address of the iLO server, the username, and the password, respectively. The function sets up the request headers and payload, and then sends the POST request to the server. If the response status code is 200, the function returns the message “Server is powering on…”. If there is an error, the function prints the error message and returns “Error”.
  2. ServerPowerOnRedfish(ilo_ip, ilo_user, ilo_pass): This function uses the HP iLO5 Redfish API to send a POST request to the iLO server to power on the server. It takes the same three parameters as the first function: ilo_ip, ilo_user, and ilo_pass. The function sets up the connection to the iLO server, request headers, and payload, and then sends the POST request to the server. If the response status code is 200, the function returns the message “Server is powering on…”. If there is an error, the function prints the error message and returns “Error”.
See also  Python IMAP Email cleaner - automatic delete old messages

Both of these functions can be used in your Telegram bot to power on servers through HP iLO. They provide different options for interacting with the iLO server depending on which API you prefer to use. It’s good to have both options available, as it can give you more flexibility in how you manage your servers.

 

Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *