该脚本假设登录Cisco设备(通过控制台端口),查找匹配的if
语句并执行相应的代码。
脚本当前只尝试匹配第一个if
语句并忽略其余语句。
脚本如何搜索if
语句列表并执行相关的语句?
# $language = "python"
# $interface = "1.0"
# Connect to an SSH server using the SSH2 protocol. Specify the
# username and password and hostname on the command line as well as
# some SSH2 protocol specific options.
# msgbox = crt.Dialog.MessageBox
def main():
# Prompt for a password instead of embedding it in a script...
#
# passwd = crt.Dialog.Prompt("Enter password for " + host, "Login", "", True)
passwd = crt.Dialog.Prompt
# Build a command-line string to pass to the Connect method.
cmd = "/SERIAL COM6 /BAUD 9600 /NOCTS /DATA 8 /NODSR /PARITY NONE /NOXON"
crt.Session.Connect(cmd, False)
crt.Screen.Synchronous = True
# This line will need to be modified to wait for what the remote will send.
crt.Screen.Send(chr(13))
initial_1 = crt.Screen.WaitForString("outer")
initial_2 = crt.Screen.WaitForString("initial configuration dialog")
initial_3 = crt.Screen.WaitForString("con0")
initial_4 = crt.Screen.WaitForString("odified")
initial_5 = crt.Screen.WaitForString("Press RETURN to get started")
if initial_1 == True:
crt.Screen.Send("en" + chr(13))
crt.Screen.WaitForString("Password", 2)
crt.Screen.Send("groovy" + chr(13))
crt.Screen.WaitForString("#")
crt.Screen.Send("erase startup-config" + chr(13))
crt.Screen.WaitForString("Erasing")
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("#")
crt.Screen.Send("delete nvram:vlan.dat" + chr(13))
crt.Screen.WaitForString("vlan.dat")
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("confirm")
crt.Screen.Send(chr(13))
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("#")
crt.Screen.Send("reload" + chr(13))
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("System configuration has been modified", 2)
crt.Screen.Send("no" + chr(13))
crt.Screen.WaitForString("Proceed with reload", 2)
crt.Screen.Send(chr(13))
if initial_2 == True:
crt.Screen.Send("no" + chr(13))
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("Would you like to terminate autoinstall", 3)
crt.Screen.Send(chr(13))
crt.Screen.Send(chr(13))
if initial_3 == True:
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("rt1")
crt.Screen.Send("en" + chr(13))
crt.Screen.WaitForString("Password")
crt.Screen.Send("groovy" + chr(13))
crt.Screen.WaitForString("#")
crt.Screen.Send("erase startup-config" + chr(13))
crt.Screen.WaitForString("Erasing")
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("#")
crt.Screen.Send("delete nvram:vlan.dat" + chr(13))
crt.Screen.WaitForString("vlan.dat")
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("confirm")
crt.Screen.Send(chr(13))
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("#")
crt.Screen.Send("reload" + chr(13))
crt.Screen.Send(chr(13))
if initial_4 == True:
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("confirm")
crt.Screen.Send(chr(13))
crt.Screen.Send(chr(13))
if initial_5 == True:
crt.Screen.Send(chr(13))
#
crt.Screen.Send("en" + chr(13))
#
crt.Screen.Send("delete flash:vlan.dat" + chr(13))
#
crt.Screen.Send(chr(13))
crt.Screen.Send(chr(13))
crt.Screen.Send(chr(13))
crt.Screen.WaitForString("#")
#
crt.Screen.Send("config t" + chr(13))
crt.Screen.WaitForString("config")
#
crt.Screen.Send("interface Cellular0/1/0" + chr(13))
crt.Screen.WaitForString("config")
crt.Screen.Send("shut" + chr(13))
crt.Screen.WaitForString("config")
#
crt.Screen.Send("end" + chr(13))
crt.Screen.WaitForString("#")