telnetlib write() 某些命令在 Python 中不起作用
telnetlib write() some command not working in Python
我使用 Python 的 telnetlib.write() 函数已经有一段时间没有出现问题,但在这种情况下,我要发送的命令没有任何作用。虽然通过 SecureCRT 发送的相同命令工作正常。
以下是我的大致情况:
import telnetlib
tn = telnetlib.Telnet(HOST)
tn.read_until('login: ')
tn.write(user + '\n')
if pswrd:
tn.read_until('Password: ')
tn.write(pswrd + '\n')
tn.write('echo "" > /path/to/file.log' + '\n')
tn.write('exit\n')
tn.close()
这应该做的是清除file.log的内容,但文件保持不变。为什么会这样?
如果值得一提,telnet 服务器是 运行 SunOS。谢谢!
你的问题几乎可以肯定是时间错误。
无法对其进行调试,也无法查看调试结果,因此很难确切地确定问题出在哪里,但我的猜测是这样的:
SunOS 机器的 telnetd
服务器有一个相对较小的缓冲区,比如说 16 字节。因此,如果您在 shell 开始之前向它发送整个命令,它只会将其中的 16 个字节发送到 shell。当然,这不会做你想做的。但是如果你等到 shell 开始,它会得到整个命令,因此工作。
通过将 tn_setdebuglevel(1)
添加到您的程序中,您正在改变时间——打印调试信息所花费的额外时间刚好(通常)足以让 shell 时间在你填满缓冲区之前开始,所以它恰好可以工作。
密码提示可能有同样的问题,你用`tn.read_until('Password: ')解决了。你只需要在这里做同样的事情。例如(取决于预期的提示是什么):
# ...
tn.read_until('$')
tn.write('echo "" > /path/to/file.log\n')
tn.read_until('$')
tn.write('exit\n')
tn.close()
我使用 Python 的 telnetlib.write() 函数已经有一段时间没有出现问题,但在这种情况下,我要发送的命令没有任何作用。虽然通过 SecureCRT 发送的相同命令工作正常。
以下是我的大致情况:
import telnetlib
tn = telnetlib.Telnet(HOST)
tn.read_until('login: ')
tn.write(user + '\n')
if pswrd:
tn.read_until('Password: ')
tn.write(pswrd + '\n')
tn.write('echo "" > /path/to/file.log' + '\n')
tn.write('exit\n')
tn.close()
这应该做的是清除file.log的内容,但文件保持不变。为什么会这样? 如果值得一提,telnet 服务器是 运行 SunOS。谢谢!
你的问题几乎可以肯定是时间错误。
无法对其进行调试,也无法查看调试结果,因此很难确切地确定问题出在哪里,但我的猜测是这样的:
SunOS 机器的 telnetd
服务器有一个相对较小的缓冲区,比如说 16 字节。因此,如果您在 shell 开始之前向它发送整个命令,它只会将其中的 16 个字节发送到 shell。当然,这不会做你想做的。但是如果你等到 shell 开始,它会得到整个命令,因此工作。
通过将 tn_setdebuglevel(1)
添加到您的程序中,您正在改变时间——打印调试信息所花费的额外时间刚好(通常)足以让 shell 时间在你填满缓冲区之前开始,所以它恰好可以工作。
密码提示可能有同样的问题,你用`tn.read_until('Password: ')解决了。你只需要在这里做同样的事情。例如(取决于预期的提示是什么):
# ...
tn.read_until('$')
tn.write('echo "" > /path/to/file.log\n')
tn.read_until('$')
tn.write('exit\n')
tn.close()