萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux expect 自動化互動實戰:SSH 免密登入、批量執行、SCP 傳檔完整範例

expect 是基於 Tcl 的自動化互動工具,能處理需要密碼、確認提示的 CLI 程式 (ssh, scp, ftp, passwd 等),實現腳本化無人值守操作。本文從核心指令解析到三大實戰場景完整示範。

核心指令速查

指令功能
spawn cmd啟動子進程並建立管道 (expect 內部指令)
expect "pattern"等待輸出匹配模式 (支援正則)
send "string\r"發送字串 (需以 \r 結尾模擬 Enter)
interact將控制權交還使用者 (保持互動)
exp_continue繼續匹配下一個 expect 區塊
expect -re "regex"正則表達式匹配
expect eof等待子進程結束

基礎範例:SSH 自動登入並保持互動

#!/usr/bin/expect
set timeout 30
spawn ssh -l root 192.168.1.188
expect "password:"
send "abc123456\r"
interact

案例一:遠端執行指令並退出

#!/usr/bin/expect -f
set timeout 30
spawn ssh -l root 192.168.1.188
expect {
    "yes/no" { send "yes\r"; exp_continue }
    "password:" { send "abc123456\r" }
}
expect -re "](#|\\$) "
send "bash /root/test.sh\r"
expect -re "](#|\\$) "
send "exit\r"

單行版 (嵌入 Bash 腳本):

#!/bin/bash
expect -c "
spawn ssh root@192.168.1.188 \"ifconfig; whoami; pwd; echo hello\"
expect {
    \"*assword\" { send \"abc123456\r\" }
    \"yes/no\" { send \"yes\r\"; exp_continue }
}
expect eof
"

案例二:批量管理多台伺服器

主機清單 host.list:

192.168.1.1 22 root pass1
192.168.1.2 22 root pass2
192.168.1.3 22 root pass3

Expect 模組 main.exp:

#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set port [lindex $argv 1]
set username [lindex $argv 2]
set passwd [lindex $argv 3]
set timeout 30
spawn ssh $ipaddress -p$port -l$username
expect {
    "yes/no" { send "yes\r"; exp_continue }
    "password:" { send "$passwd\r" }
}
expect -re "](#|\\$) "
send "bash /root/test.sh\r"
expect -re "](#|\\$) "
send "exit\r"

Bash 驅動迴圈 whi.sh:

#!/bin/bash
host="host.list"
while read line; do
    expect main.exp $line
done < $host

案例三:SCP 自動傳檔

expect -c "
spawn scp root@192.168.1.188:/root/file1.tgz /root
expect {
    \"*assword\" { set timeout 300; send \"abc123456\r\" }
    \"yes/no\" { send \"yes\r\"; exp_continue }
}
expect eof
"

安全性與最佳實務

  • 避免明文密碼:使用 SSH 金鑰認證 (ssh-copy-id) 取代 expect 密碼傳送
  • 權限控制:腳本檔案 chmod 700,限制擁有者讀寫
  • 錯誤處理:加入 catch 捕捉 spawn 失敗、連線逾時
  • 日誌記錄:log_file -noappend session.log 記錄完整互動
  • 並行優化:大量主機使用 parallel-ssh、ansible 替代 expect 迴圈

expect 自動化示意

現代替代方案比較

工具優點缺點
expect輕量、無依賴、支援任意 CLI 互動明文密碼、維護困難、無冪等性
Ansible冪等、模組豐富、YAML 可讀、並行需 Python、學習曲線

expect 仍是快速解決舊系統、網路設備、無 Python 環境自動化互動的利器,但新架構建議優先採用 Ansible/SaltStack 等現代配置管理工具。

copyright © 萬盛學電腦網 all rights reserved