expect 自動互動指令碼

2021-09-20 01:47:57 字數 4955 閱讀 1470

# 命令列引數

# $ar**,引數陣列,使用[lindex $ar** n]獲取,$ar** 0為指令碼名字

# $argc,引數個數

set username [lindex $ar** 1] # 獲取第1個引數

set passwd [lindex $ar** 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 "$ar**0 [lrange $ar** 0 2]\n"

send_user "it's ok\r"

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

interact

send_user "password?\ "

expect_user -re "(.*)\n"

for {} 1 {}

disconnect

spawn priv_prog

expect password:

send "$expect_out(1,string)\r"

. . .

exit

}

5.1 自動telnet會話:
#!/usr/bin/expect -f

set ip [lindex $ar** 0 ] # 接收第1個引數,作為ip

set userid [lindex $ar** 1 ] # 接收第2個引數,作為userid

set mypassword [lindex $ar** 2 ] # 接收第3個引數,作為密碼

set mycommand [lindex $ar** 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 $ar** 0 ] # 接收第1個引數,作為ip

set userid [lindex $ar** 1 ] # 接收第2個引數,作為userid

set mypassword [lindex $ar** 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 $ar** 0 ] # 接收第1個引數,作為ip

set username [lindex $ar** 1 ] # 接收第2個引數,作為username

set mypassword [lindex $ar** 2 ] # 接收第3個引數,作為密碼

set timeout 10 # 設定超時時間

spawn ssh $username@$ip # 傳送ssh請求

expect # 第一次ssh連線會提示yes/no,繼續

"*password:" # 出現密碼提示,傳送密碼

} interact # 互動模式,⽤戶會停留在遠端伺服器上⾯

5.4 批量登入ssh伺服器執行操作範例,設定增量的for迴圈
#!/usr/bin/expect

for

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  

foreach i

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

# 替換你自己的使用者名稱

set user "username"

#替換你自己的登入密碼

set password "yourpassword"

foreach ip $ar**

} "password:?"

expect "\$?"

# 替換你要執⾏的命令

send "last\r"

expect "\$?"

sleep 10

send "exit\r"

expect eof

}

5.7 ssh實現自動登入,並停留在登入伺服器上
#!/usr/bin/expect -f  

#接收第乙個引數,並設定ip

set ip [ lindex $ar** 0 ]

#接收第二個引數,並設定密碼

set password [ lindex $ar** 1 ]

#設定超時時間

set timeout 10

#傳送ssh請求

spawn ssh -p2002 root@$ip

expect

"*password:"

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

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/' /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 $ar** 0]

spawn ssh-copy-id -p2002 root@$host

expect

"*password:"

}expect eof

使用expect實現自動互動,自動登入指令碼

使用expect實現自動互動,自動登入指令碼 指令碼 如下 usr bin expect set timeout 30 spawn ssh l username 192.168.1.1 expect password send ispass r interact 1.usr bin expect 這...

自動互動式指令碼 expect簡介

我們經常會遇到一些需要與伺服器程式打交道的場景,比如,從登陸某個伺服器,然後進行某項工作。這很平常,但是如果把這個工作自動化進行,你就需要乙個程式能自動做你要告訴機器的事情,這樣,我們的expect就能大顯身手了。首先,expect是乙個簡單的工具語言,如要工作就是進行自動化的人機互動。它的作者對e...

linux自動化互動指令碼expect

expect指令碼是tcl指令碼語言的拓展。用來實現自動的互動式任務,無需人為干預。在實際開發中,執行shell指令碼有時候會輸入linux密碼或者是mysql密碼等,而expect可以幫我們輸入。expect在linux系統中沒有自帶,需要我們自行安裝,在ubuntu系統下,安裝命令如下 sudo...