码迷,mamicode.com
首页 > 其他好文 > 详细

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

时间:2018-08-16 10:46:14      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:ica   技术分享   分享图片   connect   思路   div   end   tin   paramik   

需求: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 \n) #解决错误的关键,编码问题
        ssh.send(su - \n)

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

        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(\n)
        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(172.25.240.164, cattsoft, Cat@2018, 52222, Huawei12#$, id)

遇到问题:

     技术分享图片

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

技术分享图片

技术分享图片

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

 

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

标签:ica   技术分享   分享图片   connect   思路   div   end   tin   paramik   

原文地址:https://www.cnblogs.com/apff/p/9484939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!