tft每日頭條

 > 生活

 > expect設置

expect設置

生活 更新时间:2024-10-05 18:19:16

expect設置?#shell編程##linux##自動化##簡介 ,下面我們就來說一說關于expect設置?我們一起去了解并探讨一下這個問題吧!

expect設置(詳解expect自動交互命令)1

expect設置

#shell編程##linux##自動化#

#簡介

expect是一個用來實現自動交互功能的軟件套件,是基于TCL的腳本編程工具語言,方便學習,功能強大

#擴展TCL:全拼為Tool Command Language ,是一種腳本語言,由John Ousterout創建。TCL功能很強大,經常被用于快速原型開發,腳本編程,GUI和測試等方面

#使用背景

在執行系統命令或程序時,有些系統會以交互式的形式要求輸出指定的字符串之後才能執行命令,如用戶設置密碼,一般都是需要手工輸入2次密碼,再如SSH登錄的,如果沒有做免密鑰登錄,第一次連接要和系統實現兩次交互式輸入

#安裝

yum install expect

#自動交互工作流程

spawn啟動指定進程--->expect獲取期待的關鍵字-->send向指定進程發送指定字符-->進程執行完畢,退出結束

#相關命令

1.spawn命令

在expect自動交互程序執行的過程中,spawn命令是一開始就需要使用的命令。通過spawn執行一個命令或程序,之後所有的expect操作都會在這個執行過的命令或程序進程中進行,包括自動交互功能,因此如果沒有spawn命令,expect程序将會無法實現自動交互

#語法

spawn [選項] [需要自動交互的命令或程序]

#示例

spawn ssh [email protected] uptime

#在spawn命令的後面,直接加上要執行的命令或程序(例如這裡的ssh命令)等,除此之外,spawn還支持如下一些選項

-open: 表示啟動文件進程

-ignore:表示忽略某些信号

#提示:使用spawn命令expect程序實現自動 交互工作流程的第一步,也是最關鍵的一步

2.expect命令

expect命令的作用就是獲取spawn命令執行後的信息,看看是否和其事先指定的相匹配。一旦匹配上指定的内容就執行expect後面的動作,expect命令也有一些選項,相對用的較多的是-re,使用正則表達式的方式來匹配

#語法格式

expect 表達式 [動作]

#示例

spawn ssh [email protected] uptime

expect "*password" {send *123456\r"}

#提示:上述命令不能直接在linux命令行中執行,需要放入expect腳本中執行

#示例

[root@game scripts]# cat ssh.exp

#!/usr/bin/expect

spawn ssh [email protected] "free -m"

expect {

"yes/no" {exp_send "yes\r";exp_continue}

"*password" {exp_send "guoke123\r"}

}

expect eof

#參數說明

exp_send和send類似。\r(回車)

匹配多個字符串的時候,需要在每次匹配并執行動作後,加上exp_continue

3.send命令

即在expect命令匹配指定的字符串後,發送指定的字符串給系統,這些命令可以支持一些特殊轉義符号,例如:\r表示回車、\n表示換行、\t表示制表符等

#參數

-i: 指定spawn_id,用來向不同的spawn_id進程發送命令,是進行多程序控制的參數

-s: s代表slowly,即控制發送的速度,使用的時候要與expect中的标量send slow相關聯

4.exp_continue命令

作用是讓expect程序繼續匹配的意思

#示例

expect {

"yes/no" {exp_send "yes\r";exp_continue}

"*password" {exp_send "guoke123\r"}

}

#因為後面還有匹配的字符,所以需要加上exp_continue,否則expect将不會自動輸入指定的字符串,最後一個就不需要加上exp_continue了

5.send_user命令

send_user命令可用來打印expect腳本信息,類似shell裡的echo命令

#用法

[root@game scripts]# cat send_user.exp

#!/usr/bin/expect

send_user "guoke\n"

send_user "youtiao.\t" #tab鍵,沒有換行

send_user "what hao\n"

#效果

[root@game scripts]# expect send_user.exp

guoke

youtiao. what hao

6.exit命令

exit命令的功能類似于shell中的exit,即直接退出expect腳本,除了最基本的退出腳本功能之外,還可以利用這個命令對腳本做一些關閉前的清理和提示等工作

#expect變量

1.普通變量

expect中的變量定義,使用方法與TCL語言中的變量基本相同

語法

set 變量名 變量值

#示例

set user "guoke"

#打印變量語法

puts $變量名

#示例

[root@game scripts]# cat test1.exp

#!/usr/bin/expect

set password "guoke123"

puts $password

#效果

[root@game scripts]# expect test1.exp

guoke123

2.特殊變量

在expect裡也有與shell腳本裡的$0、$!、$#等類似的特殊參數變量,用于接收及控制expect腳本傳參

在expect中$argv表示參數數組,可以使用[lindex $argv n]接收expect腳本傳參,n從0開始,分别表示第一個[lindex $argv 0]參數、第二個[lindex $argv 1]參數、第三個[lindex $argv 2]參數.....

#示例

[root@game scripts]# cat test2.exp

#!/usr/bin/expect

set file [lindex $argv 0]

set id [lindex $argv 1]

set host [lindex $argv 2]

puts "$file\t$id\t$host\n"

send_user "$file\t$id\t$host\n"

#效果

[root@game scripts]# expect test2.exp test.log 1 192.168.1.1

test.log 1 192.168.1.1

test.log 1 192.168.1.1

#擴展

除了基本的位置參數外,expect也支持其他的特殊參數,例如:$argc表示傳參的個數,$argv0表示腳本的名字。

#示例

[root@game scripts]# cat test2.exp

#!/usr/bin/expect

set file [lindex $argv 0]

set id [lindex $argv 1]

set host [lindex $argv 2]

puts "$file\t$id\t$host\n"

puts $argc

puts $argv0

#效果

[root@game scripts]# expect test2.exp test.log 1 192.168.1.1

test.log 1 192.168.1.1

#傳參的總數

test2.exp #腳本的名字

#expect中的關鍵字

expect中的特特殊關鍵字用于匹配過程,代表某些特殊的含義或狀态,一般隻用于expect命令中而不能在expect命令單獨使用

1.eof關鍵字

eof(end-of-file文件結尾)關鍵字用于匹配結束符

示例

[root@game scripts]# cat ssh.exp

#!/usr/bin/expect

spawn ssh [email protected] "free -m"

expect {

"yes/no" {exp_send "yes\r";exp_continue}

"*password" {exp_send "guoke123\r"}

}

expect eof

2.timeout關鍵字

timeout是expect中的一個控制時間的關鍵字變量,它是一個全局性的時間控制開關,可以通過為這個變量賦值來規定整個expect操作的時間,注意這個變量是服務于expect全局的,而不是某一條命令,即使命令沒有任何錯誤,到了時間仍然會激活這個變量,此外,到時間後還會激活一個處理及提示信息開關,

示例

[root@game scripts]# cat ssh.exp

#!/usr/bin/expect

spawn ssh [email protected] "free -m"

expect {

-timeout 3

"yes/no" {exp_send "yes\r";exp_continue}

"*password" {exp_send "guoke123\r"}

timeout {puts "request timeout;return"}

}

#效果

[root@game scripts]# expect ssh.exp

spawn ssh [email protected] free -m

request timeout #當時間超過3秒就會打印超時退出

#expect中if條件語句

#語法

if {條件表達式} {

指令

}

if {條件表達式} {

指令

} else {

指令

}

#提示:if關鍵字後面要有空格,else關鍵字前後都要有空格,{條件表達式}大括号裡面靠近大括号出可以沒有空格,将指令括起來的起始大括号”{“ 前要有空格

#示例

#使用if語句判斷腳本傳參的個數,如果不符合則給予提示

[root@game scripts]# cat test3.exp

#!/usr/bin/expect

if { $argc != 3 } {

send_user "usage:expect $argv0 file id host\n"

exit

}

set file [lindex $argv 0]

set id [lindex $srgv 1]

set host [lindex $argv 2]

puts "$file\t$id\t$host\n"

#效果

[root@game scripts]# expect test3.exp

usage:expect test3.exp file id host

#例子:拷貝文件

[root@game scripts]# cat scp.exp

#!/usr/bin/expect

set src_file [lindex $argv 0]

set dst_file [lindex $argv 1]

set host [lindex $argv 2]

set username [lindex $arv 3]

set password [lindex $arv 4]

spawn scp $src_file $username@$host:$dst_file

expect {

-timeout 15

"yes/no" {exp_send "yes\r";exp_continue}

"*password" {exp_send "guoke123\r"}

timeout {puts "request timeout";return}

}

expect eof

#效果

[root@game scripts]# expect scp.exp test.txt test.txt 192.168.228.137 root guoke123

spawn scp test.txt [email protected]:test.txt

[email protected]'s password:

test.txt 100% 3 3.4KB/s 00:00

#到目标主機上查看

[root@zabbix ~]# ls -l test.txt

-rw-r--r-- 1 root root 3 Aug 20 05:32 test.txt

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved