Python:错误后继续执行代码

发布于 2025-02-08 15:56:29 字数 1017 浏览 0 评论 0原文

我正在尝试创建一个程序,该程序将通过一系列IP地址迭代并连接到各种Cisco设备。 “ iparray”变量在其他页面上定义了,它正在从Excel电子表格中删除数据。

到目前为止,代码本身正在工作,但是我在一些假地址中添加一旦遇到错误,它就会通过循环迭代。我试图将位于erse的代码块放在:在其他情况下:字段中,但似乎该功能在第一次失败后完全停止。一旦遇到错误,我将如何继续此代码?

def backupAll(): 
    try:
        for element in ipArray:
            wRouter = {

                "device_type": "cisco_ios",
                "ip": element,
                "username": 'test',
                "password": 'cisco',
                "secret": 'cisco',

            }
            c = ConnectHandler(**wRouter)
            c.enable()
            h = c.send_command("show run | inc hostname")  ### 
            hostName = str(h)
            output1 = c.send_command("show run")
            save_file = open(hostName + '.txt', 'w')
            save_file.write(output1)
            save_file.close()
            c.disconnect()
            print(hostName + ' Complete')
    except Exception as ex:
        print(ex)
        pass
    else:
        pass
    finally:
        pass

I'm trying to create a program that will iterate through an array of IP addresses and connect to various cisco devices. The "ipArray" variable was defined on a different page and it's pulling in data from an excel spreadsheet.

So far, the code itself is working, but I added in some fake addresses so I could test a try/except clause and continue the program if one of the addresses isn't reachable, but I'm having trouble getting the function to continue iterating through the loop once it encounters the error. I've attempted to put the code block located under Try: in the Else: field, but it seems the function just stops entirely after it fails the first time. How would I be able to continue this code once encountering the error?

def backupAll(): 
    try:
        for element in ipArray:
            wRouter = {

                "device_type": "cisco_ios",
                "ip": element,
                "username": 'test',
                "password": 'cisco',
                "secret": 'cisco',

            }
            c = ConnectHandler(**wRouter)
            c.enable()
            h = c.send_command("show run | inc hostname")  ### 
            hostName = str(h)
            output1 = c.send_command("show run")
            save_file = open(hostName + '.txt', 'w')
            save_file.write(output1)
            save_file.close()
            c.disconnect()
            print(hostName + ' Complete')
    except Exception as ex:
        print(ex)
        pass
    else:
        pass
    finally:
        pass

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2025-02-15 15:56:29

您只需要将尝试/除外放在循环块中!

def backupAll():
    for element in ipArray:
        wRouter = {

            "device_type": "cisco_ios",
            "ip": element,
            "username": 'test',
            "password": 'cisco',
            "secret": 'cisco',

        }
        try:
            c = ConnectHandler(**wRouter)
            c.enable()
            h = c.send_command("show run | inc hostname")  ### 
            hostName = str(h)
            output1 = c.send_command("show run")
            save_file = open(hostName + '.txt', 'w')
            save_file.write(output1)
            save_file.close()
            c.disconnect()
            print(hostName + ' Complete')
        except Exception as ex:
            print(ex)

you just need to put the try/except in the loop block!

def backupAll():
    for element in ipArray:
        wRouter = {

            "device_type": "cisco_ios",
            "ip": element,
            "username": 'test',
            "password": 'cisco',
            "secret": 'cisco',

        }
        try:
            c = ConnectHandler(**wRouter)
            c.enable()
            h = c.send_command("show run | inc hostname")  ### 
            hostName = str(h)
            output1 = c.send_command("show run")
            save_file = open(hostName + '.txt', 'w')
            save_file.write(output1)
            save_file.close()
            c.disconnect()
            print(hostName + ' Complete')
        except Exception as ex:
            print(ex)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文