首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Napalm Automation)在测试使用" with“打开文件/cisco开关时,使用try除块后出现缩进错误。

(Napalm Automation)在测试使用" with“打开文件/cisco开关时,使用try除块后出现缩进错误。
EN

Stack Overflow用户
提问于 2019-08-24 11:19:21
回答 2查看 417关注 0票数 0

获取IndentationError:应为缩进块

我故意尝试使用错误的凭据连接到设备,以测试身份验证异常。如果我删除了try块,并确保代码块是用正确的语法编写的enter code here,那么它运行得很好。这意味着程序会崩溃,就像它想要的那样。但是,如果我使用了正确的用户名和密码,它就可以正常工作。只有当我执行try块时,我才会得到上面的错误。

代码语言:javascript
复制
from napalm import get_network_driver
from getpass import getpass
from netmiko import NetMikoAuthenticationException


username = input('username')
password = getpass('password')
driver = get_network_driver('ios')
with open('devices.txt','r') as switch_db:
    for switch in switch_db:
    #set up to connect to a switch from switch_db
        try:
            with driver(switch, username, password) as device:

        except NetMikoAuthenticationException:
            print('Authentication Error!')
EN

回答 2

Stack Overflow用户

发布于 2019-08-24 23:27:05

原来,我使用的with context管理器行被认为是一个空的try块,所以我更改了代码,以采取一个操作来设置连接调用的变量。当然,这是可行的,但您必须记住在您的代码中关闭连接。

代码语言:javascript
复制
from pprint import pprint
from napalm import get_network_driver
from getpass import getpass
from netmiko import NetMikoAuthenticationException
import json


username = input('username')
password = getpass('password')
driver = get_network_driver('ios')

with open('devices.txt','r') as switch_db:
    for switch in switch_db:
    #set up to connect to a switch from switch_db
        try:
            device = driver(switch, username, password)
            device.open()
        except NetMikoAuthenticationException:
            print('Authentication Error!')
            username = input('username')
            password = getpass('password')
            device = driver(switch, username, password)
            device.open()
        else:
            print('line after try block')
            pprint(device.get_interfaces())
            device.close()
            print('switch is closed')'
票数 0
EN

Stack Overflow用户

发布于 2019-08-25 02:01:07

在这里回复,所以也可以看到这一点。

在try/except块中有一个空的with构造函数。这是可行的,并针对思科DevNet沙盒进行了测试。

代码语言:javascript
复制
#!/usr/bin/env python3

from napalm import get_network_driver
from getpass import getpass
from netmiko import NetMikoAuthenticationException
from napalm.base.exceptions import ConnectionException
from pprint import pprint


username = input('username: ')
password = getpass('password: ')
driver = get_network_driver('ios')
with open('devices.txt','r') as switch_db:
    for switch in switch_db:
    #set up to connect to a switch from switch_db
        try:
            with driver(switch, username, password, optional_args={'port': 8181}) as device:
                pprint(device.get_facts())
        except NetMikoAuthenticationException:
            print('Authentication Error!')
        except ConnectionException:
            print(f'Could not connect to {switch}')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57634847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档