linux expect的用法

来源:互联网 发布:黑暗网络进入方法 编辑:程序博客网 时间:2024/06/07 02:16
#!/usr/bin/expect


set hostip [lindex $argv 0]
set fbasepasswd [lindex $argv 1]
set pubpasswd [lindex $argv 2]
set homspasswd [lindex $argv 3]


#puts stdout $hostip
#puts stdout $fbasepasswd
#puts stdout $pubpasswd
#puts stdout $homspasswd


if { $argc != 4 } {
  puts stderr "cres update fail"
  exit 1
}


#备份fbase
spawn ssh fbase@$hostip
expect {
 "yes/no" { send "yes\r";exp_continue }
 "fbase@$hostip's password:" { send "$fbasepasswd\r" }
}
expect "]*"
send "rm -rf fbase\r"
send "mkdir /home/fbase/fbase2_backup\r"
send "cp -avx /home/fbase/workspace /home/fbase/fbase2_backup/\$(date +%Y%m%d%H%S)/\r"
send "cp -avx /home/fbase/appcom /home/fbase/fbase2_backup/\$(date +%Y%m%d%H%S)/\r"
send "cp -avx /home/fbase/install_fbase.sh /home/fbase/fbase2_backup/\$(date +%Y%m%d%H%S)/\r"
send "exit\r"
expect eof


#上传文件


#上传fbase目录
spawn scp -r /root/fbase/ fbase@$hostip:/home/fbase
expect {
 "yes/no" { send "yes\r";exp_continue }
 "fbase@$hostip's password:" { send "$fbasepasswd\r" }
}
send "exit\r"
expect eof

#更新fbase
spawn ssh fbase@$hostip
expect {
 "yes/no" { send "yes\r";exp_continue }
 "fbase@$hostip's password:" { send "$fbasepasswd\r" }
}
expect "]*"
send "rm -rf ~/workspace\r"
send "rm -rf ~/appcom\r"
send "mv -f ~/fbase/* ~/\r"
send "chmod +x install_fbase.sh\r"
send "./install_fbase.sh\r"

expect "Y/N"

send "y\r"


expect "source .bash_profile"
send "source .bash_profile\r"
send "exit\r"
expect eof

0 0
原创粉丝点击