【问题标题】:getting error TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper收到错误类型错误:预期的 str、字节或 os.PathLike 对象,而不是 _io.TextIOWrapper
【发布时间】:2022-11-09 02:45:53
【问题描述】:

我正在尝试使用 python netmiko 库使用文件自动配置 cisco 设备,但是在测试时出现以下错误。从谷歌搜索我猜这可能与我用于配置文件的 open 函数有关。

Traceback (most recent call last):
  File "config-auto.py", line 55, in <module>
    device_list()
  File "config-auto.py", line 50, in device_list
    output =  net_connect.send_config_from_file(inputfile)
  File "/home/mmwanza/flask-project/venv/lib/python3.6/site-packages/netmiko/base_connection.py", line 2022, in send_config_from_file
    with io.open(config_file, "rt", encoding="utf-8") as cfg_file:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper


from netmiko import ConnectHandler
import getpass

 
#prompt user for username
username = input('Enter username: ')

##username method to return the username
def credentials_username():
    cred = username
    return cred


#prompt user for password
p = getpass.getpass('Enter password: ')

##password method to return the password
def credentials_password():
    password = p
    return password

#Prompt to enter device file
devices_file = input('Enter devices inventory file name: ')

with open(devices_file) as hosts:
    addresses = hosts.readlines()

#Prompt to enter configuration file
configsfile = input('Enter configuration file: ')
inputfile = open(configsfile, "r")



##devices dictionary
def device_list():
    ios_device_info = dict()
    for device in addresses:
        ios_device_info['ip'] = device
        ios_device_info['device_type'] = "cisco_ios"
        ios_device_info['username'] = credentials_username()
        ios_device_info['password'] = credentials_password()
        net_connect = ConnectHandler(**ios_device_info)
        output =  net_connect.send_config_from_file(inputfile)

device_list()

【问题讨论】:

    标签: python file netmiko


    【解决方案1】:

    我尝试做几乎相同的事情(我已经退回到尽可能简单的硬编码用户并通过并使用脚本中制作的列表)完全相同的错误。我会让你知道我发现了什么。请分享相同的!

    吉姆·W

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 2023-01-14
      • 2019-04-11
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      相关资源
      最近更新 更多