Here Document和Expect概述(免交互,变量设定,控制,注释,)(shell里使用EOF报错)
文章目錄
- Here Document概述
- Here Document使用注意事項
- Here Document免交互
- Here Document變量設定
- Here Document格式控制
- Here Document多行注釋
- Expect概述
- Expect基本命令
- 基本命令(expect流程命令)
- 基本命令(expect內容命令)
- Expect執行方式
- 語法
- Expect案例(ssh免交互)
- Expect嵌入執行
- 總結
- Expect實操
- 創建用戶zhangsan,密碼123123
- SSH登錄實現自動登錄
- shell里使用EOF報錯
- 注意事項
Here Document概述
使用I/O重定向的方式將命令列表提供給交互式程序
語法
命令<<標記 ... ... 標記Here Document使用注意事項
記可以使用任意合法字符
結尾的標記一定要頂格寫,前面不可以有任何字符
結尾的標記后面也不可以有任何字符
開頭標記前后空格會被省略掉
Here Document免交互
read命令接收輸入并打印
ps:賦值只輸出首行內容
通過passwd給用戶設置密碼
(要對應每一行內容)
Here Document變量設定
變量替換
school調用的是kgc,而school調用的是kgc,而school調用的是kgc,而filename 調用的是test.txt
變量設定
多行定義給某變量
Here Document格式控制
關閉變量替換功能
[root@localhost ~]# vim user.sh #!/bin/bash first="hello" cat <<'EOF' '單引號關閉變量替換' 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 helloHere 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 .comExpect概述
1 借助Expect處理交互的命令,可以將交互 過程如:ssh登錄,ftp登錄等寫在一個腳本上,使之自動化完成.尤其適用于需 要對多臺服務器執行相同操作的環境中,可以大大提高系統管理人員的工作效率
2 主要解決shell腳本中不可交互的問題,對于大規模的linux運維很有幫助,為了模擬這種輸入,可以使用Expect腳本
Expect基本命令
安裝命令
yum install -y expect基本命令(expect流程命令)
spawn:啟動進程,并跟蹤后續交互信息
send:向進程發送字符串,用于模擬用戶的輸入
該命令不能自動回車換行,一般要加、r(回車)
expect
expect的一個內部命令,判斷上次輸出結果里是否包含指定的字符串,如果有則立即返回,否則就等待超時時間后返回
只能捕捉有spawn啟動的進程的輸出
interact:執行完成后保持交互狀態,把控制權交給控制臺
基本命令(expect內容命令)
1.Timeout:指定超時時間,過期則繼續執行后續指令
timeout -1為永不超時
2.exp_continue
允許expect繼續向下執行指令
3.send_user
回顯命令,相當于echo
$argv參數數組
Expect腳本可以接受從bash傳遞的參數,可以使用[lindex $argc n]獲得,n從0開始,分別表示第一個,第二個,第三個…參數
arg:參數
v:value
Expect腳本的結尾
expect腳本必須以interact或expect eof結束
Expect執行方式
語法
單一分支語法
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中的任何一個,執行相應的send語句后就會退出該expect語句'expect { "aaa" {send "AAA\r";exp_continue} "bbb" {send "BBB\r";exp_continue} "ccc" {send "CCC\r"} } 'exp_continue表示繼續后面的匹配,如果匹配了aaa,執行完send語句后還會繼續向下匹配bbb' '捕捉內容要用雙引號引起來' '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";} } #轉交控制權 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嵌入執行
[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.總結
#!/usr/bin/expect -re 告訴操作系統腳本里的代碼使用那一個shell來執 2. set timeout -1 設置超時時長 -1 代表永不過期,默認時10秒 3. exp_continue 表示循環匹配。匹配到改關鍵字后繼續從頭開始匹配。若不加exp_continue則會直接退出 4. expect eof 匹配結尾 例如執行命令結束時則可以匹配到 eof 5. exit、interact exit交互結束退出 interact表示執行完后保持交互狀態,把控制權交給控制臺,這個時候就可以手工操作了 6. exp_send/send 都是想程序發送字符串,有啥區別還沒找到 7. send_user send_user 命令用來把后面的參數輸出到標準輸出中去,默人的send、exp_send 命令都是將參數輸出到程序中去的, 8.如何使用 mac 上直接使用 ./XX.sh 執行上述腳本不行。需要使用 expect XX.sh 才能正確執行 9.調試 expect -d XX.sh輸出每次執行的過程可以用于編寫腳本時調試之用Expect實操
創建用戶zhangsan,密碼123123
使用嵌入式腳本創建
spawn追蹤命令
\r:回車
SSH登錄實現自動登錄
1.首次登錄
2.正常登錄
3.連接被拒絕,可能是ssh沒開,或者端口不對,或者防火墻限制
4.腳本內容
#!/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 權限用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 權限用sh會報錯
總結
以上是生活随笔為你收集整理的Here Document和Expect概述(免交互,变量设定,控制,注释,)(shell里使用EOF报错)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存选择攻略:IT爱好者分享傲腾系列经验
- 下一篇: i3 530内存:速度稳定又容量大,电脑