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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Here Document和Expect概述(免交互,变量设定,控制,注释,)(shell里使用EOF报错)

發(fā)布時間:2024/2/28 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Here Document和Expect概述(免交互,变量设定,控制,注释,)(shell里使用EOF报错) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • Here Document概述
      • Here Document使用注意事項
      • Here Document免交互
      • Here Document變量設(shè)定
      • Here Document格式控制
      • Here Document多行注釋
  • Expect概述
  • Expect基本命令
      • 基本命令(expect流程命令)
      • 基本命令(expect內(nèi)容命令)
  • Expect執(zhí)行方式
      • 語法
      • Expect案例(ssh免交互)
      • Expect嵌入執(zhí)行
      • 總結(jié)
  • Expect實操
      • 創(chuàng)建用戶zhangsan,密碼123123
      • SSH登錄實現(xiàn)自動登錄
  • shell里使用EOF報錯
  • 注意事項

Here Document概述

使用I/O重定向的方式將命令列表提供給交互式程序

語法

命令<<標(biāo)記 ... ... 標(biāo)記

Here Document使用注意事項

記可以使用任意合法字符

結(jié)尾的標(biāo)記一定要頂格寫,前面不可以有任何字符

結(jié)尾的標(biāo)記后面也不可以有任何字符

開頭標(biāo)記前后空格會被省略掉

Here Document免交互

read命令接收輸入并打印
ps:賦值只輸出首行內(nèi)容

[root@localhost ~]# vim 1.txt.sh #!/bin/bash read i <<EOF hello world EOF echo $i [root@localhost ~]# chmod +x 1xtx.sh [root@localhost ~]# ./hello.sh hello world

通過passwd給用戶設(shè)置密碼
(要對應(yīng)每一行內(nèi)容)

[root@localhost ~]# vim 2.txt.sh #!/bin/bash passwd zhaosan <<EOF 123abc 123abc EOF [root@localhost ~]# ./2.txt.sh 更改用戶 zhangsan 的密碼 。 新的 密碼:無效的密碼: 密碼少于 7 個字符 重新輸入新的 密碼:passwd:所有的身份驗證令牌已經(jīng)成功更新。

Here Document變量設(shè)定

變量替換
school調(diào)用的是kgc,而school調(diào)用的是kgc,而school調(diào)kgcfilename 調(diào)用的是test.txt

[root@localhost ~]# vim user.sh #!/bin/bash filename="test.txt" school="kgc" cat > $filename <<EOF this is $school .com EOF [root@localhost ~]# bash 1.1.txt.sh [root@localhost ~]# lstest.txt [root@localhost ~]# cat test.txt this is school .com

變量設(shè)定
多行定義給某變量

[root@localhost ~]# vim user.sh #!/bin/bash first="hello" filename=$(cat <<EOF this is cllt.com my name is cllt $first EOF ) echo $filename [root@localhost ~]# ./user.sh this is cllt.com my name is cllt hello

Here Document格式控制

關(guān)閉變量替換功能

[root@localhost ~]# vim user.sh #!/bin/bash first="hello" cat <<'EOF' '單引號關(guān)閉變量替換' this is cllt.com $first EOF [root@localhost ~]# ./user.sh this is cllt.com $first

去除每行之前的tab字符

[root@localhost ~]# vim user.sh #!/bin/bash first="hello" cat <<-EOF '-表示抑制行首的TAB作用'this is cllt.com$first EOF [root@localhost ~]# ./user.sh this is cllt.com hello

Here Document多行注釋

[root@localhost ~]# vim 1.2.txt.sh #!/bin/bash : <<DOING echo "hello" echo "world" DOING cat <<-'EOF'this is$school .com EOF [root@localhost ~]# bash 1.2.txt.sh this is$school .com

Expect概述

1 借助Expect處理交互的命令,可以將交互 過程如:ssh登錄,ftp登錄等寫在一個腳本上,使之自動化完成.尤其適用于需 要對多臺服務(wù)器執(zhí)行相同操作的環(huán)境中,可以大大提高系統(tǒng)管理人員的工作效率

2 主要解決shell腳本中不可交互的問題,對于大規(guī)模的linux運(yùn)維很有幫助,為了模擬這種輸入,可以使用Expect腳本

Expect基本命令

安裝命令

yum install -y expect

基本命令(expect流程命令)

spawn:啟動進(jìn)程,并跟蹤后續(xù)交互信息

send:向進(jìn)程發(fā)送字符串,用于模擬用戶的輸入

該命令不能自動回車換行,一般要加、r(回車)

expect

expect的一個內(nèi)部命令,判斷上次輸出結(jié)果里是否包含指定的字符串,如果有則立即返回,否則就等待超時時間后返回

只能捕捉有spawn啟動的進(jìn)程的輸出

interact:執(zhí)行完成后保持交互狀態(tài),把控制權(quán)交給控制臺

基本命令(expect內(nèi)容命令)

1.Timeout:指定超時時間,過期則繼續(xù)執(zhí)行后續(xù)指令

timeout -1為永不超時

2.exp_continue

允許expect繼續(xù)向下執(zhí)行指令

3.send_user

回顯命令,相當(dāng)于echo

$argv參數(shù)數(shù)組

Expect腳本可以接受從bash傳遞的參數(shù),可以使用[lindex $argc n]獲得,n從0開始,分別表示第一個,第二個,第三個…參數(shù)

arg:參數(shù)

v:value

Expect腳本的結(jié)尾

expect腳本必須以interact或expect eof結(jié)束

Expect執(zhí)行方式

語法

單一分支語法

expect "password:" {send "mypassword\r"}

多分支模式語法第一種

expect "aaa" {send "AAA\r"} expect "aaa" {send "AAA\r"} expect "aaa" {send "AAA\r"} 'send命令不具備回車換行功能,所以需要自己添加\r 或 \n'

多分支模式語法第一種

expect { "aaa" {send "AAA\r"} "bbb" {send "BBB\r"} "ccc" {send "CCC\r"} } '只要匹配了aaa 或bbb或ccc中的任何一個,執(zhí)行相應(yīng)的send語句后就會退出該expect語句'expect { "aaa" {send "AAA\r";exp_continue} "bbb" {send "BBB\r";exp_continue} "ccc" {send "CCC\r"} } 'exp_continue表示繼續(xù)后面的匹配,如果匹配了aaa,執(zhí)行完send語句后還會繼續(xù)向下匹配bbb' '捕捉內(nèi)容要用雙引號引起來' 'send要寫在{}中,輸出信息也要用雙引號引起來,分號“;”要寫在}里面'

Expect案例(ssh免交互)

#!/usr/bin/expect #超時時間 set timeout 20 #開啟日志 log_file test.log #顯示信息 log_user 1 # 定義變量 set hostname [lindex $argv 0] set password [lindex $argv 1]#追蹤指令 spawn ssh root@${hostname}#捕捉提示信息 expect {"connecting (yes/no)"{send "yes\r";exp_continue}"*password:"{send "${password}\r";} } #轉(zhuǎn)交控制權(quán) interact[root@localhost opt]# ./3.txt.sh 192.168.136.128 123123 spawn ssh root@192.168.136.128 The authenticity of host '192.168.136.128 (192.168.136.128)' can't be established. ECDSA key fingerprint is SHA256:LqCIyvQfZhPr2125mmYUv+SzjIL9tC2wGdQA0CC4NMg. ECDSA key fingerprint is MD5:c4:27:63:34:5f:b9:43:92:3a:84:2b:f8:35:fa:17:6e. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.136.128' (ECDSA) to the list of known hosts. root@192.168.136.128's password: Last login: Tue Jul 28 09:19:34 2020 from 192.168.136.2 [root@zhang ~]# exit 登出

Expect嵌入執(zhí)行

[root@localhost opt]# vim 4.txt.sh #!/bin/bash hostname=$1 password=$2 #expect嵌入式 /usr/bin/expect <<-EOF spawn ssh root@${hostname} #捕捉提示信息 expect { "connecting (yes/no)"{send "yes\r";exp_continue}"*password:"{send "${password}\r";} } expect "*]#" send "exit\r" expect eof EOF [root@localhost opt]# ./4.txt.sh 192.168.136.128 123123 spawn ssh root@192.168.136.128 root@192.168.136.128's password: Last login: Tue Jul 28 09:33:16 2020 from 192.168.136.50 [root@wangwu ~]# exit 登出 Connection to 192.168.136.128 closed.

總結(jié)

#!/usr/bin/expect -re 告訴操作系統(tǒng)腳本里的代碼使用那一個shell來執(zhí) 2. set timeout -1 設(shè)置超時時長 -1 代表永不過期,默認(rèn)時10秒 3. exp_continue 表示循環(huán)匹配。匹配到改關(guān)鍵字后繼續(xù)從頭開始匹配。若不加exp_continue則會直接退出 4. expect eof 匹配結(jié)尾 例如執(zhí)行命令結(jié)束時則可以匹配到 eof 5. exit、interact exit交互結(jié)束退出 interact表示執(zhí)行完后保持交互狀態(tài),把控制權(quán)交給控制臺,這個時候就可以手工操作了 6. exp_send/send 都是想程序發(fā)送字符串,有啥區(qū)別還沒找到 7. send_user send_user 命令用來把后面的參數(shù)輸出到標(biāo)準(zhǔn)輸出中去,默人的send、exp_send 命令都是將參數(shù)輸出到程序中去的, 8.如何使用 mac 上直接使用 ./XX.sh 執(zhí)行上述腳本不行。需要使用 expect XX.sh 才能正確執(zhí)行 9.調(diào)試 expect -d XX.sh輸出每次執(zhí)行的過程可以用于編寫腳本時調(diào)試之用

Expect實操

創(chuàng)建用戶zhangsan,密碼123123

使用嵌入式腳本創(chuàng)建
spawn追蹤命令
\r:回車

#!/bin/bash user=$1 password=$2 useradd $user expect <<-EOF spawn passwd $user expect "New password:"{send "$password\r";}expect "Retype new password:"{send "$password\r"}expect eof; EOF [root@client ~]# ./a.sh zhang 123123 '執(zhí)行腳本' spawn passwd qwer Changing password for user qwer. New password: BAD PASSWORD: The password contains the user name in some form Retype new password: passwd: all authentication tokens updated successfully.

SSH登錄實現(xiàn)自動登錄

1.首次登錄

2.正常登錄

3.連接被拒絕,可能是ssh沒開,或者端口不對,或者防火墻限制

4.腳本內(nèi)容

#!/bin/bash hostname=$1 password=$2 /usr/bin/expect<<-EOFspawn ssh $hostnameexpect {"Connection refused" exit"Name or service not known" exit"to continue" {send "yes\r";exp_continue}"password:" {send "$password\r"} } expect "*]#" send "exit\r" expect eof EOF

首次登錄

[root@localhost opt]# vim 7.txt.sh [root@localhost opt]# chmod +x 7.txt.sh [root@localhost opt]# ./7.txt.sh 192.168.136.128 123123 spawn ssh 192.168.136.128 root@192.168.136.128's password: Last login: Tue Jul 28 09:35:26 2020 from 192.168.136.50 [root@wangwu ~]# exit 登出 Connection to 192.168.136.128 closed.

正常登錄

[root@client ~]# ./c.sh 192.168.197.141 123123 spawn ssh 192.168.197.141 root@192.168.197.141's password: Last login: Wed Dec 4 15:37:46 2019 from 192.168.197.139 [root@localhost ~]# exit logout Connection to 192.168.197.141 closed.

shell里使用EOF報錯

shell里使用EOF報錯

1.第一個EOF必須以重定向字符<<開始,第二個EOF必須頂格寫,否則會報錯。EOF后面不可以有空格

2.最好用vim編輯

3.chmod 權(quán)限用sh會報錯

4.yum -y install expect

68.136.128 closed.

正常登錄[root@client ~]# ./c.sh 192.168.197.141 123123 spawn ssh 192.168.197.141 root@192.168.197.141's password: Last login: Wed Dec 4 15:37:46 2019 from 192.168.197.139 [root@localhost ~]# exit logout Connection to 192.168.197.141 closed.

注意事項

shell里使用EOF報錯

1.第一個EOF必須以重定向字符<<開始,第二個EOF必須頂格寫,否則會報錯。EOF后面不可以有空格

2.最好用vim編輯

3.chmod 權(quán)限用sh會報錯

總結(jié)

以上是生活随笔為你收集整理的Here Document和Expect概述(免交互,变量设定,控制,注释,)(shell里使用EOF报错)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。