expect用法

1. [#!/usr/bin/expect] 

這一行告訴操作系統腳本里的代碼使用那一個shell來執行。這裏的expect其實和linux下的bash、windows下的cmd是一類東西。 

注意:這一行需要在腳本的第一行。 

2. [set timeout 30] 

基本上認識英文的都知道這是設置超時時間的,現在你只要記住他的計時單位是:秒   。timeout -1 爲永不超時

3. [spawn ssh -l username 192.168.1.1] 

spawn是進入expect環境後纔可以執行的expect內部命令,如果沒有裝expect或者直接在默認的SHELL下執行是找不到spawn命令的。所以不要用 “which spawn“之類的命令去找spawn命令。好比windows裏的dir就是一個內部命令,這個命令由shell自帶,你無法找到一個dir.com 或 dir.exe 的可執行文件。 

它主要的功能是給ssh運行進程加個殼,用來傳遞交互指令。 

4. [expect "password:"] 

這裏的expect也是expect的一個內部命令,有點暈吧,expect的shell命令和內部命令是一樣的,但不是一個功能,習慣就好了。這個命令的意思是判斷上次輸出結果裏是否包含“password:”的字符串,如果有則立即返回,否則就等待一段時間後返回,這裏等待時長就是前面設置的30秒 

5. [send "ispass\r"] 

這裏就是執行交互動作,與手工輸入密碼的動作等效。 

溫馨提示: 命令字符串結尾別忘記加上“\r”,如果出現異常等待的狀態可以覈查一下。 

6. [interact] 

執行完成後保持交互狀態,把控制權交給控制檯,這個時候就可以手工操作了。如果沒有這一句登錄完成後會退出,而不是留在遠程終端上。如果你只是登錄過去執行 

7.$argv 參數數組

expect腳本可以接受從bash傳遞過來的參數.可以使用[lindex $argv n]獲得,n從0開始,分別表示第一個,第二個,第三個....參數

下面的expect腳本的例子

執行這個文件./launch.exp 1 2 3

屏幕上就會分別打印出參數

send_user用來發送內容給用戶。

參數運用方面還有很多技巧

比如$argc 存儲了參數個數,args被結構化成一個列表存在argv。$argv0 被初始化爲腳本名字。

除此之外,如果你在第一行(#!那行)使用-d (debug參數),可以在運行的時候輸出一些很有用的信息

比如你會看見

argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./launch.exp argv[3] = 1 argv[4] = 2 argv[5] = 3

使用這些也可以完成參數傳遞

8.expect的命令行參數參考了c語言的,與bash shell有點不一樣。其中,$argc爲命令行參數的個數,$argv0爲腳本名字本身,$argv爲命令行參數。[lrange $argv 0 0]表示第1個參數,[lrange $argv 0 4]爲第一個到第五個參數。與c語言不一樣的地方在於,$argv不包含腳本名字本身。

 

9.exp_continue的用法

#!/usr/bin/expect -f
set ipaddr "localhost"
set passwd "iforgot"
spawn ssh root@$ipaddr              #spawn   意思是執行命令,expect內命令,shell中不存在
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]# "
send "touch a.txt\r"                       #意思爲發送命令
send "exit\r"
expect eof
exit

exp_continue可以繼續執行下面的匹配,簡單了許多。還有一點,讓我認識到匹配不見得要匹配最後幾個字符。

 

10.拿來小例子   

設置變量     set PASSWD   abcd123

#!/usr/bin/expect -f
# Expect script to supply root/admin password for remote ssh server
# and execute command.
# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# scriptname = Path to remote script which will execute on remote server
# If you username and passwd has not pass the rsa trust, your login will fail.
# Usage For example:
#  ./sshlogin.exp password 192.168.1.11 who
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# set Variables
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh yourusername@$ipaddr $scriptname $arg1
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof

==============================================================================

#!/usr/bin/expect 
 
 # 設置超時時間爲 60 秒
 set timeout  60                                         
 # 設置要登錄的主機 IP 地址
 set host 192.168.1.46
 # 設置以什麼名字的用戶登錄
 set name root 
 # 設置用戶名的登錄密碼
 set password 123456 
 
 #spawn 一個 ssh 登錄進程
 spawn  ssh $host -l $name 
 # 等待響應,第一次登錄往往會提示是否永久保存 RSA 到本機的 know hosts 列表中;等到回答後,在提示輸出密碼;之後就直接提示輸入密碼
 expect { 
    "(yes/no)?" { 
        send "yes\n"
        expect "assword:"
        send "$pasword\n"
    } 
        "assword:" { 
        send "$password\n"
    } 
 } 
 expect "#"
 # 下面測試是否登錄到 $host 
 send "uname\n"
 expect "Linux"
 send_user  "Now you can do some operation on this terminal\n"
 # 這裏使用了 interact 命令,使執行完程序後,用戶可以在 $host 終端進行交互操作。
 Interact

==============================================================================

用expect實現ssh自動登錄對服務器進行批量管理

1.實現ssh自動登錄完成任務的expect腳本

#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh shellqun@$ipaddress
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "*from*"
send "mkdir -p ./tmp/testfile\r"
#send "exit\r"
expect "#"  命令運行完, 你要期待一個結果, 結果就是返回shell提示符了(是# 或者$)
#最後一句第13行的解釋:

其實寫成 interact 的最大好處是登錄後不會退出,而會一直保持會話連接,可以後續手動處理其它任務,請根據實際情況自行選擇了。


2.調用login.exp完成批量管理

#!/bin/bash
for i in `awk '{print $1}' passwd.txt`
do
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
expect /root/shell/login.exp $i $j
done
 
3.passwd.txt
192.168.0.2  password2
192.168.0.3  password3
 
 13.
 
expect {
"?assword:" {                    
 
 #此大括號內是逐條執行,不存在if關係
 
 
send "$PASSWORD\r"     
exp_continue
}
}

轉載:http://www.cnblogs.com/iloveyoucc/archive/2012/05/11/2496433.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章