0

I'm writing a script that will find out what router model and what IOS version a Cisco router is using. I'm writing it in Python using the SecureCRT api. The script sends a show version command that displays information about the router, including the information I need. I then use the SecureCRT api to pull all of that text from the application screen and then I iterate through the text to and use if statements to match router models to see which one it is. Everytime i run the script it runs and doesnt error out but the "new.txt" file is blank.

# $language = "python"
# $interface = "1.0"

crt.Screen.Synchronous = True
ModelIOSScreen = ""

def Main():
   ModelIOS()

def ModelIOS():
    crt.Screen.Send("show version" + chr(13))
    crt.Screen.WaitForString(">")
    Screen = crt.Screen.Get(-1, 1, 50, 70)
    ModelIOSScreen = str(Screen.split(" ", -1))

    RouterModel = ""

    for word in ModelIOSScreen:
        if word == "2811":
            RouterModel = "2811"
        elif word == "2801":
            RouterModel = "2801"
        elif word == "CISCO2911/K9":
            RouterModel = "2911"

    file = open("new.txt", "w")
    file.write(ModelIOSScreen)
4

1 回答 1

1

我正在打电话,可能会写一个更好的答案,但我要睡觉了。你永远不会关闭你打开的文件。使用以下效果更好。

with open(file, "w") as fp:
  fp.write(variable)
于 2016-03-26T12:28:58.323 回答