Controlling IP PDU NetPing Sockets Using Telegram

  • Published In: Tutorial
  • Created Date: 2018-05-30
  • Hits: 1544

A vacation time is getting closer. As any other employees, system administrators also dream about having a vacation somewhere far from their workplace. For a majority of system administrators, especially in small organizations, it is not that easy to get distracted from work even for a relatively short time. Many of them are single-handedly responsible for a status of different components of the IT infrastructure. So how an IT specialist can have a vacation being confident that a company can work the same efficiently, as usual? Before, we already examined a process of creating bots for a «Telegram» messenger in our blog. In this article, we examine one of the variants of remote controlling a power supply of the IT equipment from any location of the Earth (having the Internet access) using the integration of a «Telegram» messenger and NetPing power distribution units (IP PDUs). If necessary, an IT specialist sends a command for a socket using «Telegram». After applying a command in «Telegram», a specialist will receive a notification about a successful application of a command and a socket changes its status.

Required Equipment, Software, and Preparing for the Development:

To implement a functionality of remote controlling sockets of a NetPing power distribution unit using «Telegram», the next set of equipment is required:

  • A power distribution units (NetPing 8/PWR-220 v4/SMS, NetPing 4/PWR-220 v4/SMS, NetPing 2/PWR-220 v4/SMS or NetPing 2/PWR-220 v3/ETH) – 1 pc.;
  • Connected load – the number depending on the number of built-in IP PDU NetPing sockets;
  • PC or a server with a preinstalled operating system Linux or Windows and an Internet access – 1 pc.;
  • A local network for establishing a connection between a monitoring device and PC, where a script of a bot is run – 1 pc.;
  • «Telegram» messenger on PC or a smartphone with an Internet access – 1 pc.;

In our example, we will use a NetPing 4/PWR-220 v4/SMS power distribution unit. Let's consider that a device is configured for the operation on your local network. More detailed information about settings is available in a documentation. It is also suggested that you have a configured server with an OS Ubuntu, and bots created according to the recommendations of this article. In this example, we created a bot with a name @NetPing_4PWR_bot. 

Programming Functions for Controlling a Power Supply for a Bot @NetPing_4PWR_bot

Let's program an extended functionality for our bot @NetPing_4PWR_bot. Edit the main script file that manages a bot – «bot.py»:

import telebot
import requests
from telebot import types
import conf_bot

auth = conf_bot.auth
url = conf_bot.url
bot = telebot.TeleBot(conf_bot.TOKEN)

# Processing commands "/start" and "/help"
@bot.message_handler(commands=['start', 'help'])
def start(message):
markup = types.ReplyKeyboardMarkup()
markup.row('/relay1', '/relay2')
markup.row('/relay3', '/relay4')
markup.row('/npstatus', '/help')
bot.send_message(message.chat.id, ''' 
***Test bot to control the outlets of NetPing 4/PWR-220 v4/SMS ***

To query the current status of the relay, use the command /npstatus

To control relay #1, use the /relay1 command

To control relay #2, use the /relay2 command

To control relay #3, use the /relay3 command

To control relay #4, use the /relay4 command

To access this help, use the /help command

''',
reply_markup=markup)

# Processing the "/npstatus" command, getting information about the relay status
@bot.message_handler(commands=['npstatus'])
def status(m):
i=1
while i <=4:
def relay ():
r = requests.get(url+'relay.cgi?r'+str(i), auth=auth)
io = r.text[22:23] 
if io == '0':
io = ('Current state of the relay #' 
+ str(i) + ': ' + io + ' Off')
return io
else:
io= ('Current state of the relay #' 
+ str(i) + ': ' + io + ' On')
return io 
io_stat = relay() 
sent = bot.send_message(m.chat.id, io_stat ) 
i=i+1
# Processing the "/relay1" command, creating a menu.
@bot.message_handler(commands=['relay1'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 1', 
'Disable relay 1', 'Reset relay 1' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 1.

Select the required action:''',
reply_markup=keyboard)

# Processing the "/relay2" command, creating a menu
@bot.message_handler(commands=['relay2'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 2', 
'Disable relay 2', 'Reset relay 2' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 2.

Select the required action:''',
reply_markup=keyboard)

# Processing the "/relay3" command, creating a menu
@bot.message_handler(commands=['relay3'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 3', 
'Disable relay 3', 'Reset relay 3' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 3.

Select the required action:''',
reply_markup=keyboard)

# Processing the "/relay4" command, creating a menu
@bot.message_handler(commands=['relay4'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 4', 
'Disable relay 4', 'Reset relay 4' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 4.

Select the required action:''',
reply_markup=keyboard)

# Processing of pressing the menu button "Enable relay 1"
@bot.callback_query_handler(func=lambda c: True)
def inline(c):
if c.data =='Enable relay 1':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 1 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r1=1', auth=auth)
#Processing of pressing the menu button "Disable relay 1"
elif c.data =='Disable relay 1':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 1 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r1=0', auth=auth)
#Processing of pressing the menu button "Reset relay 1"
elif c.data =='Reset relay 1':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 1 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r1=f,10', auth=auth)

# Processing of pressing the menu button "Enable relay 2"
elif c.data =='Enable relay 2':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 2 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r2=1', auth=auth)
#Processing of pressing the menu button "Disable relay 2"
elif c.data =='Disable relay 2':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 2 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r2=0', auth=auth)
#Processing of pressing the menu button "Reset relay 2"
elif c.data =='Reset relay 2':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 2 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r2=f,10', auth=auth)

# Processing of pressing the menu button "Enable relay 3"
elif c.data =='Enable relay 3':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 3 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r3=1', auth=auth)
#Processing of pressing the menu button "Disable relay 3"
elif c.data =='Disable relay 3':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 3 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r3=0', auth=auth)
#Processing of pressing the menu button "Reset relay 3"
elif c.data =='Reset relay 3':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 3 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r3=f,10', auth=auth)

# Processing of pressing the menu button "Enable relay 4"
elif c.data =='Enable relay 4':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 4 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r4=1', auth=auth)
#Processing of pressing the menu button "Disable relay 4"
elif c.data =='Disable relay 4':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 4 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r4=0', auth=auth)
#Processing of pressing the menu button "Reset relay 4"
elif c.data =='Reset relay 4':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 4 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r4=f,10', auth=auth) 
bot.polling()

Source files of a bot can be downloaded here.

Working with a Bot for Remote Controlling a Power Supply

Enable a dialogue with a bot @NetPing_4PWR_bot, in a script for controlling which we added new features. After we send a command «/start» (1) to a bot, a memo (2) will be displayed in a dialogue box, and buttons (3) will appear below a box for entering a message that duplicate commands for controlling a bot:

Telegram starting a dialogue with a bot to control a NetPing 4PWR-220 v4 SMS power distribution unit

Commands «/relay1» - «/relay4» are used for controlling sockets of a NetPing 4/PWR-220 v4/SMS power distribution unit. When choosing a command, in a dialogue window a notification about a chosen command (1) and buttons for choosing possible actions (2) are displayed. On a screenshot below, all four commands for controlling sockets are selected one by one:

Telegram command output for controlling sockets of a NetPing 4PWR-220 v4 SMS power distribution unit

Let's examine commands for controlling sockets in more details. A bot controls sockets of a NetPing 4/PWR-220 v4/SMS power distribution unit using URL-encoded commands that are specified in a script code of a bot and assigned to buttons. To control relays, the commands «On», «Off» and «Reset» are available. A command «Reset» executes a short-term 10-second switching of a relay to an inverse state (issuing a reset pulse). Therefore the equipment connected to the socket is reset. When a command is under execution, a notification about a current relay state is displayed in a dialogue window. On a screenshot below, there are the results of consequent clicking of the control button for the relay 1:

Telegram displaying notifications about a relay status of a NetPing 4PWR-220 v4 SMS power distribution unit

A command «/npstatus» allows to request a current status of all sockets of a NetPing 4/PWR-220 v4/SMS rack PDU. In this example, a command «/npstatus» does not have buttons for actions, and a status of sockets is requested as soon as a command is selected:

Telegram displaying a current status of all sockets of a NetPing 4PWR-220 v4 SMS power distribution unit

Therefore, we got a convenient tool for remote controlling a power supply of different IT devices (servers, switches, routers, Wi-Fi spots, modems, and other equipment) from any part of the world where there is the Internet access, thanks to which a system administrator can comfortably take a vacation.


comments powered by Disqus