免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3342 | 回复: 2

python特殊字符转换 [复制链接]

论坛徽章:
6
IT运维版块每日发帖之星
日期:2015-09-05 06:20:00IT运维版块每日发帖之星
日期:2015-09-06 06:20:00IT运维版块每日发帖之星
日期:2015-10-17 06:20:00IT运维版块每周发帖之星
日期:2015-11-06 19:28:13IT运维版块每日发帖之星
日期:2015-11-07 06:20:00操作系统版块每周发帖之星
日期:2015-12-02 15:01:04
发表于 2016-06-03 13:53 |显示全部楼层
通过paramiko module启动ssh会话执行完命令后,本地在windows保存了命令的输出,查看后发现内容包含很多软回车和换行符,而不是正常显示的格式,内容如下:
b'Last login: Fri Jun  3 13:33:31 2016 from 10.12.194.83\r\r\n[root@vm007 ~]# uname -a'b'\r\n'b'Linux vm007 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux\r\n[root@vm007 ~]# 'b'exit\r\nlogout\r\n'

是否有办法将\r\n转换为正常的windows下显示的内容,比如
Last login: Fri Jun  3 13:33:31 2016 from 10.12.194.83\r\r\n[root@vm007 ~]# uname -a
Linux vm007 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@vm007 ~]#'
logout

多谢

论坛徽章:
6
IT运维版块每日发帖之星
日期:2015-09-05 06:20:00IT运维版块每日发帖之星
日期:2015-09-06 06:20:00IT运维版块每日发帖之星
日期:2015-10-17 06:20:00IT运维版块每周发帖之星
日期:2015-11-06 19:28:13IT运维版块每日发帖之星
日期:2015-11-07 06:20:00操作系统版块每周发帖之星
日期:2015-12-02 15:01:04
发表于 2016-06-03 14:00 |显示全部楼层
贴上实现代码,其中我把输出用str函数转了一下,否则要报错:
def ssh_runcommand(ip, user, password1, password2, cmd, port=22):
    try:
        s = paramiko.SSHClient()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        s.connect(ip, port, user, password1)

        if user != 'root':
            ssh = s.invoke_shell()
            time.sleep(0.1)
            ssh.send('export LANG=en_US.UTF-8;su - root\n')

            buff = ''
            while not buff.endswith('assword: '):
                resp = ssh.recv(9999)
                buff += resp
            ssh.send(password2)
            ssh.send('\n')

            buff = ''
            while not buff.endswith('# '):
                resp = ssh.recv(9999)
                buff += resp
            ssh.send(cmd)
            ssh.send('\n')
            time.sleep(0.1)
            ssh.send('exit')
            ssh.send('\n')

            buff = ''
            while re.search('logout.*', buff) is None:
                resp = ssh.recv(9999)
                buff += resp
            s.close()
            ssh.close()
        else:
            ssh = s.invoke_shell()
            time.sleep(0.1)
            ssh.send(cmd)
            ssh.send('\n')
            time.sleep(0.1)
            ssh.send('exit')
            ssh.send('\n')

            buff = ''
            while re.search('logout.*', buff) is None:
                resp = ssh.recv(9999)
                buff += str(resp)
            s.close()
            ssh.close()

            f = open(os.path.join(current_path, log_file), 'a')
            f.write(str(buff))
            f.close()
    except BaseException as x:
        print('Connect %s error, can not run the command %s, error information is %s' % (ip, cmd, x))

论坛徽章:
6
IT运维版块每日发帖之星
日期:2015-09-05 06:20:00IT运维版块每日发帖之星
日期:2015-09-06 06:20:00IT运维版块每日发帖之星
日期:2015-10-17 06:20:00IT运维版块每周发帖之星
日期:2015-11-06 19:28:13IT运维版块每日发帖之星
日期:2015-11-07 06:20:00操作系统版块每周发帖之星
日期:2015-12-02 15:01:04
发表于 2016-06-08 21:43 |显示全部楼层
好沉默的论坛啊  
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP