python - 试图整合 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 *
但也没有用
解决方案
我在您的代码中看到了一些问题。首先,我看不到message
在任何地方被定义。第二,你使用bot.message_handler
装饰器的方式。
装饰器函数返回一个函数对象,它将替换您装饰的函数。见代码:
@bot.message_handler(commands=['tx'])
def tx(message):
command.tx(message)
但是,我不知道远程机器人库。我不确定这应该如何工作。
推荐阅读
- elasticsearch - 如何在完整的字典中搜索
- python - 如何从python中给定的字符串选择中随机打印一个字符串
- c# - 现有 ADO.NET 代码和 EF 的 C# 数据访问层
- ubuntu - 如何在最新的 ubuntu 上安装 gcc-3.4 和 g++-3.4?
- javascript - 输入字段必须包含允许表单提交的特定字符
- sql - PostgreSQL - 列不存在错误:列“ins_file_url”不存在
- javascript - 以编程方式反应更新参考
- excel - 如何根据单元格值选择一系列列?
- excel - 关于表单编辑功能的 VBA 建议
- terminal - 执行命令时出错 ($ laravel new my_website)