在前东家的时候,A 机房出口偶发会挂掉,需要切换到备机房B-IDC 出口。全自动化脚本是不可能的,只能半自动化脚本进行切换。基本上就是在核心上把A机房的路由干掉,转化指向B机房。如果A机房出口恢复再反向切换回来。A机房使用的是华为核心交换、B机房使用的是思科的核心交换,比较有代表性。脚本做过实际测试,并且在大促的时候成功的切换过流量,减少了损失。个人感觉还是非常有价值的一件事情 。现在把脚本分享出来仅供语法格式参考,所有ip已做更改。
#! /usr/bin/env python

import pexpect
import sys

child = pexpect.spawn('telnet 10.1.1.250')  
fout = file('huawei.txt','w')
child.logfile = fout

child.expect("Username:")
child.sendline('admin')
child.expect('Password:')
child.sendline('密码')
child.expect('>')

# for test
child.sendline('sys')
child.expect(']')
child.sendline('')

#child.sendline('sys')
child.expect(']')
child.sendline('undo ip route-static 0.0.0.0 0.0.0.0 10.11.12.254')
child.expect(']')
child.sendline('ip route-static 0.0.0.0 0.0.0.0 172.32.4.2')
child.expect(']')
child.sendline('ip route-static 0.0.0.0 0.0.0.0 172.32.5.2')
child.expect(']')
child.sendline('quit')

print "YJ-IDC changed finished"

child = pexpect.spawn('telnet 10.12.2.1')
fout = file('cisco.txt','w')
child.logfile = fout

child.expect("Username:")
child.sendline('admin')
child.expect('Password:')
child.sendline('密码')
child.expect('>')
child.sendline('en')
child.expect('Password:')
child.sendline('密码')
child.expect('#')
child.sendline('conf t')
child.expect('#')
child.sendline('')
child.expect('#')
child.sendline('')
child.expect('#')
child.sendline('ip route 172.17.0.0 255.255.0.0 172.32.2.1')
child.expect('#')
child.sendline('ip route 172.17.0.0 255.255.0.0 172.32.3.1')
child.expect('#')
child.sendline('ip route 0.0.0.0 0.0.0.0 10.1.1.254')
child.expect('#')
child.sendline('no ip route 0.0.0.0 0.0.0.0 172.32.2.1')
child.expect('#')
child.sendline('no ip route 0.0.0.0 0.0.0.0 172.32.3.1')
child.expect('#')
child.sendline('exit')
child.expect('#')
child.sendline('exit')
print "ALL OF changed outside route finished"