【问题标题】:How to store a Telnet output into a variable in python如何将 Telnet 输出存储到 python 中的变量中
【发布时间】:2021-12-11 05:47:16
【问题描述】:

我正在尝试使用 telnet 运行命令并从文本输出中获取值并将其存储在 python 中的变量中。在图像中,我试图获取的值是外部警报触点 2:模拟输入值。在这种情况下,我想将 5.99 存储在一个变量中。
我需要将 5.99 值或它可能更改的任何值存储在变量中:

【问题讨论】:

  • Python 提供了“telnetlib”模块来访问远程登录服务器。因此,您可以阅读数据并找到具体信息。如果您在自己解决此问题时遇到特定问题,可以在此处使用您的代码询问。

标签: python telnet


【解决方案1】:

如果您在 Python 中使用 telnetlib library,则可以使用 read_until() 并指定它应该读取的字符串,或使用 read_all() 读取输出直到 EOF 并从字符串中获取输出。

import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()

https://docs.python.org/2/library/telnetlib.html#telnet-objects

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2018-01-27
    • 2023-02-13
    相关资源
    最近更新 更多