1. 程式人生 > 其它 >linux expect 自動互動命令

linux expect 自動互動命令

如何從機器A上ssh到機器B上,然後執行機器B上的命令?如何使之自動化完成?看完下面的文章你就明白了

一、安裝
expect 是基於tcl 演變而來的,所以很多語法和tcl 類似

sudo apt-get install tcl tk expect
或者

yum install -y tcl tclx tcl-devel
二、如何使用
expect是linux中的一個用來處理互動的命令。藉助Expect,我們可以將互動過程寫在一個指令碼上,使之自動化完成。形象的說,ssh登入,ftp登入等都符合互動的定義。下文我們首先提出一個問題,然後介紹基礎知四個命令

四個命令

Expect中最關鍵的四個命令是send,expect,spawn,interact。

send:用於向程序傳送字串
expect:從程序接收字串
spawn:啟動新的程序
interact:允許使用者互動
1、send

send命令接收一個字串引數,並將該引數傳送到程序。

expect1.1> send "hello world\n"
hello world
2. expect命令

啟用選項

-c:執行指令碼前先執行的命令,可多次使用。

-d:debug模式,可以在執行時輸出一些診斷資訊,與在指令碼開始處使用exp_internal 1相似。

-D:啟用交換調式器,可設一整數引數。

-f:從檔案讀取命令,僅用於使用#!時。如果檔名為"-",則從stdin讀取(使用"./-"從檔名為-的檔案讀取)。

-i:互動式輸入命令,使用"exit"或"EOF"退出輸入狀態。

--:標示選項結束(如果你需要傳遞與expect選項相似的引數給指令碼時),可放到#!行:#!/usr/bin/expect --。

-v:顯示expect版本資訊。

expect命令和send命令正好相反,expect通常是用來等待一個程序的反饋。expect可以接收一個字串引數,也可以接收正則表示式

expect "hi\n"
send "hello there!\n"
這兩行程式碼的意思是:從標準輸入中等到hi和換行鍵後,向標準輸出輸出hello there。

看一段程式碼:

#!/usr/bin/expect -f
expect "hi\n"
send "you typed <$expect_out(buffer)>"
send "but I only expected <$expect_out(0,string)>"
執行結果

1
2
3
4
5
hi
you typed <1
2
3
4
5
hi
>but I only expected <hi
多分支模式

expect "hi" { send "You said hi\n" } \
"hello" { send "Hello yourself\n" } \
"bye" { send "That was unexpected\n
或者下面的寫法

expect {
"hi" { send "You said hi\n"}
"hello" { send "Hello yourself\n"}
"bye" { send "That was unexpected\n"}
}
3、spawn

spawn命令就是用來啟動新的程序的,比如登入ftp

spawn ftp ftp.test.com
4、interact

interact ##是Expect用來開啟使用者與產生程序之間通訊的命令,簡單說就是登陸以後將遠端伺服器的終端保持在當前終端,而不是將遠端終端關掉

#!/usr/bin/expect -f
set timeout -1
spawn ssh $user@$host
expect -exact "password"
send "$password\n"
send -- "pwd\n"
interacter
三、總結
1、常用命令

# 命令列引數
# $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

2、命令介紹

close:關閉當前程序的連線。
debug:控制偵錯程式。
disconnect:斷開程序連線(程序仍在後臺執行)。
執行priv_prog:定時讀取密碼
exit:退出expect。
exp_continue [-continue_timer]:繼續執行下面的匹配。
exp_internal [-f file] value:
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
}
3、範例

A、自動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
B、自動建立FTP會話

#!/usr/bin/expect -f
setip [lindex $argv 0 ] # 接收第1個引數,作為IP
setuserid [lindex $argv 1 ] # 接收第2個引數,作為Userid
setmypassword [lindex $argv 2 ] # 接收第3個引數,作為密碼
settimeout 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

C、自動登入ssh執行命令

#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set PASSWD [lindex $argv 2]
set CMD [lindex $argv 3]

spawn ssh $USER@$IP $CMD
expect {
"(yes/no)?" {
send "yes\r"
expect "password:"
send "$PASSWD\r"
}
"password:" {send "$PASSWD\r"}
"* to host" {exit 1}
}
expect eof

D、批量登入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

E、批量登入ssh並執行命令,foreach語法

#!/usr/bin/expect
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

F、從命令列獲取伺服器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
}

G、ssh自動登入expect指令碼

#!/usr/bin/expect -f
# Auther:YuanXing
# Update:2014-02-08
if {$argc < 4} {
send_user "Usage:\n $argv0 IPaddr User Passwd Port Passphrase\n"
puts stderr "argv error!\n"
sleep 1
exit 1
}

set ip [lindex $argv 0 ]
set user [lindex $argv 1 ]
set passwd [lindex $argv 2 ]
set port [lindex $argv 3 ]
set passphrase [lindex $argv 4 ]
set timeout 6
if {$port == ""} {
set port 22
}
#send_user "IP:$ip,User:$user,Passwd:$passwd,Port:$port,Passphrase:$passphrase"
spawn ssh -p $port $user@$ip

expect_before "(yes/no)\\?" {
send "yes\r"}

expect \
"Enter passphrase for key*" {
send "$passphrase\r"
exp_continue
} " password:?" {
send "$passwd\r"
exp_continue
} "*\[#\\\$]" {
interact
} "* to host" {
send_user "Connect faild!"
exit 2
} timeout {
send_user "Connect timeout!"
exit 2
} eof {
send_user "Lost connect!"
exit
}

H、通過shell指令碼呼叫

#!/bin/bash

TMP=$(mktemp)
username=(test)
# create expect script

for ip in `cat ip.txt`; do

cat > $TMP << EOF
set timeout 5
spawn ssh -i $username/id_rsa -p888 $username@$ip
expect -exact "$username"
send -- "su - \r\n"
expect -exact "Password"
send -- "213f214##!ds(*&a@\r\n"
expect -exact "root"
send -- "userdel -r zhangshan\r\n"
send -- "userdel -r lisi\r\n"
send -- "history -c \r\n exit\r\n"
send -- "history -c \r\n exit\r\n"
expect eof
EOF

expect -f $TMP
rm $TMP
done

本文轉自:https://blog.csdn.net/whatday/article/details/105632967