一.expect概述

1.expect介绍

expect 是由Don Libes基于Tcl( Tool Command Language )言语开发的,首要应用于自动化交互式操作的8 6 n ) . ; S I场景,凭借Expect处理交互的指令,能够将交互过程如:ssh登录,ft( b H (p登录等写在一个脚本上,使之自动化完结。特别适用于需求对多台服务器履行相同操作的环境中,能够大大提高系统管理人员的工作效率。

2.ec $ Yxpect指令

expect 语法:
  expect [选项] [ -c cmds ] [ [ -[f|z 5 0b] ] cmdfile ] [ args ]
常用选项
  -c:从指令行履行expect脚本,默认expect是交互地履行的
    示例:exe z U  . 3pect -c 'expect "n" {send "pressed entern"}
  -d:能够输出输出调试信息
    示例:expect -d ssh.exp
expect中相关指令
  spawn:
    发动新的进程
  send:
    用于向进程发送字符串
  expect:
    从进程接收字符串
  interactV / ^  * r = :
  = ~ ; C ^  答应用户交互
  exp_continue
    匹配多个字符串在履行动作后加此指令

3.安装expect软件包

[rq y S -oot@node101.yinzhengjie.org.cn ~]# yum info expect
Loaded plugins` : I: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrorx - } _ W q Is.huaweicloud.com
* epel: m! 6 B p @ D x = 3irrors.aliyun.com
* extras: mirrm x 4 j ^ cors.huaweicloud.com
* updates: mirrors.aliyun.com
Available Packageh W ] o 9s
Name        : expect
Arch        : x86_64
Version     : 5.45
Release     : 14.el7_1
Size        :W V e % , q { 262 k
Repo        : base/7/x86_64
Summary     : A pru & ; 6ogra} R b U h v ( R Ym-script interacA B J ? u = y Btion and testing utility
URL         : http://expect.nist.g_ [ s cov/
License     : Public Domain
DescrK ` g 5 : 3iption : Expect is a tc$  z D W 5l application for automating and testing
: interactive applications s] * N n f 4 & 0 Much as telnet, ftp, passwd, fsck,
: rlog` 9 6 W , U ein, tip, etc. ExU 1 R A z K N [ dpect makes it easy for a script to
: control another program@ -  f and interact with it.
:E ) 5 T 7 h g
: TE 3 s G yhis package contains expect and some scripts that use it.
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# yum info expect   V b T x b E 3 3   #B 8 x O v f检查expect软件包相关信息
[root@node101.yinzhengjie.org.cn ~]# yum -y ina M * , % ostall expect
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epe$ & ; | ~ 2 Al/x86_64/metaliX ^ 5 2 -nk                                                                                                                                 | 8.6 kt - N 7B  00:00:00
* base: mirrors.aU R & s r S [liyun.com
* epel: mirrors.aliyun.com
* extras: ap.stykers.moe
* updates: mirrors.aliyun.com
base                                                                                                                                                 | 3.6 kB  00:00:00
epel                                                                                                                                                 | 5.3 kg 1 H / % I c ~B  00:00:00
extras                                                                                                                                               | 2.9 kB  00:00:00
updates                                                                                                                                              | 2.9 kB  00:00:00
(1/2): epel/x86_64/upd; 3 B x r Uateinfo                                                                                                                        | 1a 7 k L 0.0 MB  00:00:06
(2/2): epel/x86_T  Y 4 :64/primary_db                                                                                                                        | 6.9 MB  00:00:14
Resolving Dependencies
--> Running transa8 1 s Z { = ( rction check| T S t { D K _
---&3 ^ J y % Z _ Z Mgt; Package expect.x86_64 0:5.45-14.el7_1 will be installed
--> Processing Dependency: libtcl8.5.so()(64bit) for package: expect-5.45-14.el7_1.x86_64
--> Running tr s n ( `ansr O 1 {action check
---> Package tcl.x86_64 1:8.5.13-8.A K d , 0 9 /el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================8 ! Z========================================3 3 A y==========================================================================Y 3 V A  Z o==========================
Package                                Arch                                   Version                                           RepositorQ P r / Gy                            Size
=========================================================6 U G / ~=======================/ i z .=====X N C d I L=======================================================================================
Installing:
expec| ( R 4 r ut                                 x86_64                                 5.45-14.el7_1                                     base                                 262 k
Installing for dependencies:
tcl                                    x86_64                                 1:8.5.13-8.el7                                    base                                 1.9 M
Transaction Su2 t | q z ? F 0mmary
================================================7 _ y==========================================h ( N ? ) K=============L _ R a r J=====================================================================
Install  1 Package (+1 DeN w r ! [pendent package)
Total download sg ? J W 3 S ^ cize: 2.1 M
InstalleA @ pd size: 4.9 M
Downloading packages:
(1/2): expecJ ; % qt-5.45-14.el7_1.x86_64.rpm                                                                                                               | 262 kB  00:00:06
(2/2): tcl-8.5.13-8.el7.x86_64.rpm                                                                                                                   | 1.9{ H [ $ l _ F / MB  00:00:07
--------------------------F + G Y S Y------------------------------------------------------------t  6 g % J U----------------2 Z # j o $ S----------------------------------------------------------------------
ToM = . j , B 5 k Ttal                                                                                                                                       294 kB/s | 2.1 MB  00:00:07
Running transaction check
Running t9 9 + m { U t { ~ransact{ ~ P n k . { 1ion test
Transaction test succeeded
Running transaction
Installing : 1:tcl-8.5.13-8.el7.x86_64                                                                                                                                1/2
Installing : expect-5.45-14.el7_1.x86_64                                                                                                                              2/2
Verifying  : 1:tcl-8.5.13-8.el7.x86_64                                                                                                                                1/2
Verifying  : expect-5.45-3 5 z - * ;14.el7_13 T N n e ) ` ^.x86_64                                                                                                                              2/2
Installed:
expect.x86_64 0:5.45-14.el7_1
Dependency Installed:
tcl.x86_64 1:8.5.13-8.el7
Complete!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node& K m m . e1019 q G 0 i.yinzhengjie.org.cn ~]# yum -y insv - g y [tag X ( = v  )ll expect   #安装expect东西

二.expect最常用的语法(tcl言语:形式-动作)

1.expect的单分支语法

[root@node101.yinzhengjie.org.cn ~]# expect 
expect1.1> expect "hi" {send "say hin"}      #捕捉用户输入的S 2 4 I ^ u"hi",然后给用户发送"say hin"
hi    #这一行是我输入的,因为被我上面界说的句子捕捉到了,下面一行的输出信息便是我之前自界说的字符串信息。
say hi
expect1.2> exi_ H X V y a + Et    #如果不想运用该程序了能够输入"exit"来正常退出交互式界面
[root@node101.yinzhengjie.j i aorg.cn ~]# 
[root@node101.yinzhengji_ 6 I 4 # 7 #e.org.cn ~]#

2.B ! Z y a : u 8expect的多分$ J f k I )支语法

[9 N = ) B aroot@node101.yinzhengjie.org.cn ~]# expect       
expect1.1> expect "hi! O ! N" {send "say hin"}
+> "bye" {send "byebyen"}
bye
byebye
expect1.2> exit
[root@node101.yinzhe6 A ] 5ngjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#
[ro4 @ { t N A 5 4 uot@node101g ]  } 6.yinzhengjie.org.cR @ = N F Ln ~]# expect           #匹配hi,hello,bye任意字符串时,履行相应输出。上面的写法和下面的写法功能是相同的。
expect1.1> expect {
+&X e - : n B q b 6gt; "hi" {send "You said hin"}+ * 
+> "hehe" {send "hehe yourselfn"}
+&x 7 s 2gt; "bye" {send{ % R { a w % "Good byen"}
+> } h t 0 J ] H
bye
Good bye
expe: O q x b ;ct1.2>
expect1.2> exit
[root@node101.yinzhengjie.org.cn ~]#

三.expect脚本事例实战

1.spa) 5 Y 1 9 7 y o Twn(发动新的进程)事例

[root@node101.yinzhengjie.org.cn ~]# ll .ssh/
total 16
-rw----d C g ^ Z i---. 1 root root  413 Jul  9 10:27 authorized_keys
-rw-------. 1j U . ! v 8 | X root root 1675 Jul  9 10:27 id_rsa
-rw-r--r--. 1 root root  413 Julh ^ - G  ~ B 4 t  9 10:27 id_rsd y Y d  S C e ma.pub
-P v D Q . Frw-r--r--. 1 root root 1608 Jul  9 10:28 knowM Z O F G ) Mn_hosts
[root@node101V i !.yinzhenM 8 l & K @ M Z 7gjie.org.cn ~]# 
[root@node101.yinzhengjie.orgB & & 4.cn ~]# 
[root@M P a bnode101.yinzhengjie.org.cn ~]# rm -rf .ssh
[root@node101.yinzhe0 D G a t #ngjie.org/ M j Q 8 J l s q.cn ~]# 
[root@node101.yinzg x ( g hengjie.org.cn ~]# ll /etc/issue
-rwR _ 5 S p V D D-r--r--. 1 root root 23 Nov 23  2018 /etc/issue
[root@k N 8 * [node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.y  ccn ~]# ll backup/
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[rQ ; b k W u ( +oot@nodx D t $ _ N p 8e101.yinzhengji8 7 W D : D 2 ^e.org.cn ~]# cat shell/scp_expect.sp m Z i I _ 4 & ?h
#!/usr/bin/expect h d S     #留意,这一行需求写expect指令的履行途径
#
#***********************I y ; 5 } p o {********v ? v @ ] & I************F ! ` W s q P****************X 1 ~ }  B W h******` q I p ( l t f***
#A^ v Z _ F E Y kuthor:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-29
#FileName:        shell/scp_expect.sh
#URL:             http://www.cnblogs.c| y b ? F mom/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************
spawn] m 6 scp /etc/issue root@172.3] P c J U = =0.1.101:/? 7 / h ^ ) /root/backup
expect {
"yes/no" { send "yesn";exp_continue }
"password" { send "yinzhengjien" }
}
expect eof
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# chmod +x shell/scp_expect} C w p ` i K V.sh    #增加履行权限
[root@node101.yinx , ; R Fzhe# . M T ; sngjie.org.cn ~K m   ? O]# 
[root@node101.yinzhen1 w v P F ]gjie.org.cn ~]# ./) f x a : # r x ashell/scp_expect.sh         #履行脚本后,我们发现无需像之前那样手动输出"yes"和密码啦~取而代之的n 4 z Z是脚本帮我们干活了。
spawn sc+ E s w Ip /etc/issue roo| L ` - = Lt@172J n T e P I.30.1.1[ ` { S B z01:/root/backup
The authenp e T 7ticity of host '172.30.1.101 (172.30.1.101)' can/ e , y 9 1 u't be established.
ECDSA key fingerprint is SHA256:KEchoZnVBkijeoWfG2nvx2ptthsXv7IjkxIJY+ + # z ` . K . ule57g.
ECDSA key fingerprint is MD5:52:c8:f5:6e:5f:cf:44:ec:c4:11:60:d2H % - : p j : 0:d0:31:3c:da.
Are you sure you want to continue co q p # Y W % z ^onnecting (yes/no)? yes
Warning: Permanently ad. * H 7 } * l aded '172.30.1.101' (ECDSA) to the list of known hosts.
r@ 4 &  h , ooot@17z U S2.30.1.101's password6 7 & 0 U ? ( k:
issue                                                                                                                                     100%   23    38.2KB/s   00:00
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll backup
t] : 5 ] e 9 Z u Cotal 4
-rw-r--r-- 1 root root 23 Nov 29 19:21 issue
[roos e L t f Q d t@node101.- ( F Y ;yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yi% O U v gnzhengjie.org.cn ~]# cat shell/scp_expect.sh

2.interact~ P r X ) F f(答应用户交互)事例

[root@node101.yinzhengjie.org.cn ~]# cat shell/ssh_expecta ~ n a = s1.sh 
#!/r d [ B * ( Q C |usr/bin/expect
#
#********************************************************************
#Author:        yinzhenC v b @ W B V ( pgjie
#QQ:             1053419035
#Date:             2019-11-29
#FileName:        shell/scp_expect.sh
#UR/ y Q `  s GL:T : _ f ^ G w             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright noF 7 = T F Ftice:     original works, no reprint! Otherwise, legal liability will be investigated.
#******************************r I M N***************Y o z ;*****m r j I R*********E Z g ^ 3 * L 1 3*********
spawn ssh yinzhengjie@172.30.1.101
expect {
"yes/no" { send "$ T j 7yesn";exp_continue }
"password" { send "yinzhengjien5 : 1 l j 6 j ~" }
}
interact
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ./shell/ssh_expect1.sh 
spaX k ` p 7 . Cwn ssh yinzhengjie@172.30.1.101
yinzhengjie@17N Z j ? W2.30.1.101's password:
Last login: Sat Nov 30 17:05:32 2019 from 172.30.1.101z A y k * - O
[yinz4 K U @ F hengjie@noj f U E LdeI K / #101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.or: b Z *g.cn ~]$ who
yinzhengjie pts/1        2019-11-30 17:06 (172.30.1.101)
root     pts/3        2019-11-29 08:24 (172.30.1a g x T M ; 6 +.254)
root     pts/2        2G d 8 ! L $019-11-29 08:24 (172.30.1.254)
[yinzhengjie@node101.yinY _ x ` ~ ( 2zhengjie.org.cn ~]$
[yinzhengjie@nh ( 8 H s wode101.yinzhengjie.org.cn ~]$ exit
logout
Connection to 172. R p l [ g I M v30.1.101 closed.
[root@node101.y8 9 _ ( ` g ]inzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/ssh_expect1.sh

3.变量运用事例

[root@node101.yinzhengjie.org.cn ~]# cat shell/ssh_expect2.sh 
#!/usr/bin/] 5 6 Z 8 S C K .expect
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-29
#FileName:        shell/scp_expect.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Co+ s 9 x A s U ^pyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************
set host 172.30.1.101
set user yinzhv j 5engjie
set password yinzhengjie
spawn ssh ${user}@${host}
expect {
"yes/no" { send "yesn";exp_continue }
"password" { send "${password}n" }
}
interact
[root@node101.yinzhengjie.org.cn ~]# 
[rooM H qt@node101.yinzhengjJ 6 Vie.org.cn ~]# ./shell/. S U L ~ C 4ssh_eP 9 U mxpect2.sh 
spawn ssh yinzhengjie@17D e U2.30.1.101
yinzhengjie@172.30.1.101's password:] L * K D
Last login: Sat Nov 30 17:09:58 2019 from 172.30.1.101
[yinzhengjie@node101.yinzht ^ 5 q B  ~ Nengjie.org.cn ~]$
[yinzhengjie@node101.yinzhen~ k ogjie.org.cn ~]$ whoami
yinzhengjie
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhez : | P G !ngjie@node101.yinzhengjie.org.cn ~u r ; z y]$ exit
logout
Connection to 172.30.1.101 closed.
[root@node101.yinzhengjie.org.cn ~a ^ 6 W )]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/sshO { 0 j 4 ` s Q C_expect2.sh

4.方} * , X E [ 1位参数事例

[root@node101.yinzhengjie.org.cn ~]# cat shell/ssh_expect3.sh 
#!/usr/bin/expect
#
#*******************************************# r   u u E*************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-29
#FileName:        shell/scp_expect.sh
#URL:             http://www.cK I 7 %nblogi W 7 Y q ~s.com/yinzhengjie
#Description:        The test script
#Copyright notice:m J 9 s E ~ l     original works, no reprint! Otherwise, legal liability will be investigated.
#****~ j )**T z J |**************************************************************
set host [lindex $ar} 8 Hgv 0]
set5 a W s a user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh ${user}@${host}
expect {
"yes/no" { sendr U n !  c B y u "yesn";exp_continue }
"passwordD e / * + [ j U `" { send "${password}n" }
}
interact
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.Z 3 ` c D c f 9 Tcn ~]# ./shell: Y 8 Y d/ssh_expect3.sh 172.30.1.101 yinzhengjie yinzhengjie
spawn ssh yinzhengjie@172.30.1.101
yinzhengjie@172.30.1.j X h A101's password:
Last login: Sat Nov 30 17:10:27 2019 from 172.30.1.101
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhI Y p vengjZ E  0 sie@node101.yinzhengjie.org.cn ~]$ whoami
yinw e v { 9 bzhengjie
[yinzhengjie@node101.yinzhengjie.[ [ k ( }  V uorg.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ exit
logout
Connection to 172.30.1.101 closed.
[root@nodA B * E xe101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/ssh_expect3+ # E U o O p E.sh

5.登录后履行多个指令事例

[root@node101.yinzhen? Q ~ +gjie.org.cn ~]# cat shell/ssh_expect4.sh 
#!/usr/bin/expect
#
#**( r # m E 9 W*****************************d ) I ) & J******H - : ?*******************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019p K E h 1 + ^ w-11-29% = % # @ W 2 O $
#0 J % 9FileName:        shell/scp_expect.sh
#URL:             http://www.cnblogs.com/yinzheng( ) Z o R u G }jie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legaw 6 -  * }l liability wv L ;ill be investigatE I Med.
#**********************************************************! l ? u ( i -**********
set host [lindex $argv 0]
set user [lindex $argv 1]
set password [linde( K N 4x $argv 2]
set createuser [lindex $argv_ { z p 5 3]
se{ a r r { z at createpasswd [lindex $argv 4]
spawG R z J R _ xn ssh ${user}@${host}
expect {
"yc N nes/no" { send "yesn";= - v A n Lexp_continue }
"password" { send "${password}n" }
}
expect "]#" { send "useradd ${createuser}n" }
expect "]#" { send "echo ${createpasswd} |passwd --stdin ${createuser}n" }
expect "]#" { send "wU r * M Q - Uhon" }
send "exitn"
expect eof
[root@node101.yinzhf = | 5engjie.org.cn ~]# 
[root@node101.yil ? F N qnzhengjie.org.cn ~]#I C / + N v O 5 wz p S 7 _  ^ho
r[ F eoot     pts/3        2C  - U t G = 4 !019-11-29 08:24 (172.30.1.254)
root     pts/2        2019-11-29 08:24 (172.30.1.254)
[root@node101.yinzhengjie.oC q { jrg.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# id jason
id: jasonS 2 V  y 2 H p d: no su6 5 ? l f /ch user
[root@node101.yinzhengjie.org.cn ~]# 
[root@node1D [ - & ] g01.yinzhengjie.org.cn ~]# ./shell/ssh_expect4.sh 172.30.1.101 root yinzhengjie jason 123456
spawn ssh root@172.30.1.101
root@1b W s 9 8 #72.30.1.101's password:
Lam = & 0 P 7st login: Se w Oat Nov 30 17:23:11 2019 from 172.30.1.101
[r3 $ | w C d % g foot@node101.yinzhe_ F & y J Q Z Ongjie.org.cn ~]# useradd jason
[root@node101.yinzhengjie.org.cn ~]# echo 123456 |passwh * ] D P , f kd --stdin jason
Changing password for user jason.
passwd: all authentication tokens updated successfully.
[root@node101.yinzhengjie.org.cn ~]# who
root     pts/1        2019-11-30 17:2$ 2 w 77 (172.30.1.101)
root     pts/M H d ) U3        2019-11-29 08:24 (172.30.1.254)
root     pts/2        2019-11-29 08:24 (172.30.1.254)
[root@node101.yinzhengjie.org.cn ~]# exit
lA B i / n o % ] oogout
Connection to 172.30.1.101 closed.
[root@node101.yinzhengjie.org.cn ~]# who
root     pts/3        201^ l ` R & { _ * K9-11-29 08:24 (172.30.1.2s ^ 3 M ! r n g P54)
root     pts/2        2019-11Z ( Q-29 08:24 (172.30.1.254)
[root@node101.yinzhengjie.org.cn ~]#
[rooB c z H S V Y 2t@node101.yinzhengjie.or~ i 2 hg.cn ~]# id jason
uid=1049(jason) gid=1049v F (jason) groP  }ups=1049(jason)
[rX | k 6 F K .oot@nodB i Ve101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengj+ I a kie.org.cn ~]# cat shell/ssh_expect4.sh

6.shell脚本调用expL W * T x h Z | &ect

[root@node101.yinzhengjie.org.cn ~]# cat shell/4 + _ A ` # L n bssh_expeQ = } H N o q 1ct5.sh  
#!/bin/bash
#
#***********3 , % O p Z i * }**********************o B W 2 |***********************************
#Aut@ ~ - J Y g Ohor:        yi- 2 s ]nzhen1 8 i 2gjie
#QY r T kQ:             1053419035
#` | % M w m $Du r N , ` + Wate:             2019-11-30
#FileName:        shell/ssh_expect5.sh
#URL:             http://www.cnx j D Bblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability wil: ) ` 3 J A o -l be investigated.
#******************************************/ % h ] 6 - -**********G S 4 O****************
ip=$1
user=$2
paz r m z Is$ S % * asword=$3
expect <0 ~ y<EOF
set timeout 10
spawn ssh $user@$ip
exu s Z 7 D G Zpect {
"yes/no" { send "yesn";exp_continue }
"passwordT W e W 5 i { U" { se1 W = r Y R -nd "$passwordn" }
}
expect "]#" { send "useraI u c 4 Udd jason2019n" }
expect "]#" { send "echo yinzhengjie |passwd --stdin jason2019n" }
expect "]#" { send "whon" }
expect "]#" { send "exitn" }
expect eof
EOF
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yz m ) = A .inzhengjie.org.cn ~]# who
root     pts/3        2019-11-29 08:24 (1W D c h c W E K T72.30.1.X / g F ,254)
root     pts/2        2019-11-29 08:24 (172.30.1.254)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node1k l !01.yin+ $ D O @ Bzhengjie.org.c( B / T S h h Yn ~]# id jason2 ^ C / h + ~019
id: jason( # ( 4 B u . g2019: no such user
[root@node101.yinzhengj$ 1 , Mie.orw 6 V r 7 } Hg.cn ~] % c# 
[root@node101.yinzhengjie.org.cn ~]# bas( 7 N 8 K } Ch: ; H P a 0 shel- J .l/ssh_expect5.sh 172.30.1.101 root yinzhengjie
spawn ssh root@172.30.10 x * S j Z Q [.101
root@2 F T d $ = m172.30.1.101's password:
Las? H i 5t login: Sat Nov 30 17:27:57 2019 from 172.30.1.101
[root@node101.yinzhengjie.org.cn ~]# useradd jasoh ~ t , vn2019
[root@node101.yinzhengjie.org.cn ~]# echo` U / - S 4 yinzhengjie |pasJ e h & C 4 ? ( swd --stdin jason2019
Changing password for user jason2019.
pas] T t 4swd: a6 Y l A bll authentication tokens updated successfu~ n l vlly.
[root@node1b z 5 h $ F01.yinzhengjie.org.cn ~]# w( D H 8 % 9 2 cho
root     pts/1        2019-11-30 17:38F N l r $ , % w (172.30.1.101)
root     pts/3        2019-11-29 08:24 (172.30.1.254)
root     pts/2        2019-11-p f y 729 08:24h ` h ; m H ) ( e (172.30.1.254)
[root@node101.yinzhengjie.org.cn ~]# exit
logout
Connection to 172.30.1.B e 1 / b c * l101 closed.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.orgs b 1 G k z 4.cn ~]# id jason2019
uid=1050(jason2019) gid=1050(jason2019) groups=l s d R - ` K1050(jason2019)
[root@node101.yinzhe- d p c O t P sngjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# who
root     pts/3        2019-11-29 08:24 (172.30.1.254)
root     pts/2        2019-11-29 08:24 (17/  | s : { H 92.30.1.2~ R J m c ( f C54)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101f Y o S ( 5 =.yinzhengjie.org.cn ~]# cat shell/ssh_expect5.sh

7.模拟给集群批量创建用户的脚本

[roE Y rot@node101.yinzhengjie.org.cn ~]# cat shell/ssh_expect6.sh 
#!/bin/bash
#
#********************************_ H [ o 0 H ~****j  4 [********************************
#Author:        yinzhengjie
#QQ:             1053419Y $ L Y ( . ! o035
#Date:             2019-11-30
#FileName:        shell/ssO 1 F T e C gh_expect5.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        TW K Khe tes_ 0 G { : ] t = Ft script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigateA @ * W Q r 0 2 1d.
#***************2 v Z**************`  ! 0 & e******************************% & N P : = 0 8 L*********
while read ip ;do
user=ro3 $ X : A | @ot
password=yinzhengjie
expect <<EOF
se1 5 5 / ] ; Bt timeout 30
spawn ssh $user@$ip
expect {
"yes/nT 0 x R 4 E t  }o" { send "yesn";eC 3 + S exp_continue }
"password" { send "$passwordn" }
}
expect "]#" { send "useradd hadoopnY $ s 4 S" }
expect "]#" { send "echo yinzhengjie |passwd --stdin hadoopn" }
expect "]#" { send "whon" }
expect "]#" { send "exitn" }
expect eof
EOF
doneP | k # B &lb 2 ] +t; /root/shell/iplist.txt
[root@node101.yinzhen6 ( ] agjie.org.cn ~]# 
[root@node101.yinzhengjie.orZ s _ Gg.cn ~]# cat shell/iplist.txt      e . G #我们这里界说了需求连接集群的主机IP
172.30.1.1D L z U01
17` : E o ( . W $2.30.1.102
172.30.1.103
172.30.1.104
172.30.1.105
172.30.1.106
17b i ( H &2.30.1.107
172.30.1.108; } a P o y k U {
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/ssh_expect6.sH U Q K # Qh       #履行脚本后发现会有很多输出信息,因为我实验的时候之开机了172o Y B  E I.30.1.101这台虚拟机,其它虚拟机悉数关机了,结果脚本只在开机的虚拟机中履行成功啦,其它主机报错提示找不到主机路由。
spawn ssh root@172.30.1.101
rooD u P n @t@172.30.1~ 5 q B.101's password:
Last login: Sat Nov 30 17:38:48 2019 from 172.30.1.101
[root@node101.yinzhengjie.org.cn ~]# useradd hadoop
[s p J nroot@node101.yinzhengji c e k ,e.org.cn ~]# echo yinzhengjie |passwd --stdin hc w F # ( U 2 Kadooq O p 8 u | !p
Changing password for user hadoop.
passw; ` T g - Id: all authentication tokens updated successfully.
[root@node101.y6 P | z U M  inzhengjie.org.cn ~]# who
root     pts/1        2019-11-30 17:53 (172.30.1.101)
root     pts/3V t a , $        2019-11-29 08:24 (172.30.1.254)
root     pts/2        2019-11-29 08:24 (172.30.1.254)
[root@node101.yinzhengj 6 U 0 m y c 1jie.org.cn ~]# ep T S E jxit
logoS _ C V $ 1 j I @ut
Connection to 172.30.1.101 closed.
spc T  h y  = # 4awn ssh root@172.30.1.102
ssh: connect to host 172.30.1.102 port 22: No route to host
expect: spawn id exp6 not open
whiX 8 /le executing
"expect "]#" { send "usef - a $ 2radd hadoopn" }"
spawn ssh root@172.30.1.103
ssh: connect to host 172.30.1.1( @ * R ; q . *03 port 22: No route to host
expect: spawn id exp6 not open
while executing
"expect "]#" { send "useradd hadoopn" }"
spawn s[ ) s R S Z O t `sh root@172.30.1.104
ssh: connect to host 172.30.1.104 port 22: No route to host
expect: spawn id expI s w6 not openv w c { :
while executing
"expect "]#" { send "useradd hadoopn" }"
spawn ssh root@172.30.1.105
ssh: connect to host 172.Q E s v - 3 d z30.1.105 port 22: No route to host
expect: spaE c b O U 7wn id exp6 not open
while executing
"expect "]#" { send "userM L s $ s Yadd hadoopn" }"
spa[ G : J { |wn ssh root@172.30.1.106
ssh: connect to host 172.30.1.106 port 22: No route to host
expectb ^ b a: spawn id exH # b p ] R mp6 not open
while executing
"expect "]#" { send "useradd hadoopn" }"
spawn ssh root@172.30.1.107
ssh: connect to host 172. M h R @ n.30.1.107 port 22: No route to host
expect: spawn id exp6 not open
while executing
"expect "]#" { send "useradd h* v + $adoopn" }"
spawn ssh root@172.30.1.108
ssh: connect to host@ - Q G 172.30.1.108 port 22: No route to host
expect: spawn id eF w f Txp6 not open
while executing
"expect "]#"G F c 4 4 { send "useradd hadoopn" }"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# id hadoop y = P F        #很显然,只要172.30.1.101主机指令履行成功了。
uid=1051(hadoop) gid=1051(hadoop) groups=1051(hadoop)
[root@node101.yinzhengjie.org.cn ~]#
[root@c : H , l q K @ cnode101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/ssh_expect6.sh

shell脚本编程之expect