Answer the question
In order to leave comments, you need to log in
How to implement a variable name in the script name?
Hello everyone, I wrote a simple bot to manage local computers, the question arose that different scripts are used for this with minor changes in accordance with who writes the command to the bot.
bash scripts are generated with the name $user-status.sh
Is it possible somehow, in order not to clutter up the bot code itself with the #statusIP block, just use a variable name in this script, depending on who writes?
class SSHWrapper(object):
def __init__(self, user='user', psw='pass1234', port=22):
self.user = user
self.psw = psw
self.port = port
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#statusIP
def get_status(self, host, user=None, psw=None, port=None):
self.ssh.connect(
hostname=host,
port=port or self.port,
username=user or self.user,
password=psw or self.psw
)
# Выполнение команды
stdin, stdout, stderr = self.ssh.exec_command("$user-status.sh")
# Читаем результат:
result = stdout.read() + stderr.read()
self.ssh.close()
return result
ssh = SSHWrapper()
def listener(messages):
#STATUSIP
@bot.message_handler(commands=['status'])
def handle_status_request(message):
# получение из чата IP сервака
ip = message.text.split()[-1]
thrue_ip = re.findall(r'[1][9][2].[1][6][8].\d{1,3}\.\d{1,3}', ip)
if ip in thrue_ip:
# проверка его на доступность
r = pyping.ping(ip)
if r.ret_code == 0:
result = ssh.get_status(ip)
bot.send_message(message.chat.id, result)
else:
result = ("Комп недоступен.")
bot.send_message(message.chat.id, result)
else:
bot.send_message(message.chat.id, "*Команда НЕ распознана.*\nВероятнее всего НЕ указан (указан неправильно) IP адрес нужного тебе компа.", parse_mode="Markdown")
bot.set_update_listener(listener)
while True:
try:
bot.polling(none_stop=True)
except Exception as err:
logging.error(err)
time.sleep(10)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question