一、expect用法及搭配
expect是一個用於自動化交互式系統的工具,它可以通過腳本模擬人的交互行為來完成自動化的操作。它通常與spawn(用於啟動一個進程)和send(用於向一個進程發送數據)等命令搭配使用。以交互登錄操作為例:
#!/usr/bin/expect -f set timeout 30 spawn ssh username@hostname expect "password:" send "yourpassword\r" expect "#" send "ls -l\r" expect "#" send "exit\r" expect eof
上述腳本先用spawn命令啟動一個ssh會話,然後使用expect命令來匹配返回的結果,如果匹配到了相應的字符串就執行後面的send命令,再匹配下一個字符串,以此類推。最後使用expect eof命令等待連接關閉。
二、expect用法固定搭配
expect還有一些經常搭配使用的命令,比如wait、interact等。
1. wait命令
wait命令可以用於等待子進程執行完成,同時支持超時功能。例如,下面的腳本用於等待子進程執行完成,然後輸出相應的消息:
#!/usr/bin/expect -f set timeout 30 spawn command wait puts "command completed"
2. interact命令
interact命令可以讓用戶在交互登錄時自由輸入命令。在expect腳本中,交互登錄通常需要用戶輸入用戶名和密碼,或者需要用戶輸入其他信息。interact命令可以將控制權交還給用戶,讓用戶自己輸入命令。例如:
#!/usr/bin/expect -f set timeout 30 spawn ssh username@hostname expect "password:" send "yourpassword\r" interact
上述腳本中使用interact命令可以讓用戶在登錄後自由輸入命令。
三、expect用法什麼意思
除了上面的用法,expect還有其他更多的用途:
1. 自動化測試
expect可以用於自動化測試,它提供了豐富的命令和變量來模擬與被測程序的交互過程。例如,下面的腳本用於測試ftp連接是否正確:
#!/usr/bin/expect -f set timeout 30 spawn ftp localhost expect "Name" send "testuser\r" expect "Password" send "testpass\r" expect "ftp>" send "ls -l\r" expect "ftp>" send "bye\r" expect eof
2. 郵件自動化
expect可以用於自動發送和接收郵件,自動添加附件等。例如,下面的腳本可以自動發送一封郵件:
#!/usr/bin/expect -f set timeout 30 spawn /usr/sbin/sendmail -t expect "Subject:" send "Test mail\r" expect "To:" send "test@example.com\r" expect "Cc:" send "test@example.com\r" expect "From:" send "test@example.com\r" expect "Enter mail, end with \".\" on a line by itself:" send "This is a test mail.\r.\r" expect "Sent" send "quit\r" expect eof
四、expect三個常見的短語
1. spawn id
spawn id是spawn命令返回的一個進程標識符,可以用於與該進程進行交互。例如:
#!/usr/bin/expect -f set timeout 30 spawn command expect "Password:" send "password\r" expect -re {[^\n]+} set result $expect_out(0,string) send "exit\r" expect eof
2. expect_out
expect_out包含了expect命令匹配的結果信息。例如:
#!/usr/bin/expect -f set timeout 30 spawn command expect "Password:" send "password\r" expect -re {[^\n]+} set result $expect_out(0,string) send "exit\r" expect eof puts "result: $result"
3. expect_before、expect_after
expect_before和expect_after分別表示匹配字符串之前和匹配字符串之後的文本內容。例如:
#!/usr/bin/expect -f set timeout 30 spawn ssh username@hostname expect { "password:" { send "yourpassword\r" expect "Welcome" puts "login success" } "Connection refused" { puts "connection failed" } } expect eof
五、expect的三種用法
expect有三種常見的用法:字符串匹配、正則匹配和超時處理。
1. 字符串匹配
字符串匹配是比較簡單的匹配方式,它可以匹配固定的字符串。例如:
#!/usr/bin/expect -f set timeout 30 spawn ssh username@hostname expect "password:" send "yourpassword\r" expect "Welcome" send "exit\r" expect eof
2. 正則匹配
正則匹配可以匹配更加靈活的字符串,支持通配符、字符類等特殊字符。例如:
#!/usr/bin/expect -f set timeout 30 spawn ssh username@hostname expect { "password:" { send "yourpassword\r" expect -re ".*(Welcome).*" puts "login success" } "Connection refused" { puts "connection failed" } } expect eof
3. 超時處理
超時處理用於處理expect命令匹配超時的情況。例如:
#!/usr/bin/expect -f set timeout 30 spawn ssh username@hostname expect { "password:" { send "yourpassword\r" expect "Welcome" puts "login success" } timeout { puts "timeout" exit 1 } eof { puts "connection closed" } } expect eof
六、expect用法和例句
下面是一些實際應用場景中常見的expect用法和例句。
1. 判斷命令執行結果,並根據結果執行不同的操作
#!/usr/bin/expect -f set timeout 30 spawn command expect { "success" { puts "command executed successfully" } "failed" { puts "command execution failed" } } send "exit\r" expect eof
2. 匹配多個字符串,按順序執行多個操作
#!/usr/bin/expect -f set timeout 30 spawn command expect { "password:" { send "yourpassword\r" expect "Welcome" send "ls -l\r" expect "success" send "exit\r" expect eof } "connection refused" { puts "connection failed" exit 1 } }
3. 交互式輸入指令,輸入命令後自動執行
#!/usr/bin/expect -f set timeout 30 spawn telnet localhost 23 expect "login:" send "username\r" expect "Password:" send "password\r" expect ">" interact
七、expect用法總結
expect是一個功能強大的工具,用於自動化交互式操作系統和應用程序。它提供了豐富的命令和變量,可以實現各種自動化任務。expect的基本用法包括字符串匹配、正則匹配和超時處理等。更複雜的使用場景需要使用更多的expect命令和語法。
八、expect用法與區別
expect與其他自動化工具相比,有以下幾個優點:
1. 支持交互式操作
expect可以直接操作交互式應用程序,比如ssh、telnet等,可以實現自動化登錄等操作。
2. 支持多種匹配方式
expect支持字符串匹配、正則匹配等多種匹配方式,可以根據不同的場景靈活選擇。
3. 可以使用TCL腳本語言編寫
TCL是一種簡單易學的腳本語言,可以快速編寫複雜的expect腳本。
原創文章,作者:TOZN,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/137302.html