Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux Shell 腳本自動輸入密碼:expect 實戰教學(scp/ssh/ftp 通用)

編輯:Linux 系統常見問題解答

痛點

自動化腳本中執行 scp、ssh、ftp 等互動式指令時,需手動輸入密碼,阻礙全自動流程。expect 專為「腳本化互動」設計,可根據提示自動回應。

安裝 expect

# CentOS/RHEL
yum install -y expect

# Ubuntu/Debian
apt-get install -y expect

# macOS (Homebrew)
brew install expect

核心語法速覽

命令說明
spawn cmd啟動子程序(如 scp)
expect "pattern"等待輸出匹配模式
send "string\r"發送字串(\r 為 Enter)
exp_continue繼續匹配下一個 expect
set timeout 秒設定等待逾時
interact將控制權交還使用者

實戰範例:自動 scp 上傳檔案

#!/usr/bin/expect
# 檔名:auto_scp.exp
# 用法:./auto_scp.exp local_file user@host:/remote/path

set timeout 20
if {$argc < 2} {
    puts "Usage: $argv0 local_file user@host:/remote/path"
    exit 1
}

set local_file [lindex $argv 0]
set remote_path [lindex $argv 1]
set password "your_password_here"
set passwd_error 0

spawn scp $local_file $remote_path

expect {
    "*assword:*" {
        if {$passwd_error == 1} {
            puts "Password error"
            exit 2
        }
        send "$password\r"
        set passwd_error 1
        exp_continue
    }
    "*(yes/no)?*" {
        send "yes\r"
        exp_continue
    }
    timeout {
        puts "Connection timeout"
        exit 3
    }
    eof {
        # 傳輸完成
    }
}

使用方法

chmod +x auto_scp.exp
./auto_scp.exp ./backup.tar.gz user@192.168.1.50:/home/user/backups/

進階:支援多種互動場景

expect {
    -re "Password:" { send "$pwd\r"; exp_continue }
    -re "Enter passphrase for key" { send "$key_pass\r"; exp_continue }
    -re "Are you sure you want to continue connecting" { send "yes\r"; exp_continue }
    -re "Permission denied" { puts "Auth failed"; exit 1 }
    timeout { puts "Timeout"; exit 1 }
    eof { }
}

常見坑點

  • Shebang 必須指向 expect:#!/usr/bin/expect,而非 #!/bin/bash。
  • 密碼含特殊字元:用單引號包裹或轉義,如 set password "pa\$\$w0rd"。
  • 逾時太短:大檔傳輸需加大 set timeout 或在傳輸期望後調整。
  • 安全性:腳本中明文密碼風險高,建議改用 **SSH 金鑰認證** 或 **sshpass**(僅限非生產環境)。

結語

expect 讓 Shell 腳本具備「人機互動」能力,解決自動化最後一公里問題。但**首選 SSH 金鑰免密登入**,expect 仅作為無法建立金鑰信任時的備選方案。

Copyright © Windows教程網 All Rights Reserved