├── LICENSE ├── README.md ├── screen.JPG ├── telegram ├── allMessage.py ├── bot.py └── config.py └── test ├── allMessage.py ├── bot.py └── config.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Vladimir Z. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ssh connection in Telegram 2 | 3 | How it work (briefly): 4 | - Put folder telegram/ on the server in the local network. (Need internet access) 5 | - Сhange settings in telegram/config.py (look at the comments) 6 | - Run /telegram/bot.py 7 | 8 | # Screen: 9 | 10 | ![Image alt](https://raw.githubusercontent.com/vzemtsov/ssh-Connection-In-Telegram/master/screen.JPG) 11 | 12 | 13 | # More info: 14 | 15 | 16 | To use this bot u need: 17 | 1. Put folder telegram/ on the server in the local network. (Need internet access) 18 | 2. Create bot in telegram. Send "/newbot" to @BotFather. 19 | 3. Copy u API tokem. Send "/mybot" to @BotFather, select u bot, and select API token. 20 | (looks something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11) 21 | 4. Paste u API token in telegram/config.py 22 | 5. Change all u want in telegram/config.py 23 | 6. Import telebot library 24 | (pip install telebot) 25 | 7. Import paramico library 26 | (pip install paramiko) 27 | 8. Start telegram.bot.py 28 | -------------------------------------------------------------------------------- /screen.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zvlb/ssh-Connection-In-Telegram/d714273863aa3017002d8465fc21d18675f8339c/screen.JPG -------------------------------------------------------------------------------- /telegram/allMessage.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | commands = { 5 | 'start_AfterAuthorized': 6 | u'Welcome to remoteSsh_bot\n\n' 7 | u'If you known password - use /on\n' 8 | u'else - connect to admin', 9 | 10 | 'start_BeforeAuthorized': 11 | u'Hello!\n' 12 | u'If you want information about this bot - use /information\n' 13 | u'If you want command list - use /help', 14 | 15 | 'help_AfterAuthorized': 16 | u'If you known password - use /on\n' 17 | u'else - connect to admin', 18 | 19 | 'help_BeforeAuthorized': 20 | u'Command:\n' 21 | u'/off - disconnect from bot\n' 22 | u'/setsshuser - Set ssh user and ssh password\n' 23 | u'/setsshhost - Set host(IP) for ssh connection\n' 24 | u'/information - Show information, about ssh-connection\n' 25 | u'/aboutbot - Information about bot and author\n', 26 | 27 | 'aboutBot': 'Author GitHub - https://github.com/vzemtsov\n' 28 | u'Please write wishes to improve this bot', 29 | } 30 | -------------------------------------------------------------------------------- /telegram/bot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import config 3 | import allMessage 4 | import telebot 5 | import paramiko 6 | import os 7 | import sys 8 | import time 9 | 10 | bot = telebot.TeleBot(config.botToken) 11 | 12 | class User: 13 | def __init__(self, userChatId): 14 | self.userChatId = userChatId 15 | 16 | def userName(self, userName): 17 | self.userName = userName 18 | 19 | def sshUser(self, sshUser): 20 | self.sshUser = sshUser 21 | 22 | def sshPassword(self, sshPassword): 23 | self.sshPassword = sshPassword 24 | 25 | def sshHost(self, sshHost): 26 | self.sshHost = sshHost 27 | 28 | def cdCommand(self, cdCommand): 29 | self.cdCommand = cdCommand 30 | 31 | def userStep(self, userStep): 32 | self.userStep = userStep 33 | #(If user for chatId) = None - user not activate 34 | #step = 1 - wait everything 35 | #step = 2 - activ 36 | 37 | knownUsers = {} 38 | 39 | # 40 | # 41 | #Pass all message(exclude /start, /on, /help), if user not activate: 42 | @bot.message_handler(func=lambda message: \ 43 | ((knownUsers.get(message.chat.id) == None) or (knownUsers.get(message.chat.id) == 1)) \ 44 | and (message.text != '/start') and (message.text != '/on') \ 45 | and (message.text != '/help'), content_types=["text"]) 46 | def pass_message(message): 47 | pass 48 | 49 | # 50 | # 51 | #Message /start after and before register user 52 | @bot.message_handler(func=lambda message: \ 53 | knownUsers.get(message.chat.id) == None, commands=['start']) 54 | def send_welcome(message): 55 | bot.send_message(message.chat.id, allMessage.commands['start_AfterAuthorized']) 56 | 57 | @bot.message_handler(func=lambda message: \ 58 | knownUsers.get(message.chat.id).userStep == 2, commands=['start']) 59 | def send_welcome(message): 60 | bot.send_message(message.chat.id, allMessage.commands['start_BeforeAuthorized']) 61 | # 62 | # 63 | #Message /help after and before register user 64 | @bot.message_handler(func=lambda message: \ 65 | knownUsers.get(message.chat.id) == None, commands=['help']) 66 | def send_welcome(message): 67 | bot.send_message(message.chat.id, allMessage.commands['help_AfterAuthorized']) 68 | 69 | @bot.message_handler(func=lambda message: \ 70 | knownUsers.get(message.chat.id).userStep == 2, commands=['help']) 71 | def send_welcome(message): 72 | bot.send_message(message.chat.id, allMessage.commands['help_BeforeAuthorized']) 73 | 74 | # 75 | # 76 | #Message /aboutBot 77 | @bot.message_handler(func=lambda message: \ 78 | knownUsers.get(message.chat.id).userStep == 2, commands=['aboutbot']) 79 | def send_aboutBot(message): 80 | bot.send_message(message.chat.id, allMessage.commands['aboutbot']) 81 | 82 | # 83 | # 84 | #User information: 85 | @bot.message_handler(func=lambda message: True, commands=['information']) 86 | def test_user(message): 87 | data = 'UserName: ' + knownUsers.get(message.chat.id).userName + '\n' +\ 88 | 'Ssh user: ' + knownUsers.get(message.chat.id).sshUser + '\n' +\ 89 | 'host(IP) for ssh connection: ' + knownUsers.get(message.chat.id).sshHost + '\n' +\ 90 | 'Your location on the server (pwd): ' + knownUsers.get(message.chat.id).cdCommand 91 | bot.send_message(message.chat.id, data) 92 | 93 | # 94 | # 95 | #Activate, and deactivation bot. 96 | @bot.message_handler(func=lambda message: True, commands=['on']) 97 | def activate_user(message): 98 | if knownUsers.get(message.chat.id) == None: 99 | newUser = User(message.chat.id) 100 | newUser.userName = '' 101 | newUser.sshUser = config.sshUser 102 | newUser.sshPassword = config.sshPassword 103 | newUser.sshHost = config.sshHost 104 | newUser.cdCommand = config.sshHomeDirectory 105 | newUser.userStep = 1 106 | knownUsers[message.chat.id] = newUser 107 | 108 | password = bot.send_message(message.chat.id, "Enter the bot password:") 109 | bot.register_next_step_handler(password, check_password) 110 | elif knownUsers.get(message.chat.id).userStep == 2: 111 | bot.send_message(message.chat.id, "You are already authorized on this bot") 112 | 113 | def check_password(message): 114 | if message.text == config.botPassword: 115 | knownUsers.get(message.chat.id).userStep = 2 116 | knownUsers.get(message.chat.id).userName = message.chat.first_name 117 | 118 | bot.send_message(message.chat.id, "Ok password. Welcome.") 119 | else: 120 | password = bot.send_message(message.chat.id, "Wrong password. Try again:") 121 | bot.register_next_step_handler(password, check_password) 122 | 123 | 124 | @bot.message_handler(func=lambda message: \ 125 | knownUsers.get(message.chat.id).userStep == 2, commands=['off']) 126 | def deactivate_user(message): 127 | knownUsers.pop(message.chat.id, None) 128 | bot.send_message(message.chat.id, "Bye") 129 | 130 | # 131 | # 132 | #Set ssh user and ssh password 133 | @bot.message_handler(func=lambda message: \ 134 | knownUsers.get(message.chat.id).userStep == 2, commands=['setsshuser']) 135 | def request_ssh_user(message): 136 | knownUsers.get(message.chat.id).userStep = 1 137 | sshUser = bot.send_message(message.chat.id, "Enter the ssh user:") 138 | bot.register_next_step_handler(sshUser, add_ssh_user) 139 | 140 | def add_ssh_user(message): 141 | if ((config.rootPermission) or (message.text != 'root')): 142 | knownUsers.get(message.chat.id).sshUser = message.text 143 | sshPassword = bot.send_message(message.chat.id, "Enter the ssh password:") 144 | bot.register_next_step_handler(sshPassword, add_ssh_password) 145 | else: 146 | bot.send_message(message.chat.id, "Root not allowed") 147 | 148 | def add_ssh_password(message): 149 | knownUsers.get(message.chat.id).userStep = 2 150 | knownUsers.get(message.chat.id).sshPassword = message.text 151 | 152 | 153 | # 154 | # 155 | #Set host(IP) for ssh connection: 156 | @bot.message_handler(func=lambda message: \ 157 | knownUsers.get(message.chat.id).userStep == 2, commands=['setsshhost']) 158 | def request_ssh_host(message): 159 | knownUsers.get(message.chat.id).userStep = 1 160 | sshDomain = bot.send_message(message.chat.id, "Enter domain(ip) for ssh connection:") 161 | bot.register_next_step_handler(sshDomain, add_ssh_host) 162 | 163 | def add_ssh_host(message): 164 | knownUsers.get(message.chat.id).userStep = 2 165 | knownUsers.get(message.chat.id).sshHost = message.text 166 | 167 | # 168 | # 169 | #Check exist sshUser: 170 | @bot.message_handler(func=lambda message: \ 171 | ((knownUsers.get(message.chat.id).userStep == 2) and \ 172 | (knownUsers.get(message.chat.id).sshUser == '') and \ 173 | (knownUsers.get(message.chat.id).sshPassword == '')), \ 174 | content_types=["text"]) 175 | def ssh_user_not_exist(message): 176 | bot.send_message(message.chat.id, "Ssh user not exist. Use /setsshuser or /help") 177 | 178 | # 179 | # 180 | #Check exist sshHost: 181 | @bot.message_handler(func=lambda message: \ 182 | ((knownUsers.get(message.chat.id).userStep == 2) and \ 183 | (knownUsers.get(message.chat.id).sshHost == '')), \ 184 | content_types=["text"]) 185 | def ssh_user_not_exist(message): 186 | bot.send_message(message.chat.id, "Ssh host not exist. Use /setsshhost or /help") 187 | 188 | # 189 | # 190 | #Do ssh command: 191 | @bot.message_handler(func=lambda message: \ 192 | knownUsers.get(message.chat.id).userStep == 2, content_types=["text"]) 193 | def do_ssh_command(message): 194 | #check and remember 'cd'-command 195 | if (message.text[0:3] == 'cd '): 196 | if knownUsers.get(message.chat.id).cdCommand[-1] != '/': 197 | knownUsers.get(message.chat.id).cdCommand += '/' 198 | if message.text[3] == '/': 199 | knownUsers.get(message.chat.id).cdCommand = message.text.split()[1] 200 | elif message.text[3:5] == '..': 201 | flag = knownUsers.get(message.chat.id).cdCommand.split('/') 202 | knownUsers.get(message.chat.id).cdCommand = '/' 203 | i = 0 204 | for element in flag: 205 | if ((i == 0) or (i == len(flag)-2) or (i == len(flag)-1)): 206 | i += 1 207 | continue 208 | knownUsers.get(message.chat.id).cdCommand += element + '/' 209 | i += 1 210 | else: 211 | bot.send_message(message.chat.id, message.text[3:5]) 212 | if knownUsers.get(message.chat.id).cdCommand[-1] =='/': 213 | knownUsers.get(message.chat.id).cdCommand += message.text.split()[1] 214 | else: 215 | knownUsers.get(message.chat.id).cdCommand += '/' + message.text.split()[1] 216 | #ban 'vi', 'less', 'more', 'tail -f': 217 | elif ((message.text[0:3] == 'vi ') or (message.text[0:5] == 'less ') or \ 218 | (message.text[0:5] == 'more ') or (message.text[0:8] == 'tail -f ')): 219 | bot.send_message(message.chat.id, "Please don't use dynamic command") 220 | #Do ssh command: 221 | else: 222 | try: 223 | client = paramiko.SSHClient() 224 | client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 225 | client.connect(hostname=knownUsers.get(message.chat.id).sshHost, \ 226 | username=knownUsers.get(message.chat.id).sshUser, 227 | password=knownUsers.get(message.chat.id).sshPassword) 228 | sshCommand = 'cd ' +knownUsers.get(message.chat.id).cdCommand + '; ' + \ 229 | message.text 230 | stdin, stdout, stderr = client.exec_command(sshCommand) 231 | data = stdout.read() + stderr.read() 232 | if (sys.getsizeof(data) < 34): 233 | pass 234 | elif (sys.getsizeof(data) > 3000): 235 | bot.send_message(message.chat.id, \ 236 | "Answer from server is too large") 237 | else: 238 | bot.send_message(message.chat.id, data) 239 | client.close() 240 | except Exception as e: 241 | bot.send_message(message.chat.id, e) 242 | 243 | #log user actions: 244 | if config.logging: 245 | logFile = open(config.logFile, 'a') 246 | logFile.write("User chat id: '" + str(knownUsers.get(message.chat.id).userChatId) + \ 247 | "'; User name: '" + str(knownUsers.get(message.chat.id).userName) + \ 248 | "'; Command: '" + str(sshCommand) + "'\n") 249 | logFile.close() 250 | 251 | 252 | 253 | while True : 254 | if __name__ == '__main__': 255 | bot.polling(none_stop=True) 256 | time.sleep(10) 257 | -------------------------------------------------------------------------------- /telegram/config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | #Telegram Bot API token (ask BotFather): 4 | botToken = '' 5 | 6 | #Password for authentication in bot: 7 | botPassword = 'password' 8 | 9 | #Default data for ssh-connection: 10 | sshUser = '' 11 | sshPassword = '' 12 | sshHost = '' 13 | sshHomeDirectory = '/' 14 | 15 | #Root permission. If 'True" - root allowed 16 | rootPermission = False 17 | 18 | #Logging. If 'True" - logging ON 19 | logging = True 20 | logFile = '/var/log/telegramBot/telegramBot.log' -------------------------------------------------------------------------------- /test/allMessage.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | commands = { 5 | 'start_AfterAuthorized': 6 | u'Welcome to remoteSsh_bot\n\n' 7 | u'If you known password - use /on\n' 8 | u'else - connect to admin', 9 | 10 | 'start_BeforeAuthorized': 11 | u'Hello!\n' 12 | u'If you want information about this bot - use /information\n' 13 | u'If you want command list - use /help', 14 | 15 | 'help_AfterAuthorized': 16 | u'If you known password - use /on\n' 17 | u'else - connect to admin', 18 | 19 | 'help_BeforeAuthorized': 20 | u'Command:\n' 21 | u'/off - disconnect from bot\n' 22 | u'/setsshUser - Set ssh user and ssh password\n' 23 | u'/setsshHost - Set host(IP) for ssh connection\n' 24 | u'/information - Show information, about ssh-connection\n' 25 | u'/aboutBot - Information about bot and author\n', 26 | 27 | 'aboutBot': 'Author GitHub - https://github.com/vzemtsov\n' 28 | u'Please write wishes to improve this bot', 29 | } -------------------------------------------------------------------------------- /test/bot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import config 3 | import allMessage 4 | import telebot 5 | import paramiko 6 | import os 7 | import sys 8 | 9 | bot = telebot.TeleBot(config.botToken) 10 | 11 | class User: 12 | def __init__(self, userChatId): 13 | self.userChatId = userChatId 14 | 15 | def userName(self, userName): 16 | self.userName = userName 17 | 18 | def sshUser(self, sshUser): 19 | self.sshUser = sshUser 20 | 21 | def sshPassword(self, sshPassword): 22 | self.sshPassword = sshPassword 23 | 24 | def sshHost(self, sshHost): 25 | self.sshHost = sshHost 26 | 27 | def cdCommand(self, cdCommand): 28 | self.cdCommand = cdCommand 29 | 30 | def userStep(self, userStep): 31 | self.userStep = userStep 32 | #(If user for chatId) = None - user not activate 33 | #step = 1 - wait everything 34 | #step = 2 - activ 35 | 36 | knownUsers = {} 37 | 38 | # 39 | # 40 | #Pass all message(exclude /start, /on, /help), if user not activate: 41 | @bot.message_handler(func=lambda message: \ 42 | ((knownUsers.get(message.chat.id) == None) or (knownUsers.get(message.chat.id) == 1)) \ 43 | and (message.text != '/start') and (message.text != '/on') \ 44 | and (message.text != '/help'), content_types=["text"]) 45 | def pass_message(message): 46 | pass 47 | 48 | # 49 | # 50 | #Message /start after and before register user 51 | @bot.message_handler(func=lambda message: \ 52 | knownUsers.get(message.chat.id) == None, commands=['start']) 53 | def send_welcome(message): 54 | bot.send_message(message.chat.id, allMessage.commands['start_AfterAuthorized']) 55 | 56 | @bot.message_handler(func=lambda message: \ 57 | knownUsers.get(message.chat.id).userStep == 2, commands=['start']) 58 | def send_welcome(message): 59 | bot.send_message(message.chat.id, allMessage.commands['start_BeforeAuthorized']) 60 | # 61 | # 62 | #Message /help after and before register user 63 | @bot.message_handler(func=lambda message: \ 64 | knownUsers.get(message.chat.id) == None, commands=['help']) 65 | def send_welcome(message): 66 | bot.send_message(message.chat.id, allMessage.commands['help_AfterAuthorized']) 67 | 68 | @bot.message_handler(func=lambda message: \ 69 | knownUsers.get(message.chat.id).userStep == 2, commands=['help']) 70 | def send_welcome(message): 71 | bot.send_message(message.chat.id, allMessage.commands['help_BeforeAuthorized']) 72 | 73 | # 74 | # 75 | #Message /aboutBot 76 | @bot.message_handler(func=lambda message: \ 77 | knownUsers.get(message.chat.id).userStep == 2, commands=['aboutBot']) 78 | def send_aboutBot(message): 79 | bot.send_message(message.chat.id, allMessage.commands['aboutBot']) 80 | 81 | # 82 | # 83 | #User information: 84 | @bot.message_handler(func=lambda message: True, commands=['information']) 85 | def test_user(message): 86 | data = 'UserName: ' + knownUsers.get(message.chat.id).userName + '\n' +\ 87 | 'Ssh user: ' + knownUsers.get(message.chat.id).sshUser + '\n' +\ 88 | 'host(IP) for ssh connection: ' + knownUsers.get(message.chat.id).sshHost + '\n' +\ 89 | 'Your location on the server (pwd): ' + knownUsers.get(message.chat.id).cdCommand 90 | bot.send_message(message.chat.id, data) 91 | 92 | # 93 | # 94 | #Activate, and deactivation bot. 95 | @bot.message_handler(func=lambda message: True, commands=['on']) 96 | def activate_user(message): 97 | if knownUsers.get(message.chat.id) == None: 98 | newUser = User(message.chat.id) 99 | newUser.userName = '' 100 | newUser.sshUser = config.sshUser 101 | newUser.sshPassword = config.sshPassword 102 | newUser.sshHost = config.sshHost 103 | newUser.cdCommand = config.sshHomeDirectory 104 | newUser.userStep = 1 105 | knownUsers[message.chat.id] = newUser 106 | 107 | password = bot.send_message(message.chat.id, "Enter the bot password:") 108 | bot.register_next_step_handler(password, check_password) 109 | elif knownUsers.get(message.chat.id).userStep == 2: 110 | bot.send_message(message.chat.id, "You are already authorized on this bot") 111 | 112 | def check_password(message): 113 | if message.text == config.botPassword: 114 | knownUsers.get(message.chat.id).userStep = 2 115 | knownUsers.get(message.chat.id).userName = message.chat.first_name 116 | 117 | bot.send_message(message.chat.id, "Ok password. Welcome.") 118 | else: 119 | password = bot.send_message(message.chat.id, "Wrong password. Try again:") 120 | bot.register_next_step_handler(password, check_password) 121 | 122 | 123 | @bot.message_handler(func=lambda message: \ 124 | knownUsers.get(message.chat.id).userStep == 2, commands=['off']) 125 | def deactivate_user(message): 126 | knownUsers.pop(message.chat.id, None) 127 | bot.send_message(message.chat.id, "Bye") 128 | 129 | # 130 | # 131 | #Set ssh user and ssh password 132 | @bot.message_handler(func=lambda message: \ 133 | knownUsers.get(message.chat.id).userStep == 2, commands=['setsshUser']) 134 | def request_ssh_user(message): 135 | knownUsers.get(message.chat.id).userStep = 1 136 | sshUser = bot.send_message(message.chat.id, "Enter the ssh user:") 137 | bot.register_next_step_handler(sshUser, add_ssh_user) 138 | 139 | def add_ssh_user(message): 140 | if ((config.rootPermission) or (message.text != 'root')): 141 | knownUsers.get(message.chat.id).sshUser = message.text 142 | sshPassword = bot.send_message(message.chat.id, "Enter the ssh password:") 143 | bot.register_next_step_handler(sshPassword, add_ssh_password) 144 | else: 145 | bot.send_message(message.chat.id, "Root not allowed") 146 | 147 | def add_ssh_password(message): 148 | knownUsers.get(message.chat.id).userStep = 2 149 | knownUsers.get(message.chat.id).sshPassword = message.text 150 | 151 | 152 | # 153 | # 154 | #Set host(IP) for ssh connection: 155 | @bot.message_handler(func=lambda message: \ 156 | knownUsers.get(message.chat.id).userStep == 2, commands=['setsshHost']) 157 | def request_ssh_host(message): 158 | knownUsers.get(message.chat.id).userStep = 1 159 | sshDomain = bot.send_message(message.chat.id, "Enter domain(ip) for ssh connection:") 160 | bot.register_next_step_handler(sshDomain, add_ssh_host) 161 | 162 | def add_ssh_host(message): 163 | knownUsers.get(message.chat.id).userStep = 2 164 | knownUsers.get(message.chat.id).sshHost = message.text 165 | 166 | # 167 | # 168 | #Check exist sshUser: 169 | @bot.message_handler(func=lambda message: \ 170 | ((knownUsers.get(message.chat.id).userStep == 2) and \ 171 | (knownUsers.get(message.chat.id).sshUser == '') and \ 172 | (knownUsers.get(message.chat.id).sshPassword == '')), \ 173 | content_types=["text"]) 174 | def ssh_user_not_exist(message): 175 | bot.send_message(message.chat.id, "Ssh user not exist. Use /setsshUser or /help") 176 | 177 | # 178 | # 179 | #Check exist sshHost: 180 | @bot.message_handler(func=lambda message: \ 181 | ((knownUsers.get(message.chat.id).userStep == 2) and \ 182 | (knownUsers.get(message.chat.id).sshHost == '')), \ 183 | content_types=["text"]) 184 | def ssh_user_not_exist(message): 185 | bot.send_message(message.chat.id, "Ssh host not exist. Use /setsshHost or /help") 186 | 187 | # 188 | # 189 | #Do ssh command: 190 | @bot.message_handler(func=lambda message: \ 191 | knownUsers.get(message.chat.id).userStep == 2, content_types=["text"]) 192 | def do_ssh_command(message): 193 | #check and remember 'cd'-command 194 | if (message.text[0:3] == 'cd '): 195 | if knownUsers.get(message.chat.id).cdCommand[-1] != '/': 196 | knownUsers.get(message.chat.id).cdCommand += '/' 197 | if message.text[3] == '/': 198 | knownUsers.get(message.chat.id).cdCommand = message.text.split()[1] 199 | elif message.text[3:5] == '..': 200 | flag = knownUsers.get(message.chat.id).cdCommand.split('/') 201 | knownUsers.get(message.chat.id).cdCommand = '/' 202 | i = 0 203 | for element in flag: 204 | if ((i == 0) or (i == len(flag)-2) or (i == len(flag)-1)): 205 | i += 1 206 | continue 207 | knownUsers.get(message.chat.id).cdCommand += element + '/' 208 | i += 1 209 | else: 210 | bot.send_message(message.chat.id, message.text[3:5]) 211 | if knownUsers.get(message.chat.id).cdCommand[-1] =='/': 212 | knownUsers.get(message.chat.id).cdCommand += message.text.split()[1] 213 | else: 214 | knownUsers.get(message.chat.id).cdCommand += '/' + message.text.split()[1] 215 | #ban 'vi', 'less', 'more', 'tail -f': 216 | elif ((message.text[0:3] == 'vi ') or (message.text[0:5] == 'less ') or \ 217 | (message.text[0:5] == 'more ') or (message.text[0:8] == 'tail -f ')): 218 | bot.send_message(message.chat.id, "Please don't use dynamic command") 219 | #Do ssh command: 220 | else: 221 | try: 222 | client = paramiko.SSHClient() 223 | client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 224 | client.connect(hostname=knownUsers.get(message.chat.id).sshHost, \ 225 | username=knownUsers.get(message.chat.id).sshUser, 226 | password=knownUsers.get(message.chat.id).sshPassword) 227 | sshCommand = 'cd ' +knownUsers.get(message.chat.id).cdCommand + '; ' + \ 228 | message.text 229 | stdin, stdout, stderr = client.exec_command(sshCommand) 230 | data = stdout.read() + stderr.read() 231 | if (sys.getsizeof(data) < 34): 232 | pass 233 | elif (sys.getsizeof(data) > 3000): 234 | bot.send_message(message.chat.id, \ 235 | "Answer from server is too large") 236 | else: 237 | bot.send_message(message.chat.id, data) 238 | client.close() 239 | except Exception as e: 240 | bot.send_message(message.chat.id, e) 241 | 242 | #log user actions: 243 | if config.logging: 244 | logFile = open(config.logFile, 'a') 245 | logFile.write("User chat id: '" + str(knownUsers.get(message.chat.id).userChatId) + \ 246 | "'; User name: '" + str(knownUsers.get(message.chat.id).userName) + \ 247 | "'; Command: '" + str(sshCommand) + "'\n") 248 | logFile.close() 249 | 250 | 251 | 252 | 253 | if __name__ == '__main__': 254 | bot.polling(none_stop=True) -------------------------------------------------------------------------------- /test/config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | #Telegram Bot API token (ask BotFather): 4 | botToken = '' 5 | 6 | #Password for authentication in bot: 7 | botPassword = 'password' 8 | 9 | #Default data for ssh-connection: 10 | sshUser = '' 11 | sshPassword = '' 12 | sshHost = '' 13 | sshHomeDirectory = '/' 14 | 15 | #Root permission. If 'True" - root allowed 16 | rootPermission = False 17 | 18 | #Logging. If 'True" - logging ON 19 | logging = True 20 | logFile = '/var/log/telegramBot/telegramBot.log' --------------------------------------------------------------------------------