shell编程之Expect

2021-01-13
expect的安装

[root@host ~]# yum -y install expect

expect应答函数的语法
是一个免费的编程工具, 用来实现自动的交互式任务, 而无需人为干预. 说白了expect就是一套用来实现自动交互功能的软件

在实际工作中我们运行命令、脚本或程序时, 这些命令、脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行. 而利用expect则可以根据程序的提示, 模拟标准输入提供给程序, 从而实现自动化交互执行. 这就是expect

能够在工作中熟练的使用Shell脚本就可以很大程度的提高工作效率, 如果再搭配上expect,那么很多工作都可以自动化进行,对工作的展开如虎添翼

用法:
1)定义脚本执行的shell
    #!/usr/bin/expect 类似于#!/bin/bash

2)set timeout 30
    设置超时时间30s

3)spawn
    spawn是进入expect环境后才能执行的内部命令,不能直接在默认的shell环境中执行
    功能:传递交互命令

4)expect
    这里的expect同样是expect命令
    功能:判断输出结果是否包含某项字符串,没有则立即返回,否则等待一段时间后返回,等待通过timeout设置

5)send
    执行交互动作,将交互要执行的动作进行输入给交互指令
    命令字符串结尾要加上“r”,如果出现异常等待状态可以进行核查

6)interract
    执行完后保持交互状态,把控制权交给控制台
    如果不加这一项,交互完成会自动退出

7)exp_continue
    继续执行接下来的操作
expect环境中设置变量用set,识别不了bash方式定义的变量
错误方式:
[root@host expect]# cat expect02.sh 
#!/usr/bin/expect
user=22
spawn echo $user
[root@host expect]# ./expect02.sh 
invalid command name "user=22"
    while executing
"user=22"
    (file "./expect02.sh" line 3)
    
正确方式:    
[root@host expect]# cat ./expect02.sh
#!/usr/bin/expect
set user 22
spawn echo $user
[root@host expect]# ./expect02.sh 
spawn echo 22

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


[root@host expect]# cat expect01.sh 
#!/usr/bin/expect
spawn ssh root@192.168.62.146
expect {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "123\r" }
}
interact

interact的作用测试
执行测试是否免交互:
要是用/usr/bin/expect的shell执行
[root@host expect]#/usr/bin/expect expect01.sh
成功

擦除最后一行interact进行测试
[root@host expect]#/usr/bin/expect expect01.sh
进入之后立即退出
============================================================================

\r的作用测试
[root@host expect]# cat expect01.sh 
#!/usr/bin/expect
set user root
set pass 123
set ip 192.168.62.146
spawn ssh $user@$ip
expect {
        "yes/no" { send "ye";exp_continue }
        "password:" { send "pas" }
}
interact

[root@host expect]# ./expect01.sh 
spawn ssh root@192.168.62.146
root@192.168.62.146's password: 

加上\r之后
[root@host expect]# ./expect01.sh 
spawn ssh root@192.168.62.146
root@192.168.62.146's password: 
Permission denied, please try again.
root@192.168.62.146's password: 
============================================================================

设置变量的方式
[root@host expect]# cat expect01.sh 
#!/usr/bin/expect
set user root
set pass 123
set ip 192.168.62.146
spawn ssh $user@$ip
expect {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "$pass\r" }
}
interact

实现ssh自动登录----expect的常用实例

#!/usr/bin/env expect
# 
# Author: 

set password "123"
set username "root"

spawn ssh $username@192.168.161.130
expect "*yes/no*" {send "yes\r"}
expect "*password*" {send "$password"}

实现自动生成密钥

[root@host ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:C+buJw3dkFuyWCTXWGr9oSHjoaLeZzMfonA0Vj/BbW0 root@nginxserver02
The key's randomart image is:
+---[RSA 2048]----+
|         +.      |
|      ..++..     |
|      .+O.= E    |
|     . ==*.= .   |
|    = ++SB. .    |
|   + =o.+o.      |
|  o . ooo        |
| . + o*.o.       |
|  . o+o*.        |
+----[SHA256]-----+

[root@host ~]# vim no_enter_sshkey
#!/usr/bin/env expect
# 
# Author: 

spawn ssh-keygen
expect "*):*" {send "\r"}
expect "*passphrase*" {send "\r"}
expect "*again*" {send "\r"}
expect eof

将expect函数嵌入shell脚本

/usr/bin/expect <<-EOF
set password "123"
set username "root"
spawn ssh $username@192.168.161.130
expect "*yes/no*" {send "yes\r"}
expect "*password*" {send "$password"}
EOF
设置位置参数的方式(拓展)
[root@host expect]# cat expect01.sh 
#!/usr/bin/expect
set timeout 30
set user [ lindex $argv 0 ]
set pass [ lindex $argv 1 ]
set ip [ lindex $argv 2 ]
spawn ssh $user@$ip
expect {
        "yes/no" { send "yes";exp_continue }
        "password:" { send "$pass\r" }
}
interact
运行结果为:
[root@host expect]# ./expect01.sh root 123 192.168.62.146
spawn ssh root@192.168.62.146
root@192.168.62.146's password: 
Last login: Thu Aug 15 23:26:59 2019 from 192.168.62.136
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,744评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,505评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,105评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,242评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,269评论 6 389
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,215评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,096评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,939评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,354评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,573评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,745评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,448评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,048评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,683评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,838评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,776评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,652评论 2 354

推荐阅读更多精彩内容