如何通过python在交互式shell中运行命令(boto for AWS)

时间:2015-11-20 16:27:12

标签: python shell amazon-web-services ssh

我正在尝试通过python boto ssh模块(使用paramiko包)中的ssh远程运行一些命令到AWS EC2实例。我正在使用boto.manage.cmdshell.sshclient_from_instance.run,它工作正常,但问题是.run是它无法远程执行sudo命令。我收到此错误消息:

sudo: sorry, you must have a tty to run sudo  

我知道我可以手动登录和编辑修复权限但我希望该部分也是自动化的。我可以使用.shell启动交互式shell,但有没有办法使用python在交互式shell中自动执行命令?

2 个答案:

答案 0 :(得分:2)

您应该使用boto.manage.cmdshell.SSHClient.run_pty(command)代替。它将请求伪终端并执行命令。

查看帖子How does paramiko Channel.recv() exactly work?,了解如何阅读标准输出。

答案 1 :(得分:1)

我最后使用.run_pty修复它,然后将sudoers文件编辑为不需要。之后我只使用sudo和.run