1

I'd like to execute commands by root user. Firstly, I need to execute the command "sudo su" and after executing other commands such as docker pull, git clone, etc in particular session. I want to figure out the most efficient way to do it by using paramiko.

  • Why not ssh as the root user? – rdas Apr 11 '19 at 13:53
  • I can't log in with root user because there is the configuration of the base AMI in AWS. I launch the instance from the AMI, then connect to the instance and provision it. And after I connect with "centos" I should switch to the root user in order to provision the instance. – Ihor Pysmennyi Apr 11 '19 at 14:41
  • Are you able to configure sudo to add your user rights to execute all commands you need? Than you might execute each command with sudo as follows: client.exec_command('sudo docker pull') – gbajson Apr 12 '19 at 11:25

1 Answers1

0

I realize that it's not ideal, but if you have a chain of commands to execute through sudo you may run them as sudo bash -c "command1; command2":

In [11]: stdin, stdout, stderr = client.exec_command('sudo bash -c "id; id"')

In [12]: stdout.read().splitlines()
Out[12]:
[b'uid=0(root) gid=0(root) groups=0(root)',
 b'uid=0(root) gid=0(root) groups=0(root)']
gbajson
  • 1,531
  • 13
  • 32