加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 连接linux服务器

发布时间:2020-12-20 10:45:57 所属栏目:Python 来源:网络整理
导读:import paramiko class Monitor(object): def __init__ (self,server_ip,user,pwd): """ 初始化ssh客户端 """ try : client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client = client print ( ‘ -------
import paramiko


class Monitor(object):
    def __init__(self,server_ip,user,pwd):
        """ 初始化ssh客户端 """
        try:
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.client = client
            print(------------开始连接服务器(%s)----------- % 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(gbk)
            return content
        except Exception as e:
            print(link_server-->返回命令发生异常,内容:,e)
        finally:
            self.client.close()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读