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:
-
News and information delivery: Telegram Bots can be used to send news updates, weather forecasts, sports scores, and other real-time information.
-
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.
-
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.
-
Social media management: Telegram Bots can be used to manage social media accounts, such as Twitter and Instagram, from within Telegram.
-
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:
- List all device models and optical power related commands.
- Configure SSH in all network devices (who are in public or private network)
- 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:
- Setup a telegram chat-bot who will listen and reply our messages
- Read and filter device IP and port from telegram messages sent from user using python.
- Connect with switch, routers and find their model number, then send proper optical power related command according to device model using Netmiko.
- 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.
1 2 3 4 |
def find_model(net_connect,cmd): #send_command("show whatever", delay_factor=2) output = net_connect.send_command(cmd) return output |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def try_connect(src_ip,router): #print('Connecting with ',src_ip) net_connect = ConnectHandler(**router) device_prompt = net_connect.find_prompt() #print(device_prompt) if '#' in device_prompt: device_type = 'cisco' elif '>' in device_prompt: device_type = 'huawei' else: device_type = 'none' return net_connect,device_type,device_prompt |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
def make_connect(src_ip): router = { 'device_type': 'autodetect', 'host': src_ip, 'username': 'username1', 'password': 'password1', 'port' : 22, 'secret': '', } device_type = '' device_prompt = '' net_connect = '' error_message = '' connect_success = 0 try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' if connect_success == 0: router = { 'device_type': 'cisco_xr', 'host': src_ip, 'username': 'username2', 'password': 'password2', 'port' : 22, 'secret': '', } try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' if connect_success == 0: router = { 'device_type': 'huawei', 'host': src_ip, 'username': 'username3', 'password': 'password3', 'port' : 22, 'secret': '', } try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' if connect_success == 0: router = { 'device_type': 'autodetect', 'host': src_ip, 'username': 'username4', 'password': 'password4', 'port' : 22, 'secret': '', } try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' return net_connect,device_type,device_prompt, error_message |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
def give_me_optical_power(src_ip,int_name): device_version_cmd_cisco = 'show version' device_version_cmd_huawei = 'display version' error_message = '' version = '' output_power = '' net_connect,device_type,device_prompt, error_message = make_connect(src_ip) #print(device_type,device_prompt,error_message) if net_connect: if device_type == 'cisco': net_connect.send_command('terminal length 0') version = find_model(net_connect,device_version_cmd_cisco) elif device_type == 'huawei': net_connect.send_command('screen-length 0 temporary') version = find_model(net_connect,device_version_cmd_huawei) #print(version) else: print('Not correct device_type. ',device_type) version = '' if 'Cisco A901-12C-F-D' in version or 'Cisco A901-12C-FT-D' in version or 'Cisco A901-6CZ-F-A' in version or 'Cisco A901-6CZ-F-D' in version or 'Cisco A901-6CZ-FT-D' in version or 'cisco ME-3400-24FS-A' in version or 'cisco ME-3400-24TS-A' in version or 'cisco ME-3800X-24FS-M' in version or 'cisco WS-C2960-24TC-L' in version or 'cisco WS-C2960-24TC-S' in version : #print('type 1') type1_power_cmd = 'sh int '+ str(int_name)+' transceiver | b Optical' type1_power_cmd_output = net_connect.send_command(type1_power_cmd) #print(type1_power_cmd_output) output_power = type1_power_cmd_output elif 'cisco ASR-902 (RSP2)' in version or 'cisco ASR-903 (RSP1)' in version or 'cisco ASR-903 (RSP2)' in version or 'cisco ASR-920-12CZ-D' in version or 'cisco ASR-920-24SZ-IM' in version or 'cisco ASR-920-24SZ-M' in version or 'cisco ASR-920-8S4Z-PD' in version: #print('type 2') pattern = r'\/' int_name_splitted = re.split(pattern, int_name) #['Gi 0', '0', '1'] pattern = r'\d+' int_name_splitted_first_digit = re.findall(pattern, int_name_splitted[0]) #['0'] type2_power_cmd = 'sh hw-module subslot ' + str(int_name_splitted_first_digit[0])+'/'+str(int_name_splitted[1]) + ' transceiver '+str(int_name_splitted[2]) + ' status | i dBm' #sh hw-module subslot 0/1 transceiver 1 status | i dBm type2_power_cmd_output = net_connect.send_command(type2_power_cmd) #print(type2_power_cmd_output) output_power = type2_power_cmd_output elif 'cisco N540X-6Z18G-SYS-D' in version or 'cisco NCS-540' in version or 'cisco NCS-5500 ' in version: #print('type 3') type3_power_cmd = 'sh controllers '+ str(int_name)+' phy | i x Power:' type3_power_cmd_output = net_connect.send_command(type3_power_cmd) #print(type3_power_cmd_output) output_power = type3_power_cmd_output elif 'HUAWEI ATN 910B-F DC' in version or 'HUAWEI NE40E-M2K' in version or 'HUAWEI NE40E-X8A' in version or 'HUAWEI NetEngine 8000 M8' in version or 'HUAWEI NE20E-S2F' in version or 'HUAWEI NE40E-X3A' in version: #print('type 4') #display interface phy-option GigabitEthernet 0/3/32 | i x Power: type4_power_cmd = 'display interface phy-option '+ str(int_name) + ' | i x Power:' type4_power_cmd_output = net_connect.send_command(type4_power_cmd) type4_power_cmd_output = str(type4_power_cmd_output).split('CTRL_C to break.')[1] #print(type4_power_cmd_output) output_power = type4_power_cmd_output elif 'HUAWEI CE6865-48S8CQ-EI' in version: #print('type 5') #display interface 100GE1/0/4 transceiver brief type5_power_cmd = 'display interface '+ str(int_name)+' transceiver brief ' #print(type5_power_cmd) type5_power_cmd_output = net_connect.send_command(type5_power_cmd) #print(type5_power_cmd_output) output_power = type5_power_cmd_output elif 'HUAWEI S2320-28TP-EI-AC Routing Switch' in version or 'HUAWEI S5300-28X-LI-24S-AC Routing Switch' in version or 'HUAWEI S5320-32X-EI-24S-AC Routing Switch' in version or 'HUAWEI S5320-32X-EI-24S-DC Routing Switch' in version or 'HUAWEI S5320-32X-EI-DC Routing Switch' in version or 'HUAWEI S5720S-28X-LI-24S-AC Routing Switch' in version or 'HUAWEI S6320-30C-EI-24S-DC Routing Switch' in version or 'HUAWEI S6320-50L-HI-48S Routing Switch' in version or 'HUAWEI S6720-30C-EI-24S-AC Routing Switch' in version or 'Quidway S2318TP-EI Routing Switch' in version or 'Quidway S3328TP-EI-24S Routing Switch' in version or 'Quidway S3328TP-SI Routing Switch' in version or 'Quidway S3700-28TP-EI-24S-AC Routing Switch' in version or 'Quidway S5328C-EI-24S Routing Switch' in version: #print('type 6') #display transceiver diagnosis interface GigabitEthernet 0/0/5 type6_power_cmd = 'display transceiver diagnosis interface '+ str(int_name)+'' #print(type6_power_cmd) type6_power_cmd_output = net_connect.send_command(type6_power_cmd) #print(type6_power_cmd_output) output_power = type6_power_cmd_output elif 'cisco ASR9K' in version: #print('type 7') type7_power_cmd = 'sh controllers '+ str(int_name)+' phy | i dBm' type7_power_cmd_output = net_connect.send_command(type7_power_cmd) #print(type7_power_cmd_output) output_power = type7_power_cmd_output elif 'HUAWEI NE05E-SE' in version: #print('type 8') type8_power_cmd = 'display interface phy-option '+ str(int_name) + ' | i l Power:' type8_power_cmd_output = net_connect.send_command(type8_power_cmd) #print(type8_power_cmd) output_power = type8_power_cmd_output else: #print('Unknown device version.') output_power = 'Unknown device version.' net_connect.disconnect() else: #print('Could not connect. Check SSH connection.') output_power = 'Could not connect. Check SSH connection.' return output_power |
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.
1 2 3 4 5 6 7 8 9 10 11 |
def validate_ip(s): a = s.split('.') if len(a) != 4: return False for x in a: if not x.isdigit(): return False i = int(x) if i < 0 or i > 255: return False return True |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
def main() -> None: TOKEN = 'YOUR_TOKEN_FOUND_FROM_BOTFATHER' bot = telebot.TeleBot(TOKEN, parse_mode=None) @bot.message_handler(func=lambda message: True) def proccess_scr(message): print(message.chat.id,type(message.chat.id) ) msg = message.text msg = "".join(str(msg).split()) msg_split = msg.split(",") if message.chat.id == YOUR_TELEGRAM_CHAT_ID: #bot.reply_to(message, "Authorised user.") if validate_ip(msg_split[0]) and ',' in msg: #print(msg_split[0],msg_split[1]) optical_power = give_me_optical_power(msg_split[0],msg_split[1]) bot.reply_to(message, optical_power) else: #print('You have entered wrong ip/port format.') bot.reply_to(message, "You have entered wrong ip/port format.") else: bot.reply_to(message, "Unauthorised user. Your info will be reported to Admin.") try: bot.infinity_polling() except Exception as e: print(e) |
If user send a valid IP with coma separated port name then it will connect the device and find optical power.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
import telebot import requests from bs4 import BeautifulSoup import re from netmiko import ConnectHandler from netmiko.ssh_autodetect import SSHDetect def find_model(net_connect,cmd): #send_command("show whatever", delay_factor=2) output = net_connect.send_command(cmd) return output def try_connect(src_ip,router): #print('Connecting with ',src_ip) net_connect = ConnectHandler(**router) device_prompt = net_connect.find_prompt() #print(device_prompt) if '#' in device_prompt: device_type = 'cisco' elif '>' in device_prompt: device_type = 'huawei' else: device_type = 'none' return net_connect,device_type,device_prompt def make_connect(src_ip): router = { 'device_type': 'autodetect', 'host': src_ip, 'username': 'username1', 'password': 'password1', 'port' : 22, 'secret': '', } device_type = '' device_prompt = '' net_connect = '' error_message = '' connect_success = 0 try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' if connect_success == 0: router = { 'device_type': 'cisco_xr', 'host': src_ip, 'username': 'username2', 'password': 'password2', 'port' : 22, 'secret': '', } try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' if connect_success == 0: router = { 'device_type': 'huawei', 'host': src_ip, 'username': 'username3', 'password': 'password3', 'port' : 22, 'secret': '', } try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' if connect_success == 0: router = { 'device_type': 'autodetect', 'host': src_ip, 'username': 'username4', 'password': 'password4', 'port' : 22, 'secret': '', } try: net_connect,device_type,device_prompt = try_connect(src_ip,router) connect_success = 1 except Exception as e: connect_success = 0 error_message = error_message + str(e) + '\n\n\n' return net_connect,device_type,device_prompt, error_message def give_me_optical_power(src_ip,int_name): device_version_cmd_cisco = 'show version' device_version_cmd_huawei = 'display version' error_message = '' version = '' output_power = '' net_connect,device_type,device_prompt, error_message = make_connect(src_ip) #print(device_type,device_prompt,error_message) if net_connect: if device_type == 'cisco': net_connect.send_command('terminal length 0') version = find_model(net_connect,device_version_cmd_cisco) elif device_type == 'huawei': net_connect.send_command('screen-length 0 temporary') version = find_model(net_connect,device_version_cmd_huawei) #print(version) else: print('Not correct device_type. ',device_type) version = '' if 'Cisco A901-12C-F-D' in version or 'Cisco A901-12C-FT-D' in version or 'Cisco A901-6CZ-F-A' in version or 'Cisco A901-6CZ-F-D' in version or 'Cisco A901-6CZ-FT-D' in version or 'cisco ME-3400-24FS-A' in version or 'cisco ME-3400-24TS-A' in version or 'cisco ME-3800X-24FS-M' in version or 'cisco WS-C2960-24TC-L' in version or 'cisco WS-C2960-24TC-S' in version : #print('type 1') type1_power_cmd = 'sh int '+ str(int_name)+' transceiver | b Optical' type1_power_cmd_output = net_connect.send_command(type1_power_cmd) #print(type1_power_cmd_output) output_power = type1_power_cmd_output elif 'cisco ASR-902 (RSP2)' in version or 'cisco ASR-903 (RSP1)' in version or 'cisco ASR-903 (RSP2)' in version or 'cisco ASR-920-12CZ-D' in version or 'cisco ASR-920-24SZ-IM' in version or 'cisco ASR-920-24SZ-M' in version or 'cisco ASR-920-8S4Z-PD' in version: #print('type 2') pattern = r'\/' int_name_splitted = re.split(pattern, int_name) #['Gi 0', '0', '1'] pattern = r'\d+' int_name_splitted_first_digit = re.findall(pattern, int_name_splitted[0]) #['0'] type2_power_cmd = 'sh hw-module subslot ' + str(int_name_splitted_first_digit[0])+'/'+str(int_name_splitted[1]) + ' transceiver '+str(int_name_splitted[2]) + ' status | i dBm' #sh hw-module subslot 0/1 transceiver 1 status | i dBm type2_power_cmd_output = net_connect.send_command(type2_power_cmd) #print(type2_power_cmd_output) output_power = type2_power_cmd_output elif 'cisco N540X-6Z18G-SYS-D' in version or 'cisco NCS-540' in version or 'cisco NCS-5500 ' in version: #print('type 3') type3_power_cmd = 'sh controllers '+ str(int_name)+' phy | i x Power:' type3_power_cmd_output = net_connect.send_command(type3_power_cmd) #print(type3_power_cmd_output) output_power = type3_power_cmd_output elif 'HUAWEI ATN 910B-F DC' in version or 'HUAWEI NE40E-M2K' in version or 'HUAWEI NE40E-X8A' in version or 'HUAWEI NetEngine 8000 M8' in version or 'HUAWEI NE20E-S2F' in version or 'HUAWEI NE40E-X3A' in version: #print('type 4') #display interface phy-option GigabitEthernet 0/3/32 | i x Power: type4_power_cmd = 'display interface phy-option '+ str(int_name) + ' | i x Power:' type4_power_cmd_output = net_connect.send_command(type4_power_cmd) type4_power_cmd_output = str(type4_power_cmd_output).split('CTRL_C to break.')[1] #print(type4_power_cmd_output) output_power = type4_power_cmd_output elif 'HUAWEI CE6865-48S8CQ-EI' in version: #print('type 5') #display interface 100GE1/0/4 transceiver brief type5_power_cmd = 'display interface '+ str(int_name)+' transceiver brief ' #print(type5_power_cmd) type5_power_cmd_output = net_connect.send_command(type5_power_cmd) #print(type5_power_cmd_output) output_power = type5_power_cmd_output elif 'HUAWEI S2320-28TP-EI-AC Routing Switch' in version or 'HUAWEI S5300-28X-LI-24S-AC Routing Switch' in version or 'HUAWEI S5320-32X-EI-24S-AC Routing Switch' in version or 'HUAWEI S5320-32X-EI-24S-DC Routing Switch' in version or 'HUAWEI S5320-32X-EI-DC Routing Switch' in version or 'HUAWEI S5720S-28X-LI-24S-AC Routing Switch' in version or 'HUAWEI S6320-30C-EI-24S-DC Routing Switch' in version or 'HUAWEI S6320-50L-HI-48S Routing Switch' in version or 'HUAWEI S6720-30C-EI-24S-AC Routing Switch' in version or 'Quidway S2318TP-EI Routing Switch' in version or 'Quidway S3328TP-EI-24S Routing Switch' in version or 'Quidway S3328TP-SI Routing Switch' in version or 'Quidway S3700-28TP-EI-24S-AC Routing Switch' in version or 'Quidway S5328C-EI-24S Routing Switch' in version: #print('type 6') #display transceiver diagnosis interface GigabitEthernet 0/0/5 type6_power_cmd = 'display transceiver diagnosis interface '+ str(int_name)+'' #print(type6_power_cmd) type6_power_cmd_output = net_connect.send_command(type6_power_cmd) #print(type6_power_cmd_output) output_power = type6_power_cmd_output elif 'cisco ASR9K' in version: #print('type 7') type7_power_cmd = 'sh controllers '+ str(int_name)+' phy | i dBm' type7_power_cmd_output = net_connect.send_command(type7_power_cmd) #print(type7_power_cmd_output) output_power = type7_power_cmd_output elif 'HUAWEI NE05E-SE' in version: #print('type 8') type8_power_cmd = 'display interface phy-option '+ str(int_name) + ' | i l Power:' type8_power_cmd_output = net_connect.send_command(type8_power_cmd) #print(type8_power_cmd) output_power = type8_power_cmd_output else: #print('Unknown device version.') output_power = 'Unknown device version.' net_connect.disconnect() else: #print('Could not connect. Check SSH connection.') output_power = 'Could not connect. Check SSH connection.' return output_power def validate_ip(s): a = s.split('.') if len(a) != 4: return False for x in a: if not x.isdigit(): return False i = int(x) if i < 0 or i > 255: return False return True def main() -> None: TOKEN = 'YOUR_TOKEN_FOUND_FROM_BOTFATHER' bot = telebot.TeleBot(TOKEN, parse_mode=None) @bot.message_handler(func=lambda message: True) def proccess_scr(message): print(message.chat.id,type(message.chat.id) ) msg = message.text msg = "".join(str(msg).split()) msg_split = msg.split(",") if message.chat.id == YOUR_TELEGRAM_CHAT_ID: #bot.reply_to(message, "Authorised user.") if validate_ip(msg_split[0]) and ',' in msg: #print(msg_split[0],msg_split[1]) optical_power = give_me_optical_power(msg_split[0],msg_split[1]) bot.reply_to(message, optical_power) else: #print('You have entered wrong ip/port format.') bot.reply_to(message, "You have entered wrong ip/port format.") else: bot.reply_to(message, "Unauthorised user. Your info will be reported to Admin.") try: bot.infinity_polling() except Exception as e: print(e) if __name__ == '__main__': main() |
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.
1 2 |
if __name__ == '__main__': main() |
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.
0 Comments