使用Python将命令发送到远程机器应用程序 [英] Send commands to a remote machine application with Python

查看:252
本文介绍了使用Python将命令发送到远程机器应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to do the following in Python. Connect to a remote machine open an application there and then send some commands to this application. My idea was to do it through telnetlib and subprocess. I managed to connect to the machine and start the application (with telnetlib alone), but I am not sure how to continue. Is it possible?

P.S.我也乐于接受以另一种方式进行操作的想法,但我更希望使用python进行操作.

P.S. I am also open to ideas to do it another way, but I would prefer to do it with python.

提前谢谢!

推荐答案

您可以执行以下操作:

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

you can install pexpect using pip

您还可以列出期望:

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

这篇关于使用Python将命令发送到远程机器应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆