Python学习笔记--ssh登录Linux设备(root、非root切换)
编写成功的代码
# -- coding: UTF-8
__author__ = 'jlr'
import paramiko
class ProcessCheck(object):
#初始化
def __init__(self, deviceIP):
self.deviceIP = deviceIP
#登录无需跳转用户,如root
def LoginDevice(self, port, username, password, cmd):
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(self.deviceIP, port, username, password)
self.stdin, self.stdout, self.stderr = self.ssh.exec_command(cmd)
return self.stdout.read().decode()
#登录跳转用户,如zxisec->root
def logindevice_reinforce(self, port1, username1, password1, root_pwd, cmd2):
self.ssh2=paramiko.SSHClient()
self.ssh2.load_system_host_keys()
self.ssh2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh2.connect(hostname = self.deviceIP,port=int(port1),username=username1, password=password1)
if username1 != 'root':