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 \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‘, ‘[email protected]‘, ‘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

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

时间: 2024-10-13 03:11:37

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte的相关文章

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

这是我搭建django的时候的问题,那个时候重装了系统,出来后运行manage.py就报错了. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcf in position 1: invalid continuation byte. 错误代码如图. 原因是重装时主机名(计算机名)有中文. 尼玛坑爹啊.然后改主机名(计算机名)重启就好. 主机名  在 我的电脑右键属性里面 UnicodeDecodeError: 'utf-8' codec

TensorFlow学习笔记(UTF-8 问题解决 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte)

我使用VS2013  Python3.5  TensorFlow 1.3  的开发环境 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 在是使用Tensorflow读取图片文件的情况下,会出现这个报错 代码如下 # -*- coding: utf-8 -*- import tensorflow as tf import numpy as np import mat

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

Traceback (most recent call last): File "/Users/c2apple/PycharmProjects/easyToPython/fileMethod.py", line 94, in <module> line=fp.readline() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/codecs.py", line 3

python-使用pip安装第三方库报UnicodeDecodeError: &#39;utf8&#39; codec can&#39;t decode byte 0xcb in position 7: invalid continuation byte 错误解决方案

在python 的安装目录下找到Lib\ntpath.py文件,找到def join(path, *paths):方法,添加如下两行语句: reload(sys) sys.setdefaultencoding('gbk') 出现这种错误的原因:使用pip安装文件 先将文件放在一个临时文件夹下,而此文件夹的路径存在中文无法解析 python-使用pip安装第三方库报UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position

python3 报错:UnicodeDecodeError: &#39;utf-8&#39; codec can&#39;t decode byte 0xd6 in position 201: invalid continuation byte

代码: # -*- coding:utf-8 -*- from urllib import request resp = request.urlopen('http://www.xxx.com') print(resp.read().decode('utf-8')) 报错: Traceback (most recent call last): File "F:/workspace/python/py3/test_urllib.py", line 7, in <module>

Flask入门 - 关于报错UnicodeDecodeError: &#39;utf-8&#39; codec can&#39;t decode byte 0xd0 in position 2: invalid continuation byte

今天学习了Flask入门就遇到一个棘手的问题 run的时候会报一个编码错误: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 2: invalid continuation byte 问题解决办法: 当前计算机名称不能是中文,可以确认一下 Flask入门 - 关于报错UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 2:

&#39;utf-8&#39; codec can&#39;t decode byte 0xd5 in position XXX: invalid continuation byte问题

找了一下午,各种资料搜集,愣是没搜出来答案. 结果今天早上,做一个小小的改变,就整出来了... 步骤如下: 1.打开excel,全选数据 2.新建记事本,粘贴,选择脚本,更改字体: 3.新建Excel表格,粘贴回去,就OK了. 折腾了一下午啊,有没有?本来还打算直接从数据库中读取的... 不忘了,确实是参考人家帖子的,附上: 用python3读csv文件,出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position

Running error: &#39;utf-8&#39; codec can&#39;t decode byte 0xb4 in position 0: invalid start byte

Running Error: It is caused by the txt's encode format. Please do use UTF-8 to save the file (you can use Notepad++ to choose the encode format to save the file) if you would like to read in a python script. Running error: 'utf-8' codec can't decode

python &#39;utf-8&#39; codec can&#39;t decode byte 0xb8 in position 0: invalid start byte

在导入csv文件中,出现如上所示的错误,经过查阅资料,解决方法如下: 方法一: pd.read_csv(file_path, encoding='unicode_escape') 方法二: pd.read_csc(file_path.encoding='gbk') python 'utf-8' codec can't decode byte 0xb8 in position 0: invalid start byte 原文地址:https://www.cnblogs.com/zhichun/p/