Contact directly Cisco and Huawei switch routers through Telegram messaging app

Featured Image
Hasan-Uz-Zaman Ashik

Written by Hasan-Uz-Zaman

April 11, 2022

Telegram Bot is a third-party application that runs inside the popular messaging app Telegram. Telegram Bots can perform various functions, such as sending messages, photos, videos, and files, among others. Some popular use cases for Telegram Bots include:

  1. News and information delivery: Telegram Bots can be used to send news updates, weather forecasts, sports scores, and other real-time information.

  2. Automated customer support: Telegram Bots can be used to provide instant support to customers by answering common questions, resolving technical issues, and providing information about products and services.

  3. E-commerce and shopping: Telegram Bots can be integrated with e-commerce platforms to provide customers with a seamless shopping experience, including product recommendations, order tracking, and payment processing.

  4. Social media management: Telegram Bots can be used to manage social media accounts, such as Twitter and Instagram, from within Telegram.

  5. Fun and entertainment: Telegram Bots can also be used for fun and entertainment, such as playing games, taking quizzes, and generating memes.

Telegram Bots are created using the Telegram Bot API, which provides a set of tools and functions for building and running bots. The API is well-documented and easy to use, making it accessible to developers of all skill levels.

Optical power of devices like as switches and routers degrades for a variety of causes in the telecom industry. Network monitoring professionals frequently use cli to check optical power in real time, which necessitates learning all commands related to checking optical power for all vendors and models. Furthermore, manually logging into the device and running commands takes time. This verification can also be done using python and a messaging platform like Telegram as an alternative. The user will give merely the device’s IP and port name to a Telegram chat bot or in a Telegram group, and the bot will use SSH to login to the device and retrieve optical info for the user. So, any operation may be accomplished in this manner directly from the messaging app without the user having to log in to the device.

 

In order to develop this, we have to ensure followings:

  1. List all device models and optical power related commands.
  2. Configure SSH in all network devices (who are in public or private network)
  3. Setup a server/PC from where devices are reachable.

Now that we have our materials, we can begin constructing it.

We shall carry out the following actions:

  1. Setup a telegram chat-bot who will listen and reply our messages
  2. Read and filter device IP and port from telegram messages sent from user using python.
  3. Connect with switch, routers and find their model number, then send proper optical power related command according to device model using Netmiko.
  4. Finally, show output in telegram as a response of user input message.

 

Setup telegram chat-bot:

To see what functionalities are available, type BotFather into the telegram search box and send the message /help.

Send /newbot to create a new bot with a name and a unique username. Bot letters must be added to the end of the username. After successfully building a bot, you’ll receive an HTTP API key, which we’ll use to communicate with our bot in Python later.

Then, let’s create a telegram group with our bot. After that for security change the privacy settings of bot so that no one can add this bot to other group.

Send /setjoingroups message to BotFather and then send Disable.

‘Disable’ – block group invitations, the bot can’t be added to groups.

Now no one can add your bot to other group but they can still send message to your bot. We will control it latter in python.

Now send /setcommands and Send /empty to keep the command list empty. After that send /setprivacy and send Disable.

‘Disable’ – your bot will receive all messages that people send to groups.

Now let’s list some of our device modes with optical power commands. We will group those models according to similarity of commands.

Device Model Optical power see command
Cisco A901-12C-F-D show int Gi0/10 transceiver
Cisco A901-12C-FT-D show int Gi0/10 transceiver
Cisco A901-6CZ-F-A show int Gi0/10 transceiver
Cisco A901-6CZ-F-D show int Gi0/10 transceiver
Cisco A901-6CZ-FT-D show int Gi0/10 transceiver
cisco ASR-902 (RSP2) sh hw-module subslot 0/1 transceiver 1 status
cisco ASR-903 (RSP1) sh hw-module subslot 0/1 transceiver 1 status
cisco ASR-903 (RSP2) sh hw-module subslot 0/1 transceiver 1 status
cisco ASR-920-12CZ-D sh hw-module subslot 0/1 transceiver 1 status
cisco ASR-920-24SZ-IM sh hw-module subslot 0/1 transceiver 1 status
cisco ASR-920-24SZ-M sh hw-module subslot 0/1 transceiver 1 status
cisco ASR-920-8S4Z-PD sh hw-module subslot 0/1 transceiver 1 status
cisco ASR9K sh controllers Hu0/5/0/15 phy
cisco ME-3400-24FS-A show int Gi0/10 transceiver
cisco ME-3400-24TS-A show int Gi0/10 transceiver
cisco ME-3800X-24FS-M show int Gi0/10 transceiver
cisco N540X-6Z18G-SYS-D sh controllers Gi0/0/0/16 phy  | i x Power:
cisco NCS-540 sh controllers Gi0/0/0/16 phy  | i x Power:
cisco NCS-5500 sh controllers Gi0/0/0/16 phy  | i x Power:
cisco WS-C2960-24TC-L show int Gi0/10 transceiver
cisco WS-C2960-24TC-S show int Gi0/10 transceiver
HUAWEI ATN 910B-F DC display int phy-option Gi 0/2/15
HUAWEI CE6865-48S8CQ-EI display int 100GE1/0/4 transceiver brief
HUAWEI NE40E-M2K display int phy-option Gi 0/2/15
HUAWEI NE40E-X8A display int phy-option Gi 0/2/15
HUAWEI NetEngine 8000 M8 display int phy-option Gi 0/2/15
HUAWEI S2320-28TP-EI-AC Routing Switch display transceiver diagnosis int Gi 0/0/5
HUAWEI S5300-28X-LI-24S-AC Routing Switch display transceiver diagnosis int Gi 0/0/23
HUAWEI S5320-32X-EI-24S-AC Routing Switch display transceiver diagnosis int Gi 0/0/10
HUAWEI S5320-32X-EI-24S-DC Routing Switch display transceiver diagnosis int Gi 0/0/10
HUAWEI S5320-32X-EI-DC Routing Switch display transceiver diagnosis int XGi 0/0/1
HUAWEI S5720S-28X-LI-24S-AC Routing Switch display transceiver diagnosis int Gi 0/0/1
HUAWEI S6320-30C-EI-24S-DC Routing Switch display transceiver diagnosis int 40GE0/0/1
HUAWEI S6320-50L-HI-48S Routing Switch display transceiver diagnosis int XGi 0/0/30
HUAWEI S6720-30C-EI-24S-AC Routing Switch display transceiver diagnosis int XG0/0/14
Quidway S2318TP-EI Routing Switch display transceiver diagnosis int Gi 0/0/1
Quidway S3328TP-EI-24S Routing Switch display transceiver diagnosis int Gi 0/0/1
Quidway S3328TP-SI Routing Switch display transceiver diagnosis int Gi 0/0/3
Quidway S3700-28TP-EI-24S-AC Routing Switch display transceiver diagnosis int Gi 0/0/4
Quidway S5328C-EI-24S Routing Switch display transceiver diagnosis int Gi 0/0/1
HUAWEI NE05E-SE display int phy-option Gi 0/2/24 | include l Power
HUAWEI NE20E-S2F display int phy-option Gi 0/2/15
HUAWEI NE40E-X3A display int phy-option Gi 0/2/15

Let’s suppose we have 4 types devices where device credentials or vendor name are different. We will make our program to try one SSH connection attempt and if not succeed we will try another way of connecting the device.

We started by importing the essential modules. Then we’ll develop some functions to help us with this task. With the specified SSH connection object, the find_model function will send a command to the device and return us device version.

 

To distinguish between Cisco and Huawei devices, script will find the device prompt and search for the ‘>’ or ‘#’ sign in the hostname. The ‘#’ symbol appears in the hostname of Cisco devices, while the ‘>’ symbol appears in the hostname of Huawei devices. This has been implemented in the function named try_connect.

 

After that, we’ll create another function called make_connect, which will attempt to connect to the device using a variety of device types and credentials and return the SSH connection object to us. Device type, device prompt, and error message will all be returned as well.

 

We set ‘terminal length 0’ in the give_me_optical_power function to avoid partial output missing complexity. It will be ‘screen-length 0 temporarily’ for Huawei devices. We are grouping our devices and issuing optical power specific commands if our device version contains a specified device model string. Finally, we get output power from the devices.

 

To validate that user has gave us a valid IP in the telegram message we have made a function validate_ip which will check the IP is valid or not.

 

 

Finally, we use the API key to connect to the Telegram bot in the main() function. The infinity_polling() function helps us to read user input messages continously. We’re matching user chat ids with expected ids for security reasons. As a result, if another individual sends a message to our bot, it will not give any optical power information.

 

If user send a valid IP with coma separated port name then it will connect the device and find optical power.

 

 

We are matching __main__ before running the main function because we only want to run the script when executed and not when imported in another script.

 

Github link.

Screenshoot of telegram app which is showing optical power of different devices. We have provided device ip and port name saparated by a coma. In the output we can see that bot has replied us with optical power information. 

You May Also Like…

Hasan-Uz-Zaman

Hasan-Uz-Zaman

Network Engineer

Zaman is an aspiring Technical Writer and passionate about software-defined networking (SDN), Network Automation, Ansible, Log data management (Syslog-ng), Python tools, Web Application development (django) etc.

Let's start talking!

0 Comments

Submit a Comment

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