shell腳本需要交互的地方可以使用here文檔是實(shí)現(xiàn),但是有些命令卻需要用戶手動(dòng)去就交互如passwd灭抑、scp
對(duì)自動(dòng)部署免去用戶交互很痛苦,expect能很好的解決這類問(wèn)題斟薇。
expect的核心是spawn expect send set
spawn 調(diào)用要執(zhí)行的命令
expect 等待命令提示信息的出現(xiàn)嫉柴,也就是捕捉用戶輸入的提示:
send 發(fā)送需要交互的值,替代了用戶手動(dòng)輸入內(nèi)容
set 設(shè)置變量值
interact 執(zhí)行完成后保持交互狀態(tài)武通,把控制權(quán)交給控制臺(tái)霹崎,這個(gè)時(shí)候就可以手工操作了。如果沒(méi)有這一句登錄完成后會(huì)退出冶忱,而不是留在遠(yuǎn)程終端上尾菇。
expect eof 這個(gè)一定要加,與spawn對(duì)應(yīng)表示捕獲終端輸出信息終止囚枪,類似于if....endif
expect腳本必須以interact或expect eof結(jié)束派诬,執(zhí)行自動(dòng)化任務(wù)通常expect eof就夠了。
設(shè)置expect永不超時(shí)
set timeout -1
設(shè)置expect 300秒超時(shí)链沼,如果超過(guò)300沒(méi)有expect內(nèi)容出現(xiàn)默赂,則推出
set timeout 300
expect編寫語(yǔ)法,expect使用的是tcl語(yǔ)法括勺。
一條Tcl命令由空格分割的單詞組成. 其中, 第一個(gè)單詞是命令名稱, 其余的是命令參數(shù)
cmd arg arg arg
foo
方括號(hào)執(zhí)行了一個(gè)嵌套命令. 例如, 如果你想傳遞一個(gè)命令的結(jié)果作為另外一個(gè)命令的參數(shù), 那么你使用這個(gè)符號(hào)
[cmd arg]
雙引號(hào)把詞組標(biāo)記為命令的一個(gè)參數(shù). ""符號(hào), 引號(hào),方括號(hào)和大括號(hào)的特殊含義
expect使用實(shí)例
1缆八。首先確認(rèn)expect的包要安置。
rpm -qa | grep expect
如果沒(méi)有則需要下載安裝疾捍,
yum install expect
2.安裝完成后奈辰,查看expect的路徑,可以用
which expect
/usr/bin/expect
3.編輯腳本
vi test.sh
添加如下內(nèi)容
#!/usr/bin/expect -f //這個(gè)expect的路徑就是用which expect 查看的結(jié)果
spawn su - nginx //切換用戶
expect "password:" //提示讓輸入密碼
send "testr" //輸入nginx的密碼
interact //操作完成
4.確定腳本有可執(zhí)行權(quán)限
chmod +x autosu.sh
5.執(zhí)行腳本 expect autosu.sh 或 ./autosu.sh
expect常用腳本
登陸到遠(yuǎn)程服務(wù)器:
#!/usr/bin/expect
set timeout 5
set server [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh -l $user $server
expect {
"(yes/no)" { send "yesr"; exp_continue }
"password:" { send "$passwdr" }
}
expect "*Last login*" interact
scp拷貝文件
#!/usr/bin/expect
set timeout 10
set host [lindex $argv 0] //第1個(gè)參數(shù)乱豆,其它2,3,4參數(shù)類似
set username [lindex $argv 1]
set password [lindex $argv 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn scp $src_file $username@$host:$dest_file
expect {
"(yes/no)?"
{
send "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:"
{
send "$passwordn"
}
}
expect "100%"
expect eof
#!/bin/bash
set timeout 30
expect<<-END
spawn ssh-keygen -t rsa
expect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send "\n"
expect "Overwrite (y/n)? "
send "\n"
expect eof
exit
END
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
echo $i
expect<<-END
spawn ssh root@$i "mkdir /root/.ssh/"
expect "password: "
send "du77\n"
expect eof
exit
END
expect<<-END
spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
expect "password: "
send "du77\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "touch /root/.ssh/authorized_keys"
expect "password: "
send "du77\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
expect "password: "
send "du77\n"
expect eof
exit
END
done
#!/bin/bash
set timeout 30
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
echo $i
expect<<-END
spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
expect "password: "
send "123@123\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "touch /root/.ssh/authorized_keys"
expect "password: "
send "123@123\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
expect "password: "
send "123@123\n"
expect eof
exit
END
done