def exec_cmd(self, ip_addr, username, password, cmd,
ssh_log=None, look_for_keys=True, key_filename=None):
if ssh_log is not None:
self.SSH_LOG = ssh_log
if self.log.get_level() == Logger.DEBUG:
paramiko.util.log_to_file(self.SSH_LOG)
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(
ip_addr,
port=self.SWITCH_PORT,
username=username,
password=password,
look_for_keys=look_for_keys,
key_filename=key_filename)
except (
paramiko.BadHostKeyException,
paramiko.AuthenticationException,
paramiko.SSHException,
socket.error,
BaseException) as exc:
self.log.error('%s: %s' % (ip_addr, str(exc)))
raise SSH_Exception('SSH connection Failure - {}'.format(exc))
# sys.exit(1)
try:
_, stdout, stderr = ssh.exec_command(cmd)
except paramiko.SSHException as exc:
self.log.error('%s: %s, %s' % (ip_addr, str(exc), stderr.read()))
sys.exit(1)
stdout_ = stdout.read()
stderr_ = stderr.read()
status = stdout.channel.recv_exit_status()
ssh.close()
return status, stdout_, stderr_
ssh.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录