改进
1、以前的代码比较适合交换机少的情况,一个交换机写一个代码,现在有70多台交换机,把交换机的信息导入sql数据库,然后用一段代码写完。(数据库真的很好用,导入修改都很方便,以后新增交换机,修改数据库就可以了)
2、生成的excel文档,行数太多,根据楼层,分层多个页,每页就不会有很多行。
代码
抓取交换机信息代码,改进。
import pexpect
import sys
import datetime
import os
import pymssql
today=datetime.date.today().strftime('%Y%m%d')
path = "/root/F5/"+today
os.mkdir(path,777)
passwd='xxx'
def Switch(S,I,N):#循环找到数据库每行里面对应的数据,交换机的名字、IP等。
try:#遇到错误信息,直接掠过,防止因一个交换机信息有误,导致程序不能继续进行。
name1="---- More ----"
child=pexpect.spawn('telnet %s'%I)#循环telnetIP地址
fout=open('/root/F5/'+today+'/'+'%s-%s.txt'%(S,I),'wb+')
child.logfile = fout
child.expect('Username:')
child.sendline("admin")
child.expect('(?i)ssword:')
child.sendline("%s"%passwd)
child.expect('<%s>'%N)
child.sendline("dis mac-address")
for i in range(10):
index = child.expect([name1,'<%s>'%N])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("quit")
break#不能用sys.exit()
except:
pass
host = 'x.x.x.x'
user = 'sa'
pwd = 'xxxxxx'
db = 'MAC'
conn = pymssql.connect(host=host,user=user,password=pwd,database=db,timeout=1,login_timeout=1,charset="utf8")
cur = conn.cursor()
sqls ="select * from [dbo].[F5Switch]"
cur.execute(sqls)
listall = cur.fetchall()
for