zoukankan      html  css  js  c++  java
  • 第一篇:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

    需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 

    解决参考:

    代码:

    def verification_ssh(host,username,password,port,root_pwd,cmd):
        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(' export LANG=en_US.UTF-8 
    ') #解决错误的关键,编码问题
            ssh.send('export LANGUAGE=en 
    ')

    ssh.send('su - ')
    
            buff = ""
            while not buff.endswith('Password: '): #true
                resp = ssh.recv(9999)
                print(resp)
                buff +=resp.decode('utf8')
    
            print('hhhhh')
            print(buff)
    
            ssh.send(root_pwd)
            ssh.send('
    ')
    
            buff = ""
            # n = 0
            while not buff.endswith('# '):
                # n += 1
                resp = ssh.recv(9999)
                print(resp)
                buff +=resp.decode('utf8')
                # print(n)
                # if n >=3:
                #     break
    
    
    
            # print(buff)
    
            ssh.send('sh /tmp/check/101.sh') #放入要执行的命令
            ssh.send('
    ')
            buff = ''
            # m = 0
            while not buff.endswith('# '):
                resp = ssh.recv(9999).decode()
                buff +=resp
                # m += 1
                # print(m)
    
            result  = buff
            # print(type(result))
            # print(result)
            s.close()
    
    if __name__ == "__main__":
        verification_ssh('测试IP地址', '普通账号', '普通账号的密码', '52222', 'root密码', 'id')

    遇到问题:

         

    思路:经过检查发现这个是中文字符集和英文字符集返回密码格式不同导致的,在代码中加入:ssh.send(' export LANG=en_US.UTF-8 '),即可解决上述问题

     因为中文和英文字符集转换不同,所以导致报错。

    补充:由于操作系统字符集中‘LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"’的缘故,即使修改了LANG也是无用的,入下图

    此时还需将LANGUAGE的变量值修改:export LANGUAGE=en

    第二篇:ssh.invoke_shell() 切换root出现的新问题

    注:转载请注明出处

  • 相关阅读:
    获取系统信息
    Spring下获取项目根路径--good
    Java 获取webapp,Root,classpath,项目等路径工具类
    并发与并行的区别
    享元模式的简单使用
    mysql 分库分表(水平切割和垂直切割)
    sql随机筛选几条记录
    创建表
    sql字段组合唯一
    Jobject 使用
  • 原文地址:https://www.cnblogs.com/apff/p/9484939.html
Copyright © 2011-2022 走看看