天天看點

expect - 自動互動腳本

1. expect參數

2. 啟用選項

  • -c :執行腳本前先執行的指令,可多次使用。
  • -d :debug模式,可以在運作時輸出一些診斷資訊,與在腳本開始處使用 exp_internal 1 相似。
  • -D :啟用交換調式器,可設一整數參數。
  • -f :從檔案讀取指令,僅用于使用#!時。如果檔案名為"-",則從stdin讀取(使用"./-"從檔案名為-的檔案讀取)。
  • -i :互動式輸入指令,使用"exit"或"EOF"退出輸入狀态。
  • -- :标示選項結束(如果你需要傳遞與expect選項相似的參數給腳本時),可放到 #! 行: #!/usr/bin/expect -- 。
  • -v :顯示expect版本資訊。

3. 常用指令

# 指令行參數
# $argv,參數數組,使用[lindex $argv n]擷取,$argv 0為腳本名字
# $argc,參數個數
set username [lindex $argv 1]   # 擷取第1個參數
set passwd [lindex $argv 2]     # 擷取第2個參數

set timeout 30                 # 設定逾時

# spawn是expect内部指令,開啟ssh連接配接
spawn ssh -l username 192.168.1.1

# 判斷上次輸出結果裡是否包含“password:”的字元串,如果有則立即傳回,否則就等待一段時間(timeout)後傳回
expect "password:"

# 發送内容ispass(密碼、指令等)
send "ispass\r"

# 發送内容給使用者
send_user "$argv0 [lrange $argv 0 2]\n"
send_user "It's OK\r"

# 執行完成後保持互動狀态,控制權交給控制台(手工操作)。否則會完成後會退出。
interact
           

4. 指令介紹

  • close:關閉目前程序的連接配接。
  • debug:控制調試器。
  • disconnect:斷開程序連接配接(程序仍在背景運作)。
  • 定時讀取密碼、執行priv_prog
send_user "password?\ "
    expect_user -re "(.*)\n"
    for {} 1 {} {
     if {[fork]!=0} {sleep 3600;continue}
     disconnect
     spawn priv_prog
     expect Password:
     send "$expect_out(1,string)\r"
     . . .
     exit
    }
           
  • exit:退出expect。
  • exp_continue [-continue_timer]:繼續執行下面的比對。
  • xp_internal [-f file] value:

5. expect範例

5.1 自動telnet會話:

#!/usr/bin/expect -f
set ip [lindex $argv 0 ] # 接收第1個參數,作為IP
set userid [lindex $argv 1 ] # 接收第2個參數,作為userid
set mypassword [lindex $argv 2 ] # 接收第3個參數,作為密碼
set mycommand [lindex $argv 3 ] # 接收第4個參數,作為指令
set timeout 10 # 設定逾時時間

# 向遠端伺服器請求打開一個telnet會話,并等待伺服器詢問使用者名
spawn telnet $ip
     expect "username:"
     # 輸入使用者名,并等待伺服器詢問密碼
     send "$userid\r"
     expect "password:"
     # 輸入密碼,并等待鍵⼊需要運作的指令
     send "$mypassword\r"
     expect "%"
     # 輸入預先定好的密碼,等待運作結果
     send "$mycommand\r"
     expect "%"
     # 将運作結果存入到變量中,顯示出來或者寫到磁盤中
     set results $expect_out(buffer)
     # 退出telnet會話,等待伺服器的退出提示EOF
     send "exit\r"
     expect eof
           

5.2 自動建立FTP會話

#!/usr/bin/expect -f
set ip [lindex $argv 0 ]            # 接收第1個參數,作為IP
set userid [lindex $argv 1 ]        # 接收第2個參數,作為Userid
set mypassword [lindex $argv 2 ]    # 接收第3個參數,作為密碼
set timeout 10                     # 設定逾時時間

# 向遠端伺服器請求打開一個FTP會話,并等待伺服器詢問使用者名
spawn ftp $ip
     expect "username:"
     # 輸入使用者名,并等待伺服器詢問密碼
     send "$userid\r"
     expect "password:"
     # 輸入密碼,并等待FTP提示符的出現
     send "$mypassword\r"
     expect "ftp>"
     # 切換到二進制模式,并等待FTP提示符的出現
     send "bin\r"
     expect "ftp>"
     # 關閉ftp的提示符
     send "prompt\r"
     expect "ftp>"
     # 下載下傳所有檔案
     send "mget *\r"
     expect "ftp>"
     # 退出此次ftp會話,并等待伺服器的退出提示EOF
     send "bye\r"
     expect eof
           

5.3 自動登入SSH

#!/usr/bin/expect -f 
set ip [lindex $argv 0 ]            # 接收第1個參數,作為IP
set username [lindex $argv 1 ]  # 接收第2個參數,作為username
set mypassword [lindex $argv 2 ]   # 接收第3個參數,作為密碼
set timeout 10                      # 設定逾時時間


spawn ssh $username@$ip         # 發送ssh請求
expect {                      # 傳回資訊比對
    "*yes/no" { send "yes\r"; exp_continue}     # 第一次ssh連接配接會提示yes/no,繼續 
    "*password:" { send "$mypassword\r" }        # 出現密碼提示,發送密碼 
} 
interact    # 互動模式,⽤戶會停留在遠端伺服器上⾯
           

5.4 批量登入ssh伺服器執行操作範例,設定增量的for循環

#!/usr/bin/expect
for {set i 10} {$i <= 12} {incr i} {
     set timeout 30
     set ssh_user [lindex $argv 0]
     spawn ssh -i .ssh/$ssh_user abc$i.com

     expect_before "no)?" {
     send "yes\r" }
     sleep 1
     expect "password*"
     send "hello\r"
     expect "*#"
     send "echo hello expect! > /tmp/expect.txt\r"
     expect "*#"
     send "echo\r"
}
exit
           

5.5 批量登入ssh并執⾏指令,foreach文法

if {$argc!=2} {
     send_user "usage: ./expect ssh_user password\n"
     exit
}

foreach i {11 12} {
     set timeout 30
     set ssh_user [lindex $argv 0]
     set password [lindex $argv 1]
     spawn ssh -i .ssh/$ssh_user [email protected]
     expect_before "no)?" {
     send "yes\r" }
     sleep 1
     expect "Enter passphrase for key*"
     send "password\r"
     expect "*#"
     send "echo hello expect! > /tmp/expect.txt\r"
     expect "*#"
     send "echo\r"
}
exit
           

5.6 另一自動ssh範例

從指令行擷取伺服器IP,foreach文法,expect嵌套

#!/usr/bin/expect
# 使用方法: script_name ip1 ip2 ip3 ...
set timeout 20
if {$argc < 1} {
     puts "Usage: script IPs"
     exit 1
}
# 替換你自己的使用者名
set user "username"
#替換你自己的登入密碼

set password "yourpassword"
foreach IP $argv {
spawn ssh $user@$IP

expect \
  "(yes/no)?" {
     send "yes\r"
     expect "password:?" {
         send "$password\r"
     }
  } "password:?" {
     send "$password\r"
}

expect "\$?"

# 替換你要執⾏的指令
send "last\r"
expect "\$?"
sleep 10
send "exit\r"
expect eof
}
           

5.7 ssh實作自動登入,并停留在登入伺服器上

#!/usr/bin/expect -f  
#接收第一個參數,并設定IP
set ip [ lindex $argv 0 ]

#接收第二個參數,并設定密碼
set password [ lindex $argv 1 ]

#設定逾時時間
set timeout 10

#發送ssh請求
spawn ssh -p2002 root@$ip
 expect {
 "*yes/no" { send "yes\r"; exp_continue}
 "*password:" { send "$password\r" }
 }

# 互動模式,使用者會停留在遠端伺服器上面.
interact
           

exp_continue

是表示繼續比對下一次輸入。

5.8 批量拷貝公鑰至目标主機

實作免密碼登入,通過shell腳本去調用expect腳本,達到循環拷貝公鑰的目的。将expect腳本和shell腳本寫在一起的話,容易出現問題。

shell腳本:

[root@LOCALHOST sh]# cat ssh-copy-id.sh 
#!/bin/bash

# 截取第2列以DH開頭的行,并輸出第二列
hosts=`awk '$2~/^DH/{print $2}' /etc/hosts`

for host in $hosts;do
    ./ssh-copy-id.exp $host         # 調用expect腳本
done
           

expect腳本:

[root@LOCALHOST sh]# cat ssh-copy-id.exp 
#!/usr/bin/expect -f

set timeout 5
set host [lindex $argv 0]

spawn ssh-copy-id -p2002 root@$host
expect {
   "*yes/no" { send "yes\r";exp_continue }
   "*password:" { send "etyfhzm\r" }
}
expect eof