linux except 批量,LINUX EXPECT比较经典的批量化操作脚本

#!/usr/bin/expect -f

###################################################

# Script Function :Backup batch for oracle

# Auther: Zhao Hai

# Date: 20160209

# Usage:

#    1. Input "ip,dba_user,password,db_user,password" information to the configuration file named "SERVERLIST".

#       example: 10.8.190.24 oracle oracle dbuser dbuserpasswd

# 2. run command "expect oracle_backup.exp"

###################################################

###################################################

# Set the timeout for expect operation.

# Set date variable for dump file name and log name usage.

# Set SERVERLIST variable as configuration file name.

###################################################

set timeout 6000

set DATE [exec date "+%Y%m%d"]

set SERVERLIST [open SERVERLIST r]

###################################################

# main()

###################################################

while { [gets $SERVERLIST LINE]>=0 } {

set ADR [lindex $LINE 0]

set DBA [lindex $LINE 1]

set DBAPASS [lindex $LINE 2]

set USR [lindex $LINE 3]

set USRPASS [lindex $LINE 4]

spawn ssh $DBA@$ADR

expect {

"yes/no" {send "yes\r"; exp_continue}

"Password:" {send "$DBAPASS\r"}

"password:" {send "$DBAPASS\r"}

}

# Make dump directory for oracle backup job.

expect "oracle@"

send "mkdir /tmp/db-backup\r"

expect "oracle@"

send "sqlplus / as sysdba;\r"

expect "SQL>"

send "create directory ora_dump as '/tmp/db-backup'\r"

expect "SQL>"

send "grant read,write on directory ora_dump to public\r"

expect "SQL>"

send "quit\r"

# Make dump file for oracle db.

expect "oracle@"

send "export NLS_LANG=AMERICAN_AMERICA.ZHS32GB18030\r"

expect "oracle@"

send "expdp $USR/$USRPASS schemas=$USR dumpfile=ora_dump:$USR.dump.$DATE logfile=ora_dump:$USR.log.$DATE\r"

expect "oracle@"

send "exit\r"

send "exit\r"

}

close $SERVERLIST

expect eof

exit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值