使用expect和cvs来给华为设备做配置比较
以前一只用rancid来对cisco的设备做配置比较,但显然rancid对华为不太友好,一直就不支持华为设备(包括h3)。因此呢,由于本人还有点小聪明,知道rancid是考expect和cvs来实现的,于是乎自己写了些比较笨的脚本来给华为的设备做配置比较。
1)去华为设备抓配置
#!/usr/local/bin/expect
spawn telnet x.x.x.x
expect "sername:"
send "xxxx\n"
expect "assword:"
send "xxxx\n"
expect ">"
send "su\n"
expect "assword:"
send "xxxx\n"
expect ">"
send "dis cu\n"
log_file /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
while (1) {
expect {
"More ----" {send " "}
"1>" {
send "quit\n"
exit
}
}
}
2)把配置中的换行符和敏感信息去掉
cd /tmp/rancid.tmp
rm /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
/home/rancid/huawei/d-cp-ne20-1.sh
cd /tmp/rancid.tmp/Distribution/configs
tr -d '\r' < /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/\ \ ----\ More.\{57\}//g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/d\ cipher.\{25\}/d\ cipher\ ******/g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/\ simple.*/\ simple\ ******/g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/local-user\ .*\ /\local-user ***** \ /g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
3)把配置加入rancid的cvs中
cvs add d-cp-ne20-1
cvs commit -m updates
4)把以下内容做成一个文件加入cron就ok了
cd /tmp/rancid.tmp
rm /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
/home/rancid/huawei/d-cp-ne20-1.sh
cd /tmp/rancid.tmp/Distribution/configs
tr -d '\r' < /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/\ \ ----\ More.\{57\}//g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/d\ cipher.\{25\}/d\ cipher\ ******/g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/\ simple.*/\ simple\ ******/g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
sed -e 's/local-user\ .*\ /\local-user ***** \ /g' /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1 > /tmp/rancid.tmp/d-cp-ne20-1.tmp
mv /tmp/rancid.tmp/d-cp-ne20-1.tmp /tmp/rancid.tmp/Distribution/configs/d-cp-ne20-1
cvs commit -m updates > /dev/null
btw:关于cvs在web上的显示,我使用的上viewvc,感觉比cvsweb要好哦。还有,不要说俺的脚本写的很烂,俺是学网络的,脚本这东西,现学现用,凑合了。