How to use expect script to realize backup Huawei Switch configuration (ii)

Source: Internet
Author: User
Tags execution zip backup

The script consists of four files, which are as follows, the execution is tar.sh->loop.sh->loginfo.exp, and the relationship is called in turn. The script completes log in sequentially to each switch and executes the "TFTP 192.168.36.44 put vrpcfg.zip" command on each switch and then changes the saved profile to the appropriate switch IP address. Finally, the file is packaged as a file with the day date to complete all operations. Where SH is the shell script,. Exp is the expect script, the expect script only completes the function of the login execution command, while the shell script only handles the saved file, is it clear? Oh

1. Loginfo.exp

#!/usr/bin/expect #注意这是用expect来执行脚本, not shell

Set IP [lindex $argv 0] #传递来自shell脚本的变量 $ip value

Spawn ssh-v -1-c des-q-o stricthostkeychecking=no wang11@game-inc.com@ $ip #ssh登录这里输入ssh帐号

Expect "*password:" #expect是一个命令, if Match "*password:" then continue to execute

Send "p@ssw0rd\r" #交换机密码, \ R for input

Expect "*>" #若成功匹配 "*>" continues to execute

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Network/jhjs/

Send "tftp 192.168.36.44 put vrpcfg.zip\r" #在交换机上执行命令, 192.168.36.44 is the address of the TFTP server

Expect "*>" #匹配则继续

Send "quit\r" #登出交换机

Expect EOF #expect脚本执行结束

2. loop.sh

#!/bin/sh

While read IP

Todo

Expect Loginfo.exp $ip #调用loginfo. Exp Script

mv/opt/tftp/vrpcfg.zip/opt/tftp/$ip. zip #将tftp上的配置文件改名为相应的IP地址

Done < Ip.txt

3. tar.sh

#!/bin/sh

/bin/sh loop.sh

Cd/opt/tftp

TAR-ZCVF $ (Date +%y%m%d). tar.gz *.zip #整个循环结束后将所有文件打包

Rm-rf/opt/tftp/*.zip

4. Ip.txt

192.168.36.1

192.168.36.2

......

192.168.36.254

All of the above scripts can be executed in the same directory

[Root@localhost opt]# sh-x tar.sh

To be Continued ~

This article from the "Wang 11" blog, please be sure to retain this source http://wangshiyi.blog.51cto.com/703685/1122163

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.