1

I am trying to run some commands to an AWS EC2 instance remotely via ssh within the python boto ssh module (which uses paramiko package). I am using boto.manage.cmdshell.sshclient_from_instance.run which works fine but the problem is that with .run is that it cannot do sudo command remotely. I get this error message:

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

I know I can manually login and edit permission to fix that but I want that part to be automated as well. I can launch an interactive shell using .shell but is there a way to automate commands within that interactive shell using python?

AnthonyC
  • 1,880
  • 2
  • 20
  • 27

2 Answers2

2

You should use boto.manage.cmdshell.SSHClient.run_pty(command) instead. It will request a pseudo-terminal and execute the command.

See the post How does paramiko Channel.recv() exactly work? to see how you can read standard output.

Community
  • 1
  • 1
Eric Citaire
  • 4,355
  • 1
  • 29
  • 49
  • Yea I should've mentioned that but what I noticed with that command is that I cannot simply do a print to see the output (while it does give output they are not the same as print). Also I noticed someone asked that question before but there was no answer. (http://stackoverflow.com/questions/27711530/correct-way-to-get-output-of-run-pty-from-a-boto-sshclient) I supposed what I can do is to use .run_pty to quickly change the permission so I can use .run. – AnthonyC Nov 20 '15 at 17:30
  • True but I have maybe about a week of actual python coding experience so would have prefer an easier way out. :o – AnthonyC Nov 20 '15 at 17:49
  • You should take a look to [paramiko.channel.Channel.exec_command documentation](http://docs.paramiko.org/en/1.16/api/channel.html#paramiko.channel.Channel.exec_command). – Eric Citaire Nov 20 '15 at 17:54
  • This post might also help : http://stackoverflow.com/questions/24125182/how-does-paramiko-channel-recv-exactly-work – Eric Citaire Nov 21 '15 at 11:24
1

I ended up fixing it by using .run_pty and then edit the sudoers file to not requiretty. Afterward I just use sudo with .run

AnthonyC
  • 1,880
  • 2
  • 20
  • 27