【问题标题】:Send commands to a remote machine application with Python使用 Python 向远程机器应用程序发送命令
【发布时间】:2013-08-16 12:59:21
【问题描述】:

我想在 Python 中执行以下操作。 连接到远程机器在那里打开一个应用程序,然后向这个应用程序发送一些命令。 我的想法是通过 telnetlib 和子进程来完成。 我设法连接到机器并启动应用程序(仅使用 telnetlib),但我不确定如何继续。 有可能吗?

附:我也愿意用另一种方式来做这件事,但我更喜欢用 python 来做。

提前致谢!

【问题讨论】:

  • 你需要使用pexpect。

标签: python subprocess remote-access telnetlib


【解决方案1】:

您可以执行以下操作:

child = pexpect.spawn('telnet 192.168.0.1')
child.expect('[Ll]ogin') #you use the expected output, here will match either Login or login
child.sendline('username')
child.expect('[Pp]assword')
child.sendline('password')
child.expect('your remote prompt')
child.sendline('command')

你可以使用 pip 安装 pexpect

你也可以有一个expect的列表:

index = child.expect['[Ll]ogin', '[Pp]assword']
if index == 0:
    child.sendline('username')
else index == 1:
    child.sendline('password')

【讨论】:

  • 好主意,谢谢!我会试试看。附言是的,我打算使用 telnet。
猜你喜欢
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
相关资源
最近更新 更多