首页 / 操作系统 / Linux / 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")本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134607.htm