zoukankan      html  css  js  c++  java
  • paramiko 实现远程切换root

    # 远程执行docker 命令
    def exec_cod(host, username, password, port, root_pwd, image_count, cmd, cmd2):
        s = paramiko.SSHClient()
        s.load_system_host_keys()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        s.connect(hostname=host, port=int(port), username=username, password=password)
        if username != 'root':
            ssh = s.invoke_shell()
            time.sleep(0.1)
            ssh.send('su - 
    ')
            buff = ''
            while not buff.endswith('Password: '):
                resp = ssh.recv(9999)
                buff += resp.decode('utf-8')
            ssh.send(root_pwd)
            ssh.send('
    ')
            buff = ''
            while not buff.endswith('# '):
                resp = ssh.recv(9999)
                buff += resp.decode('utf-8')
            if host == '10.210.0.71' or host == '10.210.0.73':
                ssh.send('docker-compose -f /home/ubuntu/ruo/docker-compose.yml scale php=' + str(image_count))
            else:
                ssh.send('docker-compose -f /root/ubuntu/ruo/docker-compose.yml scale php=' + str(image_count))
            ssh.send('
    ')
            buff = ''
            while not buff.endswith('# '):
                resp = ssh.recv(9999)
                buff += resp.decode('utf-8')
            print(buff)
            if host == '10.210.0.71' or host == '10.210.0.73':
                ssh.send('docker-compose -f /home/ubuntu/ruo/docker-compose.yml restart')
            else:
                ssh.send('docker-compose -f /root/ubuntu/ruo/docker-compose.yml restart')
            ssh.send('
    ')
    
            buff = ''
            while not buff.endswith('# '):
                resp = ssh.recv(9999)
                buff += resp.decode('utf-8')
            print(buff)
            ssh.send('docker ps')
            ssh.send('
    ')
            buff = ''
            while not buff.endswith('# '):
                resp = ssh.recv(9999)
                buff += resp.decode('utf-8')
            s.close()
            result = buff
            print(buff)
        else:
            pass
            # stdin, stdout, stderr = s.exec_command(cmd)
            # result = stdout.read()
            # s.close()
        return result
    

      

  • 相关阅读:
    python函数第4天(day 23)
    python函数第3天(day 22)
    python函数第2天(day 21)
    python函数第一天(day 20)
    opencv笔记一(Mat初始化及Mat传递)
    ubuntu上opencv源码安装
    ubuntu上显卡驱动安装——GeForce GTX 1080 Ti
    ubuntu上CUDA9.0和CUDNN7.0安装
    vscode配置opencv c++开发环境
    opencv中Mat的属性
  • 原文地址:https://www.cnblogs.com/412013cl/p/9096122.html
Copyright © 2011-2022 走看看