python操作リモートサーバをrootユーザーに切り替える


自動化されたメンテナンスでは、リモート・サーバがrootユーザーの下でコマンドを実行するように切り替える必要があります.いくつかの方法を試して、次のような使いやすい方法を得ました.
import time
import paramiko

def verification_ssh(host,username,password,port,root_pwd,cmd):
    s=paramiko.SSHClient()  
    s.load_system_host_keys()  
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname = host,port=int(port),username=username, password=password)
    if username != 'root':
        ssh = s.invoke_shell()
        time.sleep(0.1)
        ssh.send('su - 
')         buff = ''         while not buff.endswith('Password: '):             resp = ssh.recv(9999)             buff +=resp         ssh.send(root_pwd)         ssh.send('
')         buff = ''         while not buff.endswith('# '):             resp = ssh.recv(9999)             buff +=resp         ssh.send(cmd)         ssh.send('
')         buff = ''         while not buff.endswith('# '):             resp = ssh.recv(9999)             buff +=resp         s.close()         result = buff    else:         stdin, stdout, stderr = s.exec_command(cmd)         result = stdout.read()         s.close()     return result if __name__ == "main":     verification_ssh('192.168.1.11','cimer','1q2w3e4r',22,'1q2w3e4r','ifdown eth0')