gpt4 book ai didi

python - Telnetlib read_until() 和 expect() 未检测到终端回显

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:07 25 4
gpt4 key购买 nike

我用 python 编写了一个脚本,它使用 telnetlib 模块远程登录到 linux 设备,运行 install.sh 文件,然后在完成后重新启动。在完成 install.sh 脚本后,我添加了一行,用于向终端发送 echo“install complete”。不幸的是,read_until("install complete") 和 expect(["install complete"]) 没有检测到这个回显。

tn = telnetlib.Telnet(ip)
tn.read_until("login: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("cd [file path to the install script]")#echoes "install complete" when finished
tn.write("./install.sh\n")
tn.read_until("install complete") #where the process hangs returns None if I add timeout parameter

telnetlib 是否无法到达 echo 语句,或者我应该使用其他模块或语言吗?如果我手动运行安装程序,我可以按预期确认“安装完成”回声。

最佳答案

所示代码在 cd 命令后不包含换行符 ("\n"),因此实际发送的数据可能是 "cd somepath./install.sh\n"而不会工作...

此外,我会避免在此处使用术语“echo”,因为在 telnet 中,“echo”实际上是将您发送的字符发送回给您的部分(用于远程回显)。在这种情况下,无论发生与否,read_until() 都不会关心。简而言之,这根本不是回声。

关于python - Telnetlib read_until() 和 expect() 未检测到终端回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com