如何在python中将Telnet输出存储到变量中

2024-06-17 08:07:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

I need to store the 5.99 value or whatever it may change to in a variable


1条回答
网友
1楼 · 发布于 2024-06-17 08:07:27

如果您使用的是Python的^{} 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

相关问题 更多 >