第一篇: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 ...
随机推荐
- 装饰器(Decorator)模式
public interface IDoThings { public void doSomeThing(); } public class DoThings implements IDoThings ...
- Error: Duplicate key name 'PCS_STATS_IDX' (state=42000,code=1061)
以下异常说明mysql已经启动. 应先关掉先前启动的mysql.再执行初始化schema操作. $service mysql stop; # $HIVE_HOME/bin/schematool -db ...
- UVALive - 4223(hdu 2926)
---恢复内容开始--- 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS ...
- ACdream 1236 Burning Bridges 割边 + 去重边
题目就是求一副图的割边,然后对于那些有重复的边的,不能算做割边. 思路就是每次加入一条边的时候,判断这条边是否存在过,存在过的话,就把那条边设为inf,表示不能作为割边.于是有了这样的代码 #incl ...
- unity ForceMode
public float jumpAbility; GetComponent<Rigidbody>().AddForce(Vector3.up * jumpAbility, ForceMo ...
- (转)在CentOS中修改中文字符集
虽然在实际工作环境下,Linux中不建议使用中文,但是如果一定要进行中文显示,尤其对于刚接触linux且英语基础不太好的人来说,那么本文具有一定的参考价值. 本文介绍在linux的shell环境下优化 ...
- jquery的.get方法说解
·Customer类 public class Customer { public int Unid { get; set; } public string CustomerName { get; s ...
- Quality of Service (QoS) in LTE
Background: Why we need QoS ? There are premium subscribers who always want to have better user expe ...
- DataBinding 访问 3
MVVM中的Model 我们可以用任何POJO 作为 data binding 的 Model, 但是直接修改POJO对象,不能直接更新UI android的 dataBinding 模块 给提供了通 ...
- Morris.js-利用JavaScript生成时序图
morris.js是一个轻量级的时间序列javascript类库,是网页显示图表的好工具.github项目地址:点击打开,使用起来很简单,但是需要你有一点网页设计的一些基本知识,对一个网页内容的结构要 ...