首页 > 解决方案 > 试图整合 paramiko 和 pyTelegramBotAPI

问题描述

我是使用 python 编程的新手。我正在尝试创建一个连接到不同 Linux 服务器以提取数据或检查某些服务是否正在运行的 python 脚本。我能够发送命令-> 使用 paramiko 提取数据-> 将数据发送到电报。问题是我试图让代码更短,只为函数创建一个文件并调用它,但我无法让它工作。这是文件(没有可执行文件)和代码:

文件:

def tx(message):

    host = "111.222.333.444"

    user = "user"

    password = "12345"

    ssh = paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh.connect(hostname=host, username=user, password=password)

    stdin, stdout, stderr = ssh.exec_command("sudo tail -1 /usr/local/bin/noc/respaldos/diatx.txt")

    time.sleep(.5)

    output = stdout.readlines()

    ssh.close()

    return output

脚本:

    import telebot  
    import paramiko
    import time
    import commands


    TOKEN = "abcde"

    bot = telebot.TeleBot(TOKEN)

    @bot.message_handler(commands=['tx'])

    commands.tx(message)

    bot.send_message(message.chat.id, output)

    bot.polling()

我的目的是为不同的数据和检查创建 20 个函数,但都在 commands.py 文件中的函数中。我试过了,from commands import *但也没有用

标签: python

解决方案


我在您的代码中看到了一些问题。首先,我看不到message在任何地方被定义。第二,你使用bot.message_handler装饰器的方式。

装饰器函数返回一个函数对象,它将替换您装饰的函数。见代码:

@bot.message_handler(commands=['tx'])
def tx(message):
   command.tx(message)

但是,我不知道远程机器人库。我不确定这应该如何工作。


推荐阅读