TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件

不安全的IT

我遇到缓冲区错误,但是在第123行的“ with open()”块中有一个行缓冲区。这是否是缓冲区的正确位置?我是否也应该在connect类中添加某些东西,或者应该是parser参数。

我正在尝试使用带有地址或主机名的文件,并使用'--host_file'参数在多个ASA上运行SNMP命令。使用'--host'参数就可以了。

提供的任何帮助将不胜感激。

Traceback (most recent call last):
  File "asaos-snmpv3-tool.py", line 145, in <module>
    main()
  File "asaos-snmpv3-tool.py", line 124, in main
    with open(hosts, mode='r', buffering=-1):
TypeError: coercing to Unicode: need string or buffer, file found

import pexpect
import argparse

PROMPT = ['# ', '>>> ', '>', '\$ ']
SNMPGROUPCMD = ' snmp-server group '
V3PRIVCMD = ' v3 priv '
SNMPSRVUSRCMD = ' snmp-server user '
V3AUTHCMD = ' v3 auth '
PRIVCMD = ' priv '
SNMPSRVHOSTCMD = ' snmp-server host '
VERSION3CMD = ' version 3 '
SHAHMACCMD = ' sha '
SNMPSRVENTRAP = ' snmp-server enable traps all '
WRME = ' write memory '

def send_command(child, cmd):
    child.sendline(cmd)
    child.expect(PROMPT)
    print child.before

def connect(user, host, passwd, en_passwd):
    ssh_newkey = 'Are you sure you want to continue connecting?'
    constr = 'ssh ' + user + '@' + host
    child = pexpect.spawn(constr)
    ret = child.expect([pexpect.TIMEOUT, ssh_newkey, '[P|p]assword:'])

    if ret == 0:
        print '[-] Error Connecting'
        return
    if ret == 1:
        child.sendline('yes')
        ret = child.expect([pexpect.TIMEOUT, '[P|p]assword:'])
        if ret == 0:
            print '[-] Error Connecting'
            return
    child.sendline(passwd)
    child.expect(PROMPT)
    child.sendline('enable')
    child.sendline(en_passwd)
    child.expect(PROMPT)
    child.sendline('config t')
    child.expect(PROMPT)
    return child

def connects(user, hosts, passwd, en_passwd):
    ssh_newkey = 'Are you sure you want to continue connecting?'
    constr = 'ssh ' + user + '@' + hosts
    child = pexpect.spawn(constr)
    ret = child.expect([pexpect.TIMEOUT, ssh_newkey, '[P|p]assword:'])

    if ret == 0:
        print '[-] Error Connecting'
        return
    if ret == 1:
        child.sendline('yes')
        ret = child.expect([pexpect.TIMEOUT, '[P|p]assword:'])
        if ret == 0:
            print '[-] Error Connecting'
            return
    child.sendline(passwd)
    child.expect(PROMPT)
    child.sendline('enable')
    child.sendline(en_passwd)
    child.expect(PROMPT)
    child.sendline('config t')
    child.expect(PROMPT)
    return child

def main():
    parser = argparse.ArgumentParser('usage %prog ' + '--host --host_file --username --password--enable --group --snmp_user --snmp_host --int_name --snmp_v3_auth --snmp_v3_hmac --snmp_v3_priv --snmp_v3_encr')
    parser.add_argument('--host', dest='host', type=str, help='specify a target host')
    parser.add_argument('--host_file', dest='hosts', type=file, help='specify a target host file')
    parser.add_argument('--username', dest='user', type=str, help='specify a user name')
    parser.add_argument('--password', dest='passwd', type=str, help='specify a passwd')
    parser.add_argument('--enable', dest='en_passwd', type=str, help='specify an enable passwd')
    parser.add_argument('--group', dest='group', type=str, help='specify an snmp group')
    parser.add_argument('--snmp_user', dest='snmpuser', type=str, help='specify an snmp user')
    parser.add_argument('--snmp_host', dest='snmphost', type=str, help='specify an snmp server host')
    parser.add_argument('--int_name', dest='intname', type=str, help='specify interface name')
    parser.add_argument('--snmp_v3_auth', dest='snmpauth', type=str, help='specify the snmp user authentication')
    parser.add_argument('--snmp_v3_hmac', dest='snmphmac', type=str, help='set snmp HMAC, md5 or sha')
    parser.add_argument('--snmp_v3_priv', dest='snmppriv', type=str, help='specify the snmp priv password')
    parser.add_argument('--snmp_v3_encr', dest='snmpencrypt', type=str, help='specify encryption, des, 3des, or aes(128/192/256)')

    args = parser.parse_args()
    host = args.host
    hosts = args.hosts
    user = args.user
    passwd = args.passwd
    en_passwd = args.en_passwd
    group = args.group
    snmpuser = args.snmpuser
    snmphost = args.snmphost
    intname = args.intname
    snmpauth = args.snmpauth
    snmppriv = args.snmppriv
    snmpencrypt = args.snmpencrypt

    if hosts:
        with open(hosts, mode='r', buffering=1):
            for line in hosts:
                hosts = line.rstrip
                child = connects(user, hosts, passwd, en_passwd)
                send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
                send_command(child, SNMPSRVUSRCMD + snmpuser + ' ' + group + V3AUTHCMD + SHAHMACCMD + snmpauth + PRIVCMD + snmpencrypt + ' ' + snmppriv)
                send_command(child, SNMPSRVHOSTCMD + intname + ' ' + snmphost + VERSION3CMD + snmpuser)
                send_command(child, SNMPSRVENTRAP)
                send_command(child, WRME)

    elif host:
        child = connect(user, host, passwd, en_passwd)
        send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
        send_command(child, SNMPSRVUSRCMD + snmpuser + ' ' + group + V3AUTHCMD + SHAHMACCMD + snmpauth + PRIVCMD + snmpencrypt + ' ' + snmppriv)
        send_command(child, SNMPSRVHOSTCMD + intname + ' ' + snmphost + VERSION3CMD + snmpuser)
        send_command(child, SNMPSRVENTRAP)
        send_command(child, WRME)
    else:
        print ('Specify either --host or --host_file or I have nothing to do')

if __name__ == '__main__':
    main()
阿巴内特

这里有一系列问题:

if hosts:
    with open(hosts, mode='r', buffering=1):

在这里,hosts显然是一个文件名。您打开该文件……然后不要将其存储在变量中的任何位置,然后继续使用文件名就好像它是文件一样。

但是,在你甚至到达那里,hosts是不是真正的文件名。的一个漂亮功能之一argparse是它可以自动为您打开文件,并且您要求它通过type=file在参数spec中使用来做到这一点这意味着它hosts实际上是一个文件对象,您正在尝试将该文件对象本身用作文件名!这实际上是导致TypeError: coercing to Unicode: need string or buffer, file foundopen尝试将文件名转换为Unicode字符串的原因,并且不知道如何使用文件对象来实现。如果您想open托管,请不要告诉argparse为您托管;只需将其类型保留为即可str

        for line in hosts:

由于hosts应该是文件名,所以它是字符串,因此可以给您文件名中的每个字符作为字符串。这没有用。

            hosts = line.rstrip

这将覆盖hosts您需要变量。而且,更糟糕的是,它使用bound方法line.rstrip而不是call的结果覆盖了它line.rstrip

            child = connects(user, hosts, passwd, en_passwd)

在这里,您要将绑定的方法传递给需要字符串的函数,该函数将无法正常工作。

不要反复使用相同的变量名来表示不同的含义。每件事都使用不同的名称。理想情况下,其名称在某种程度上与其持有的东西有关。例如,文件ech中的ech(剥离)行hosts可能是hosthosts_entry并非全部的事情是您的全部hosts

无论如何,要解决所有这些问题:

if hosts:
    for line in hosts:
        host = line.rstrip()
        child = connects(user, host, passwd, en_passwd)

或者,如果您想控制hosts打开方式(我不确定为什么您认为指定重要性很重要buffering=1,但想必您有某些理由?):

parser.add_argument('--host_file', dest='hosts', type=str, help='specify a target host file')

# …

if hosts:
    with open(hosts, buffering=1) as hosts_file:
        for line in hosts_file:
            host = line.rstrip()
            child = connects(user, host, passwd, en_passwd)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件(open()函数)

来自分类Dev

TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件(open()函数)

来自分类Dev

Python:TypeError: 强制转换为 Unicode: 需要字符串或缓冲区,找到文件

来自分类Dev

TypeError:强制转换为Unicode:需要字符串或缓冲区,找到列表

来自分类Dev

TypeError:强制转换为Unicode:需要字符串或缓冲区,找到int

来自分类Dev

Python TypeError:强制转换为Unicode:需要字符串或缓冲区,找到元组

来自分类Dev

TypeError:强制转换为Unicode:需要字符串或缓冲区,找到ResultSet

来自分类Dev

TypeError:强制转换为Unicode:需要字符串或缓冲区,找到了PosixPath

来自分类Dev

TypeError:强制转换为Unicode:需要字符串或缓冲区,找到WSGIRequest

来自分类Dev

Boto3 S3:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件

来自分类Dev

Django模型:TypeError:强制转换为Unicode:需要字符串或缓冲区,已找到用户

来自分类Dev

熊猫to_csv():TypeError:强制转换为Unicode:需要字符串或缓冲区,找到列表

来自分类Dev

强制转换为Unicode:需要字符串或缓冲区,找到标记

来自分类Dev

强制转换为Unicode:需要字符串或缓冲区,找到列表

来自分类Dev

TypeError:强制转换为Unicode,需要字符串或缓冲区,找不到NoneType

来自分类Dev

Python MQTT:TypeError:强制转换为Unicode:需要字符串或缓冲区,发现布尔

来自分类Dev

引发异常时出现“ TypeError:强制转换为Unicode:需要字符串或缓冲区”

来自分类Dev

强制转换为Unicode:需要字符串或缓冲区,找到int-Django Rest Framework

来自分类Dev

类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到 _sre.SRE_Pattern

来自分类Dev

Django 错误强制转换为 Unicode:需要字符串或缓冲区

来自分类Dev

错误:强制转换为Unicode:需要字符串或缓冲区,很长一段时间

来自分类Dev

强制转换为 Unicode:需要字符串或缓冲区,NoneType 发现一切似乎都很好,但仍然出现此错误

来自分类Dev

而将字符缓冲区转换为字符串是需要 strdup

来自分类Dev

Python TypeError:导入文本文件时需要字符串或其他字符缓冲区对象

来自分类Dev

如何将字符串转换为二进制缓冲区?

来自分类Dev

将字节缓冲区转换为UTF8字符串

来自分类Dev

将固定大小的缓冲区(字节数组)转换为字符串

来自分类Dev

如何将tkFileDialog.asksaveasfilename()转换为缓冲区字符串?

来自分类Dev

JavaScript-将数组缓冲区转换为字符串

Related 相关文章

  1. 1

    TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件(open()函数)

  2. 2

    TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件(open()函数)

  3. 3

    Python:TypeError: 强制转换为 Unicode: 需要字符串或缓冲区,找到文件

  4. 4

    TypeError:强制转换为Unicode:需要字符串或缓冲区,找到列表

  5. 5

    TypeError:强制转换为Unicode:需要字符串或缓冲区,找到int

  6. 6

    Python TypeError:强制转换为Unicode:需要字符串或缓冲区,找到元组

  7. 7

    TypeError:强制转换为Unicode:需要字符串或缓冲区,找到ResultSet

  8. 8

    TypeError:强制转换为Unicode:需要字符串或缓冲区,找到了PosixPath

  9. 9

    TypeError:强制转换为Unicode:需要字符串或缓冲区,找到WSGIRequest

  10. 10

    Boto3 S3:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件

  11. 11

    Django模型:TypeError:强制转换为Unicode:需要字符串或缓冲区,已找到用户

  12. 12

    熊猫to_csv():TypeError:强制转换为Unicode:需要字符串或缓冲区,找到列表

  13. 13

    强制转换为Unicode:需要字符串或缓冲区,找到标记

  14. 14

    强制转换为Unicode:需要字符串或缓冲区,找到列表

  15. 15

    TypeError:强制转换为Unicode,需要字符串或缓冲区,找不到NoneType

  16. 16

    Python MQTT:TypeError:强制转换为Unicode:需要字符串或缓冲区,发现布尔

  17. 17

    引发异常时出现“ TypeError:强制转换为Unicode:需要字符串或缓冲区”

  18. 18

    强制转换为Unicode:需要字符串或缓冲区,找到int-Django Rest Framework

  19. 19

    类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到 _sre.SRE_Pattern

  20. 20

    Django 错误强制转换为 Unicode:需要字符串或缓冲区

  21. 21

    错误:强制转换为Unicode:需要字符串或缓冲区,很长一段时间

  22. 22

    强制转换为 Unicode:需要字符串或缓冲区,NoneType 发现一切似乎都很好,但仍然出现此错误

  23. 23

    而将字符缓冲区转换为字符串是需要 strdup

  24. 24

    Python TypeError:导入文本文件时需要字符串或其他字符缓冲区对象

  25. 25

    如何将字符串转换为二进制缓冲区?

  26. 26

    将字节缓冲区转换为UTF8字符串

  27. 27

    将固定大小的缓冲区(字节数组)转换为字符串

  28. 28

    如何将tkFileDialog.asksaveasfilename()转换为缓冲区字符串?

  29. 29

    JavaScript-将数组缓冲区转换为字符串

热门标签

归档