1. 程式人生 > >使用expect實現shell自動互動

使用expect實現shell自動互動

shell指令碼需要互動的地方可以使用here文件是實現,但是有些命令卻需要使用者手動去就互動如passwd、scp

對自動部署免去使用者互動很痛苦,expect能很好的解決這類問題。

expect的核心是spawn expect send set

spawn 呼叫要執行的命令


expect 等待命令提示資訊的出現,也就是捕捉使用者輸入的提示:


send 傳送需要互動的值,替代了使用者手動輸入內容


set 設定變數值


interact 執行完成後保持互動狀態,把控制權交給控制檯,這個時候就可以手工操作了。如果沒有這一句登入完成後會退出,而不是留在遠端終端上。
expect eof 這個一定要加

,與spawn對應表示捕獲終端輸出資訊終止,類似於if....endif

expect指令碼必須以interact或expect eof結束,執行自動化任務通常expect eof就夠了。

設定expect永不超時
set timeout -1

設定expect 300秒超時,如果超過300沒有expect內容出現,則推出
set timeout 300

expect編寫語法,expect使用的是tcl語法。

一條Tcl命令由空格分割的單片語成. 其中, 第一個單詞是命令名稱, 其餘的是命令引數
cmd arg arg arg

$符號代表變數的值. 在本例中, 變數名稱是foo.
$foo

方括號執行了一個巢狀命令

. 例如, 如果你想傳遞一個命令的結果作為另外一個命令的引數, 那麼你使用這個符號
[cmd arg]

雙引號把片語標記為命令的一個引數. "$"符號和方括號在雙引號內仍被解釋
"some stuff"

大括號也把片語標記為命令的一個引數. 但是, 其他符號在大括號內不被解釋
{some stuff}

反斜線符號是用來引用特殊符號. 例如:n 代表換行. 反斜線符號也被用來關閉"$"符號, 引號,方括號和大括號的特殊含義

expect使用例項

1。首先確認expect的包要安置。

#rpm -qa | grep expect

如果沒有則需要下載安裝,

#yum install expect

2.安裝完成後,檢視expect的路徑,可以用

#which expect

/usr/bin/expect

3.編輯指令碼
#vi autosu.sh
新增如下內容

123456#!/usr/bin/expect  -f   //這個expect的路徑就是用which expect 檢視的結果spawn su-nginx//切換使用者expect"password:"//提示讓輸入密碼send"testr"//輸入nginx的密碼interact//操作完成

4.確定指令碼有可執行許可權

chmod +x autosu.sh

5.執行指令碼 expect autosu.sh 或 ./autosu.sh

expect常用指令碼

登陸到遠端伺服器

1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/expect   settimeout5 setserver[lindex$argv0] setuser[lindex$argv1] setpasswd[lindex$argv2] spawnssh-l$user$server expect{ "(yes/no)"{send"yesr";exp_continue} "password:"{send"$passwdr"} } expect"*Last login*"interact

scp拷貝檔案

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/usr/bin/expect set timeout10 set host[lindex$argv0]//第1個引數,其它2,3,4引數類似 set username[lindex$argv1] set password[lindex$argv2] set src_file[lindex$argv3] set dest_file[lindex$argv4] 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 [email protected]$i "mkdir /root/.ssh/"
   expect "password: "
   send "du77\n"
expect eof
exit
END
expect<<-END
   spawn scp /root/.ssh/id_rsa.pub [email protected]$i:/root/.ssh/id_rsa.pub
   expect "password: "
   send "du77\n"
expect eof
exit
END
expect<<-END
   spawn ssh [email protected]$i "touch /root/.ssh/authorized_keys"
   expect "password: "
   send "du77\n"
expect eof
exit
END
expect<<-END
   spawn ssh [email protected]$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 [email protected]$i:/root/.ssh/id_rsa.pub
   expect "password: "
   send "[email protected]\n"
expect eof
exit
END
expect<<-END
   spawn ssh [email protected]$i "touch /root/.ssh/authorized_keys"
   expect "password: "
   send "[email protected]\n"
expect eof
exit
END
expect<<-END
   spawn ssh [email protected]$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
   expect "password: "
   send "[email protected]\n"
expect eof
exit
END
done