日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

expect返回值给shell_使用expect实现shell自动交互

發布時間:2024/4/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

方括號執行了一個嵌套命令. 例如, 如果你想傳遞一個命令的結果作為另外一個命令的參數, 那么你使用這個符號

[cmd arg]

雙引號把詞組標記為命令的一個參數. "

"符號, 引號,方括號和大括號的特殊含義

expect使用實例

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

rpm -qa | grep expect

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

yum install expect

2.安裝完成后,查看expect的路徑,可以用

which expect

/usr/bin/expect

3.編輯腳本

vi test.sh

添加如下內容

#!/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常用腳本

登陸到遠程服務器:

#!/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個參數,其它2,3,4參數類似

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<

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<

spawn ssh root@$i "mkdir /root/.ssh/"

expect "password: "

send "du77\n"

expect eof

exit

END

expect<

spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub

expect "password: "

send "du77\n"

expect eof

exit

END

expect<

spawn ssh root@$i "touch /root/.ssh/authorized_keys"

expect "password: "

send "du77\n"

expect eof

exit

END

expect<

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<

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<

spawn ssh root@$i "touch /root/.ssh/authorized_keys"

expect "password: "

send "123@123\n"

expect eof

exit

END

expect<

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

總結

以上是生活随笔為你收集整理的expect返回值给shell_使用expect实现shell自动交互的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。