【问题标题】:Python: How to get else statement to run x number of times?Python:如何让 else 语句运行 x 次?
【发布时间】:2016-11-06 21:26:13
【问题描述】:

我有一个 telnet 路由器的脚本,如果它不能 ping 8.8.8.8 来重启路由器,但我不希望它连续重启路由器超过 3 次。 如果它在 3 次后无法 ping 8.8.8.8,我希望它继续 ping 8.8.8.8,直到它再次恢复并重新开始。

import subprocess
output = subprocess.call('ping 8.8.8.8', shell=True)
while output == 0:
output = subprocess.call('ping 8.8.8.8', shell=True)
else:
import telnetlib
import datetime

now = datetime.datetime.now()

host = "912.168.1.1" # your router ip
username = "name" # the username
password = "password"
filename_prefix = "cisco-backup"

tn = telnetlib.Telnet(host)
tn.read_until("Username:")
tn.write(username+"\n")
tn.read_until("Password:")
tn.write(password+"\n")
tn.write("reload"+"\n")
tn.write("y"+"\n")
enter code here

我只是这方面的新手,我不知道如何让 else 语句连续重复三次。

【问题讨论】:

  • else 后面需要一个循环。但是,如果您没有break,则在while 循环上添加else 毫无意义。 else 是多余的。

标签: python telnet ping cisco


【解决方案1】:

你缺少缩进,所以我希望你的逻辑是正确的,无论如何以下应该做你想要的。

在您的情况下,使用 else 语句几乎没有意义,因为这只会使您处于必须创建另一个 while 循环的位置。另一个修复是在路由器重置后将output = subprocess.call... 行移动到while 循环的末尾(如果您进入循环,如果没有响应则清除路由器,没有理由在重新启动前重试)。

我添加了retries 变量来跟踪路由器被重置的次数,并将其作为条件添加到while 循环中。如果重试次数超过 3 次,循环将中断。

import subprocess
retries = 0
output = subprocess.call('ping 8.8.8.8', shell=True)
while (output == 0 and retries < 3):
    import telnetlib
    import datetime

    now = datetime.datetime.now()

    host = "912.168.1.1" # your router ip
    username = "name" # the username
    password = "password"
    filename_prefix = "cisco-backup"

    tn = telnetlib.Telnet(host)
    tn.read_until("Username:")
    tn.write(username+"\n")
    tn.read_until("Password:")
    tn.write(password+"\n")
    tn.write("reload"+"\n")
    tn.write("y"+"\n")
    output = subprocess.call('ping 8.8.8.8', shell=True)
    retries += 1

if (retries == 3):
    print ("Couldnt restore router")

【讨论】:

    【解决方案2】:

    您可以尝试这样做:

    如果我理解正确,您要做的是制作一个脚本,该脚本将不断尝试 ping 8.8.8.8,如果失败,如果重新启动没有帮助,它将重新启动路由器(最多 3 次)该脚本应该 ping 8.8.8.8 直到它成功。之后脚本将重新开始再次

    import subprocess
    output = 0
    while output == 0:
        output = subprocess.call('ping 8.8.8.8', shell=True)
    
        for i in xrange(3): #will restart the router until ping works OR at most 3 times
    
            if output == 0:  # if the ping succeeded don't restart the router
                break
    
            import telnetlib
            import datetime
    
            now = datetime.datetime.now()
    
            host = "912.168.1.1" # your router ip
            username = "name" # the username
            password = "password"
            filename_prefix = "cisco-backup"
    
            tn = telnetlib.Telnet(host)
            tn.read_until("Username:")
            tn.write(username+"\n")
            tn.read_until("Password:")
            tn.write(password+"\n")
            tn.write("reload"+"\n")
            tn.write("y"+"\n")
    
            output = subprocess.call('ping 8.8.8.8', shell=True) #after restarting the router check the ping again
    
        #after the loop
        while output != 0: output = subprocess.call('ping 8.8.8.8', shell=True)  #will ping until the ping work
    

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 2017-07-03
      • 2017-09-28
      • 2015-02-14
      • 2017-09-11
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多