本人工作需要经常配置出厂模式的防火墙,每次GUI上重复劳动。所以利用 python 的 expect 实现 pexpect 编写自动配置脚本,是通过 CLI 交互的。
此脚本是一开始配置 WAN 口的,因为此时没有配置 WAN IP,只能通过 console (实际 test bed 用的是 telnet )进行配置。以后的脚本直接通过
ssh 连接到 WAN 口。
#!/usr/local/bin/python import pexpect import time import sys #get port number ipDict = {'10.0.0.66':'2029', '10.0.0.25':'2011', '10.0.0.20':'2043', '10.0.0.61':'2022', '10.0.0.64':'2025'} usage = '''Usage: command WANIP e.g. ./cfgWANIP.py 10.0.0.25''' while True: if len(sys.argv) == 1: print usage sys.exit() elif sys.argv[1] == '-h' or sys.argv[1] == '--help': print usage sys.exit() elif len(sys.argv) != 2: print usage sys.exit() else: break #telnet login (console login) wanip = sys.argv[1] port = ipDict[wanip] child = pexpect.spawn("telnet 10.103.64.8 %s" % port) child.logfile = sys.stdout child.expect("login:") child.sendline("admin") child.expect("Password:") child.sendline("password") time.sleep(1) child.sendline("") while True: index = child.expect(["User", "admin@[A-Z0-9]{12}>"]) if index == 0: child.sendline("admin") child.expect("Password:") child.sendline("password") child.expect("admin@[A-Z0-9]{12}>") child.sendline("configure terminal") break elif index == 1: child.sendline("configure terminal") break else: print "Program error! Exit." sys.exit() # login to UTM console, starting to configure WAN IP child.expect("config([A-Z0-9]{12})#") child.sendline("interface X1") child.expect("(edit-interface[X1])#") child.sendline("ip-assignment WAN static") child.expect("(edit-WAN-static[X1])#") child.sendline("ip %s netmask 255.255.255.0" % wanip) child.expect("(edit-WAN-static[X1])#") child.sendline("gateway 10.0.0.1") child.expect("(edit-WAN-static[X1])#") child.sendline("dns primary 10.217.131.101") child.expect("(edit-WAN-static[X1])#") child.sendline("exit") child.expect("(edit-interface[X1])#") child.sendline("management https") child.expect("(edit-interface[X1])#") child.sendline("management ssh") child.expect("(edit-interface[X1])#") child.sendline("management ping") child.expect("(edit-interface[X1])#") child.sendline("commit") time.sleep(2) child.close()