因需要管理大量的同品牌交换机,一直想找一个批量配置管理交换机的方法,终于找到并测试成功了:
系统:centos 7
安装tftp-server ,从下载empty-0.6.19b,(最新版的0.6.20b测试不能使用)
此empty 可以替代expect,且易于编译安装。
按照empty里面的安装说明make && make install
以h3c交换机为例,创建批量配置交换机脚本
vi configh3c.sh
内容如下:
#!/bin/sh
# emssh.sh v.0.5
# ssh example for empty
# Though, don't really know who wants to use empty for ssh :)
# Copyright (C) 2005, 2006 Mikhail E. Zakharov
#
pkill -9 empty
rm -f /tmp/empty.*
folder=logs
if [ ! -x "$folder" ]; then
mkdir "$folder"
fi
read -p "Enter connection type--1:telnet 2:ssh(default) please input 1 or 2 :" connectype
if [ ! -n "$connectype" ]; then #
connectype=2
else
connectype=$connectype
fi
read -p "Enter switch username:" login
stty -echo
read -p "Enter switch password:" password
stty echo
while read ip
do
day=`date +%Y%m%d`
telnet="telnet"
ssh="ssh" # (/full/path/to/)ssh
target="$ip" # target host
#login="admin" # username (Change it!)
#password="lgadminzt" # password (Change it!)
fifo_in="/tmp/empty.in" # input fifo
fifo_out="/tmp/empty.out" # output
# -----------------------------------------------------------------------------
telnetcmd="$telnet $target"
sshcmd="$ssh $login@$target"
tmp="/tmp/empty.tmp" # tempfile to store results
echo "Starting empty"
if [ $connectype = 1 ]; then
empty -f -L $tmp -i $fifo_in -o $fifo_out $telnetcmd
else
empty -f -L $tmp -i $fifo_in -o $fifo_out $sshcmd
fi
if [ $connectype != 1 ]; then
sleep 7 #wait the RSA key
if cat $tmp | grep -q -i "(yes/no)"
then
empty -s -o $fifo_in "yes\n"
fi
fi
if [ $? = 0 ]; then
if [ -w $fifo_in -a -r $fifo_out ]; then
if [ $connectype = 1 ]; then
echo "Sending Username"
empty -w -v -i $fifo_out -o $fifo_in -t 2 sername: "$login\n"
sleep 2
fi
echo "Sending Password"
empty -w -v -i $fifo_out -o $fifo_in -t 2 assword: "$password\n"
empty -w -v -i $fifo_out -o $fifo_in -t 2 \> "clock timezone beijing add 8\n"
sleep 1
empty -w -v -i $fifo_out -o $fifo_in -t 2 \> " \n"
sleep 2
empty -w -v -i $fifo_out -o $fifo_in -t 1 \> "sys \n"
empty -w -v -i $fifo_out -o $fifo_in -t 1 \] "ntp-service unicast-server 172.23.142.2 \n"
empty -w -v -i $fifo_out -o $fifo_in -t 1 \] "igmp-snooping enable\n"
empty -w -v -i $fifo_out -o $fifo_in -t 5 \] "save\n"
sleep 2
empty -w -v -i $fifo_out -o $fifo_in -t 3 \] "y\n"
sleep 2
empty -w -v -i $fifo_out -o $fifo_in -t 3 \: "\n"
sleep 10
empty -w -v -i $fifo_out -o $fifo_in -t 3 \] "quit\n"
empty -w -v -i $fifo_out -o $fifo_in -t 5 \> "quit\n"
sleep 1
sed -i '/password\:/d' $tmp
cp $tmp logs/$ip-$day.log
sleep 2
pkill -9 empty
rm -f /tmp/empty.*
else
echo "Error: Can't find I/O fifos!"
return 1
fi
else
echo "Error: Can't start empty in daemon mode"
return 1
fi
echo "Done"
done
创建ip.txt文件,写入要备份交换机的管理IP,一行一个IP
内容示例如下:
192.168.10.10
192.168.10.21
192.168.10.22
执行sh configh3c.sh 按照提示输入使用Telnet或者ssh连接、用户名、密码即可批量配置交换机了
本脚本h3c交换机测试通过,每分钟可以配置5台交换机。
阅读(7219) | 评论(0) | 转发(0) |