第一篇: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('export LANGUAGE=en \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('测试IP地址', '普通账号', '普通账号的密码', '', 'root密码', 'id')
遇到问题:

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


因为中文和英文字符集转换不同,所以导致报错。
补充:由于操作系统字符集中‘LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"’的缘故,即使修改了LANG也是无用的,入下图


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

第二篇:ssh.invoke_shell() 切换root出现的新问题
注:转载请注明出处
第一篇:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte的更多相关文章
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte
需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 解决参考: 代码: def verification_ssh(host,username,password,port,roo ...
- Python:出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 0: invalid continuation byte问题
我在导入一个csv文件的时候出现了一个问题 报错的内容是这样的: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in positio ...
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte
用pandas打开csv文件可能会出现这种情况,原因可能是excel自己新建一个*.csv文件时候容易出错.进入文件另存为,然后选择csv文件即可.
- 解决linux 终端UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte
vi /etc/locale.conf 修改LANG="zh_CN.gbk" 最后执行source /etc/locale.conf 即可永久生效,下次登录,中文就不会乱码了.
- 用python3读csv文件出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
1.python3读取csv文件时报如下图所示的错误 2.分析原因:读取的csv文件不是 UTF8 编码的,而IDE工具默认采用 UTF8 解码.解决方法是修改源文件的解码方式. 3.使用nodepa ...
- windows系统下python setup.py install ---出现cl问题,cpp_extension.py:237: UserWarning: Error checking compiler version for cl: 'utf-8' codec can't decode byte 0xd3 in position 0: invalid continuation byte
将cpp_extension.py文件中的 原始的是 compiler_info.decode() try: if sys.platform.startswith('linux'): minimu ...
- UnicodeDecodeError: 'utf8' codec can't decode byte 0xce in position 47: invalid continuation byte
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 22: invalid continuation byte
在使用python读取文本文件,一般会这样写: # -*- coding:utf-8 -*- f = open("train.txt", "r", encodi ...
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 52: invalid continuation byte
代码: df_w = pd.read_table( r'C:\Users\lab\Desktop\web_list_n.txt', sep=',', header=None) 当我用pandas的re ...
随机推荐
- Domain Model
VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来. DTO(Data Transfer Object):数据传输对象,这个概念来源于J2EE的 ...
- UVALive - 6440
题目链接:https://vjudge.net/contest/241341#problem/G Indonesia, as well as some neighboring Southeast As ...
- 程序代码里出现illegal character '\ufeff' 和 expected class or object definition的解决办法(图文详解)
不多说,直接上干货! 问题详情 问题分析 可能原因导致1:你的程序也许,是在他人那里复制而来,会导致这样的问题. 可能原因导致2:由于页面编码造成的. 可能原因导致1的解决办法 这个,好比,我 ...
- SVN的搭建(入门篇)
如果转载,请注明出处,谢谢 1.安装SVN # yum install subversion 2.创建一个仓库 创建一个仓库svnrepos # svnadmin create /usr/svnrep ...
- pat1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- GridView相同内容合并单元格
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web; ...
- AngularJS(十):依赖注入
本文也同步发表在我的公众号“我的天空” 依赖注入 依赖注入不是AngularJS独有的概念,而是现代软件开发与架构的范畴,但是在AngularJS中“依赖注入”是其核心思想之一,所以我们专门来学习一下 ...
- angular 学习笔记(2) ng-repeat遍历json
视图: <div ng-app="myApp" ng-controller="myCtrl"> <ul> <li ng-repea ...
- 【干货】Html与CSS入门学习笔记12-14【完】
十二.HTML5标记 现代HTML html5新增的元素:header nav footer aside section article time 这些新增元素使页面结构更清晰,取代<div i ...
- 【干货】Html与CSS入门学习笔记4-8
四.web镇之旅,连接起来 找一家托管公司如阿里云,购买域名和空间,然后将网页文件上传到购买的空间的根目录下. 1.绝对路径url url:uniform resource locators 统一资源 ...