Home python SSH in Python, paramiko

SSH in Python, paramiko

Author

Date

Category

I saw other useful discussion but not found

import paramiko
ssh = paramiko.SSHClient ()
ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())
ssh.connect ( '192.168.0.13', username = 'aa', password = 'aa')
stdin, stdout, stderr = ssh.exec_command ( "sudo reboot")
stdin.write ( 'aa \ n')
stdin.flush ()
data = stdout.read () + stderr.read ()
print (data)
ssh.close ()

This code should perform like ‘sudo reboot’ and accordingly send the password ‘aa’ that would sudo to run, but it does not, why? The code itself is executed without errors

UPD: I checked, and it is connected “is”


Answer 1

This is probably because a password is required to enter the terminal, and you have not provided any terminal. Try ssh.exec_command (…, get_pty = True) andreymal


Answer 2

Try the following code:

import paramiko
client = paramiko.SSHClient ()
client.set_missing_host_key_policy (paramiko.AutoAddPolicy ())
client.connect (...)
slient.get_transport channel = (). open_session ()
channel.get_pty ()
channel.settimeout (5)
channel.exec_command ( 'sudo reboot')
channel.send (password + '\ n')
print channel.recv (1024)
channel.close ()
client.close ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions