1. 程式人生 > >使用expect自動化批量向多臺機器分文件

使用expect自動化批量向多臺機器分文件

變量 生產場景 批量 cat lin 解釋 基本 uptime 功能

自動化運維的過程中,某些時候我們需要受用輸入密碼,這時候Expect這個工具可以完成。


首先要安裝expect,直接yum安裝就可以的。

下來我們試試expect這個工具:

使用ssh登陸:

#!/bin/expect                                      #腳本解釋器
spawn ssh [email protected]  uptime             #開啟expect自動交互,執行ssh命令
expect "*password" {send "123456\n"}               #如果ssh命令輸出匹配*password,就發送123456給系統
expect eof                                         #表示交互結束

下來時匹配多個expect:

#!/bin/expect

spawn ssh [email protected]  uptime

expect {                                       #將多個字符放在expect裏面,然後spawn執行的過程中進行匹配
  "yes/no" {exp_send "yes\r";exp_continue}      #exp_send和前面的send一樣,匹配多個字符串在每次匹配並執行動作後,加上exp_continue
  "*password" {exp_send "123456\n"}
}
expect eof

利用expect響應shell腳本中的多個read讀入:

shell腳本:
#!/bin/bash
read -p "plz input your username:" name
read -p "plz input your password:" pass
echo -en "your username $name,your password $pass\n"
~
expect腳本:
#!/bin/expect
spawn sh /root/32.sh
expect {
  "username" {exp_send "xpg\r";exp_continue}
  "password" {exp_send "123456\n"}
}
expect eof
~

18.4.5 send_user命令

相當於echo -n這個命令:

#!/bin/expect
send_user "I AM OLDBOY\n"
send_user "I am who?\n"

18.4.6 exit命令

類似shell中的shell,直接退出expect腳本,除了基本的退出腳本功能之外,還可以利用這個腳本做一些關閉前的清理和提示等工作:

#!/bin/expect
send_user "I AM OLDBOY\n"
send_user "I am who?\n"
exit -onexit {
  send_user "Good bye\n"
}

18.5 Expect程序變量

18.5.1普通變量

定義變量的基本語法如下:

set 變量名 變量值

打印:

puts $變量名

#!/bin/expect
set password "123456"
puts $password

18.5.2特殊參數變量

在expect裏有與shell腳本裏的$0,$1,$2等類似的特殊參數變量,用於接收及控制expect腳本傳參。

在expect中$argv表示參數數組,可以使用[lindex $argv n]接收expect腳本傳參,n從0開始,分別表示第一個[lindex $argv0] 參數、

例如:

#!/bin/expect
set file [lindex $argv 0]
set user [lindex $argv 1]
set dir  [lindex $argv 2]

puts $argv0
puts $argc
puts $file
send_user "your name is $user;your dir is $dir\n"

18.6 Expect程序中的if條件語句

if { 表達式 } {

指令

}

或者

if { 條件表達式 } {

指令

}else {

指令

}

範例18-9 使用if語句判斷腳本傳參的個數,如果不符合給予提示:

#!/bin/expect
if { $argc != 3 } {
   send_user "usage:expect $argv0 file host dir/n"
   exit -onexit {
   send_user "Good bye bye"
   }
}
set file [lindex $argv 0]
set host [lindex $argv 1]
set dir  [lindex $argv 2]

puts $file
puts $argv0
puts $argc
~

18.7 timeout 關鍵字

這個關鍵字是服務於全局的,而不是某一個;和shell中的sleep很像,休息30s。

18.8企業生產場景下expect案例

  1. 開發expect自動交互的腳本:

#!/bin/expect
if { $argc != 2 } {
   send_user "usage:expect $argv0 ip cmd/n"
   exit
   }

set ip   [lindex $argv 0]
set cmd  [lindex $argv 1]
#set dir  [lindex $argv 2]
spawn ssh root@$ip  $cmd
expect {
   "yes/no" {exp_send "yes\r";exp_continue}
   "*password" {exp_send "123456\n"}
}
expect eof

上面是一個,我們向想辦法批量執行execpt腳本試試:

用這個腳本調用上面的腳本,就可以實現循環批量

#!/bin/bash
if [ $# - ne 1 ]
  then
    echo "USAGE:$0 cmd"
    exit
fi

cmd=$1

for i in 136 137
do
  expect /root/4.exp 192.168.116.$i $cmd
done

18.8自動化 批量發送文件 **

下面這兩個腳本相結合就能實現

#cat 5.exp
#!/bin/expect

if { $argc != 3 } {
  send_user "plz input:usage $argv0 ip cmd"
  exit
}
set file [lindex $argv 0]
set ip   [lindex $argv 1]
set dir  [lindex $argv 2]

spawn scp -rp $file root@$ip:$dir

expect {
  "yes/no" {exp_send "yes\r";exp_continue}
  "*password" {exp_send "123456\n"}
}
expect eof
#cat 55.sh
#!/bin/bash
if [ $# -ne 2 ]; then
   echo "plz input $0 file dir"
   exit
fi
file=$1
dir=$2
for i in 136 137
do
   expect 5.exp $file 192.168.116.$i $dir
done
~


使用expect自動化批量向多臺機器分文件