需求: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的更多相关文章

  1. 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 ...

  2. 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 ...

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

    用pandas打开csv文件可能会出现这种情况,原因可能是excel自己新建一个*.csv文件时候容易出错.进入文件另存为,然后选择csv文件即可.

  4. 解决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 即可永久生效,下次登录,中文就不会乱码了.

  5. 用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 ...

  6. 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 ...

  7. UnicodeDecodeError: 'utf8' codec can't decode byte 0xce in position 47: invalid continuation byte

  8. 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 ...

  9. 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 ...

随机推荐

  1. Codeforces Round #365 (Div. 2) A

    Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...

  2. Jquery 实现表单验证,所有验证通过方可提交

    1. [代码]Jquery 实现表单验证,所有验证通过方可提交 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  3. object.wait为什么要和synchronized一块使用

    Object.wait 中JDK提供的doc文档 Causes the current thread to wait until another thread invokes the notify() ...

  4. LeetCode 583 Delete Operation for Two Strings 删除两个字符串的不同部分使两个字符串相同,求删除的步数

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  5. SPOJ 3267: DQUERY 树状数组,离线算法

    给出q个询问,询问一段区间里面的不同元素的个数有多少个. 离线做,用树状数组. 设树状数组的意义是:1--pos这个段区间的不用元素的种类数.怎么做?就是add(pos,1);在这个位置中+1,就是说 ...

  6. Java文件与io——字符流

    Writer写入字符流的抽象类.对文件的操作使用:FileWriter类完成 Reader读取字符的抽象类. public class CharDemo { /** * @param args */ ...

  7. 深入理解C#中的IDisposable接口(转)

    转自:https://www.cnblogs.com/wyt007/p/9304564.html 写在前面 在开始之前,我们需要明确什么是C#(或者说.NET)中的资源,打码的时候我们经常说释放资源, ...

  8. linux下使用shell脚本批处理命令

    1.新建脚本touch first.sh 2.写入命令vi first.sh #!/bin/bash #publish service and api echo "copy file&quo ...

  9. ubuntu下virtualbox的卸载

    本想在ubuntu下virtualbox,可惜出错了,需要卸载后再安装,只能百度拼凑后再安装: 1.首先是执行删除命令:sudo apt-get remove virtualbox*( 这样就不用去查 ...

  10. Filter过滤器,xml配置与页面不乱码整理

    1.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...