首页 > 解决方案 > 对 paramiko ssh 上的命令是否有可能回答是/否?

问题描述

我想使用 SSH 自动执行命令,但是当它运行时会出现一条消息,输入是或否。它可能在一行命令中或在 paramiko 中以某种方式回答此消息。

我已经尝试了很多类似的东西。

 sudo /usr/sbin/service-maintenance-window ; yes

 sudo /usr/sbin/service-maintenance-window | yes

 yes | sudo /usr/sbin/service-maintenance-window 

 yes | sudo /usr/sbin/service-maintenance-window

这是代码:

from paramiko import SSHClient
import paramiko     

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect("xxxx", username="xxxx", password="xxxx",timeout=5)
stdin,stdout,stderr = ssh.exec_command("yes | sudo /usr/sbin/service-maintenance-window")

print(stdout.read())

我想接受的信息是:

------------------------------ WARNING ---------------------------------------

You are about to be granted temporary privileged shell access of VPLEX
to perform advanced maintenance or servicing tasks.

As this shell has higher privileges, non-expert usage of this shell can
result in damage to your VPLEX as well as compromising of various
security features.

Respond Yes only if you are authorized and qualified to perform advanced
VPlex maintenance and/or servicing.

Make sure to:

1 - limit your use of this shell to only those items
that require this level of elevated privilege.    
2 - exit this elevated privilege shell when you have finished using it.

-------------------------------------------------------------------------------

Are you sure you want to continue ? [Yes/No]:

标签: pythonsshparamiko

解决方案


推荐阅读