0

嘿伙计们,我正在尝试测试凝固汽油弹的提交,但它无法找到 cfg 文件,我也尝试更改为“txt”,但同样的错误。这是我的代码:

import napalm
import json


driver = napalm.get_network_driver("ios")


device = driver(
    hostname="10.0.0.254",
    username="cisco",
    password="cisco",
    optional_args={"secret" : "cisco"}

)

device.open()

device.load_merge_candidate(filename="config.cfg")
device.commit_config()
device.close() 

这是错误,两个文件都在同一个文件夹中:

user@user-pc:~/Documents/python_files$ /usr/bin/python3 /home/user/Documents/python_files/network/config_compare.py Traceback(最近一次调用最后):文件“/home/user/Documents/python_files /network/config_compare.py”,第 18 行,在 device.load_merge_candidate(filename="config.cfg") 文件“/home/user/.local/lib/python3.8/site-packages/napalm/ios/ios. py”,第 315 行,在 load_merge_candidate return_status 中,msg = self._load_candidate_wrapper(文件“/home/user/.local/lib/python3.8/site-packages/napalm/ios/ios.py”,第 282 行,在 _load_candidate_wrapper (return_status, msg) = self._scp_file( File "/home/user/.local/lib/python3.8/site-packages/napalm/ios/ios.py", line 620, in _scp_file return self._xfer_file( File “/home/user/.local/lib/python3.8/site-packages/napalm/ios/ios.py”,第 670 行,在 _xfer_file 中,使用 TransferClass(**kwargs) 作为传输:文件“/home/user/.local/lib/python3.8/site-packages/ netmiko/ssh_dispatcher.py”,第 278 行,在 FileTransfer 返回 FileTransferClass(*args, **kwargs) 文件“/home/user/.local/lib/python3.8/site-packages/netmiko/scp_handler.py”,行80,在初始化 self.source_md5 = self.file_md5(source_file) 文件“/home/user/.local/lib/python3.8/site-packages/netmiko/scp_handler.py”,第 257 行,在 file_md5 中打开(file_name,“rb ") as f: FileNotFoundError: [Errno 2] 没有这样的文件或目录:'config.c

求任何帮助。。

4

1 回答 1

0

看起来文件路径有问题,您确定“config.cfg”与您的代码在同一个目录中吗?

请尝试以下操作,看看您是否可以访问该文件(如果不能,则需要修改文件路径):

import napalm

driver = napalm.get_network_driver("ios")

device = driver(
    hostname="10.0.0.254",
    username="cisco",
    password="cisco",
    optional_args={"secret" : "cisco"}
)

device.open()

print('This is a config to be added:\n')

with open('config.cfg','r') as f:
    output = f.read()

device.load_merge_candidate(filename="config.cfg")
# I would add this to see what's you are merging:

print(device.compare_config())

confirm_config= input('Do you want to deploy the above config? Press Y to deploy \n')

if confirm_config == 'Y':
    device.commit_config()
    device.close() 
else:
    device.close() 
于 2020-06-12T13:50:24.910 回答