前幾天下班的時候,聽系統運維組的經理在給本部門的人推薦expect工具,這個工具我曾經用過,本以為現在
關注的人少了,沒想到它的粉絲還是很多的,借此機會,我翻出之前整理過的文檔,再次發揮餘熱。
##########################################
# create by maidong
# date 2011--05-13 晚
# http://www.bdkyr.com
#
Expect是在Tcl基礎上建立起來的,它還提供了一些Tcl所沒有的指令,
它可以用來做一些linux下無法做到互動的一些指令操作,在遠端管理
方面發揮很大的作用。
spawn指令激活一個Unix程式來進行互動式的運作。
send指令向程序發送字元串。
expect指令等待程序的某些字元串。
expect支援正規表達式并能同時等待多個字元串,并對每一個字元串執行不同的操作.
#-------------------------------------------------------------------------#
Other interesting scripts are available separately in the directory
http://expect.nist.gov/scripts/ (ftpable as
ftp://ftp.nist.gov/mel/div826/subject/expect/scripts). (See below for
how to retrieve these.) You are welcome to send me scripts to add to
this directory. A number of Expect scripts are also available in the
Tcl archive, available at ftp://ftp.neosoft.com/pub/tcl.
#----------------操作步驟----------------------------------------------------#
A. Tcl 安裝
首頁: http://www.tcl.tk
下載下傳位址: http://www.tcl.tk/software/tcltk/downloadnow84.tml
1.下載下傳源碼包
wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz
wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tk8.4.11-src.tar.gz
2.解壓縮源碼包
#tar xfvz tk8.4.11-src.tar.gz #暫時可以不用
tar xfvz tcl8.4.11-src.tar.gz
3.安裝配置
cd tcl8.4.11
cd unix
./configure --prefix=/usr/tcl --enable-shared
make && make install
4.安裝完畢以後,進入tcl源代碼的根目錄,把子目錄unix下面的tclUnixPort.h copy到子目錄generic中。
暫時不要删除tcl源代碼,因為expect的安裝過程還需要用。
cp tclUnixPort.h ../generic/
B. expect 安裝 (需Tcl的庫)
首頁: http://expect.nist.gov/
wget http://expect.nist.gov/expect-5.43.0.tar.gz
wget http://cdnetworks-kr-1.dl.sourceforge.net/project/buluoos/0.1/src/expect-5.43.0.tar.gz
tar xfvz expect-5.43.0.tar.gz
cd expect-5.43
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=/home/xuekun/tools/tcl8.4.11/generic/
make
make install
#--執行個體1.1----by maidong-------------------------------------------------#
[root@nginx scripts]# vi expect.exp
#!/usr/expect/bin/expect
set timeout 60
set port 50718
set host 192.168.15.52
set name root
set password dn3s
spawn ssh -p$port $host -l $name
expect {
"(yes/no)?" {
send "yes\r"
expect "password:"
send "$password\r"
}
"password:" { send "$password\r" }
}
expect "#"
send "uname\n"
expect "Linux"
send_user "Now you can do some operation on this terminal\n"
[root@nginx scripts]# /usr/expect/bin/expect expect.exp
spawn ssh -p50718 192.168.15.52 -l root
[email protected]'s password:
Last login: Sun May 15 05:41:26 2011 from 192.168.15.51
[root@lamp-002 ~]# uname
Linux
Now you can do some operation on this terminal
[root@nginx scripts]#
#------------------------------------------------------------------------------#
#說明
#!/usr/bin/expect
# 設定逾時時間為 60 秒
set timeout 60
# 設定要登入的主機 IP 位址
set host 192.168.15.52
# 設定以什麼名字的使用者登入
set name root
# 設定使用者名的登入密碼
set password dn3s
#spawn 一個 ssh 登入程序
spawn ssh $host -l $name
# 等待響應,第一次登入往往會提示是否永久儲存 RSA 到本機的 know hosts 清單中;
#等到回答後,在提示輸出密碼;之後就直接提示輸入密碼
expect {
"(yes/no)?" {
send "yes\n"
expect "Password:"
send "$pasword\n"
}
"Password:" {
send "$password\n"
}
expect "#"
# 下面測試是否登入到 $host
send "uname\n"
expect "Linux"
send_user "Now you can do some operation on this terminal\n"
exit
#-----------------finish-------------------------------------------------#
#--執行個體1.2--expect互動式scp檔案--------------by xuekun--------------------#
[xuekun@expect ~]$ cat scp.exp
#modify by xk
#201105-30
set ADDR [lindex $argv 0]
set LOGIN [lindex $argv 1]
set PASSWD [lindex $argv 2]
set DIR [lindex $argv 3]
spawn scp -rp -P50718 "$DIR" "$LOGIN@$ADDR:/tmp"
set timeout 60
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "$PASSWD\r"
interact
#--運作測試-------------------------------------------------------------------------------#
[xuekun@expect ~]$ /usr/expect/bin/expect scp.exp 192.168.15.52 root xuekun /home/xuekun/
說明:輸入4個參數, $DIR 為要copy到遠端伺服器的目錄或者檔案
$LOGIN 為使用者名, 通過expect 自動輸入yes 和 password
#-----------------------------------------------------------------------------------------#
#!/bin/sh
/usr/bin/expect autocopy.exp 192.168.15.53 root 123456 /home/xuekun
/usr/bin/expect autocopy.exp 192.168.15.54 root 123456 /home/xuekun
/usr/bin/expect autocopy.exp 192.168.15.55 root 123456 /home/xuekun
/usr/bin/expect autocopy.exp 192.168.15.56 root 123456 /home/xuekun
隻要 execute 上面這個 sh 腳本, 就可以批量完成資料的遠端copy。