1. [#!/usr/bin/expect] html
這一行告訴操做系統腳本里的代碼使用那一個shell來執行。這裏的expect其實和linux下的bash、windows下的cmd是一類東西。 linux
注意:這一行須要在腳本的第一行。 shell
2. [set timeout 30] windows
基本上認識英文的都知道這是設置超時時間的,如今你只要記住他的計時單位是:秒 。timeout -1 爲永不超時數組
3. [spawn ssh -l username 192.168.1.1] bash
spawn是進入expect環境後才能夠執行的expect內部命令,若是沒有裝expect或者直接在默認的SHELL下執行是找 不到spawn命令的。因此不要用 「which spawn「之類的命令去找spawn命令。比如windows裏的dir就是一個內部命令,這個命令由shell自帶,你沒法找到一個dir.com 或 dir.exe 的可執行文件。 服務器
它主要的功能是給ssh運行進程加個殼,用來傳遞交互指令。 ssh
4. [expect "password:"] ide
這裏的expect也是expect的一個內部命令,有點暈吧,expect的shell命令和內部命令是同樣的,但不是一個功能,習 慣就行了。這個命令的意思是判斷上次輸出結果裏是否包含「password:」的字符串,若是有則當即返回,不然就等待一段時間後返回,這裏等待時長就是 前面設置的30秒 post
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
}
}