[英]Get command prompt via "sudo su -" then run command using Paramiko exec_command
你能帮我跟进吗? 我需要以用户身份登录然后sudo su -
然后运行命令 ex。 parted -l
在我运行以下代码时使用exec_command
它挂起。 请让我知道在被绞死之前如何检查。
with SSHClient() as client:
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('172.20.xxx.xx', port=22, username='xxx', password='xxxxxxx',timeout=15.0)
transport = client.get_transport()
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command('sudo su -')
time.sleep(5)
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stderr = session.makefile_stderr('rb', -1)
stdin.write('xxxxxxx' + '\n')
stdin.flush()
session.exec_command('parted -l')
time.sleep(5)
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stderr = session.makefile_stderr('rb', -1)
stdin.flush()
for line in stdout.read().splitlines():
print 'LINE: %s' % (line)
你想检查单个parted
命令的结果吗? 如果要检查单个命令的结果,请不要运行 shell(在本例中为su
)。 仅运行命令:
sudo su -c "parted -l"
另请参阅使用 Python 在 SSH 中使用“su -l”执行命令
然后你可以使用recv_exit_status()
:
如何使用 Paramiko 获取 SSH 返回码?
如果您想出于任何原因保留当前的方法,请参阅:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.