What I've tried:
invoke_shell()
thenchannel.send
su
and then sending the password resulted in not being rootinvoke_shell()
and thenchannel.exec_command
resulted in a "Channel Closed" error_transport.open_session()
thenchannel.exec_command
resulted in not being rootinvoke_shell()
then writing to stdin and flushing it resulted in not being root
Im sorry i dont have time for details answer but i was able to implement sudo commands on paramiko using this advise
invoke_shell
worked for me like this:AlexS
Fine tuned answer (which I am now using it in production) would be:Remove the
key_filename
part ofconnect
method if you dont use a key file and in contrast if you only use a key without password, remove thepassword
part.Some notes about this is that, it is multi command capable. Meaning that is is running a
bash
asroot
so you can as much commands as you can in a single run with just separating them with;
.I was able to run
sudo cupsdisable
command on the remote server manually without having to enter the password when I login to that server as one of the admin user(not root) but when I execute the same using stdin, stdout,stderr = client.exec_command("sudo cupsdisable <Printqueuename>")
it does nothing.The command that worked for me was:
This is specific to the above mentioned scenario only. Hope this helps someone
To my mind it would be much easier and secure to create a script which has sudoer rights.
For example, add this to sudoers:
Now you can just invoke the script via paramiko on the host machine and be done with it.