首页 > 编程语言 > 详细

python连接linux服务器

时间:2020-07-15 14:02:27      阅读:57      评论:0      收藏:0      [点我收藏+]
import paramiko


class Monitor:
	def __init__(self, server_ip, user, pwd):
		""" 初始化ssh客户端 """
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
			self.client = client
			print(f‘------------开始连接服务器{server_ip}-----------‘)
			self.client.connect(server_ip, 22, username=user, password=pwd, timeout=4)
			print(‘------------认证成功!.....-----------‘)
		except Exception:
			print(f‘连接远程linux服务器(ip:{server_ip})发生异常!请检查用户名和密码是否正确!‘)

	def link_server(self, cmd):
		"""连接服务器发送命令"""
		try:
			stdin, stdout, stderr = self.client.exec_command(cmd)
			content = stdout.read().decode()
			return content
		except Exception as e:
			print(‘link_server-->返回命令发生异常,内容:‘, e)
		finally:
			self.client.close()

  

python连接linux服务器

原文:https://www.cnblogs.com/yywy/p/13304544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!