python连接linux服务器


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()

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM