shell编程expect用法

shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd、scp

对自动部署免去用户交互很痛苦,expect能很好的解决这类问题。

expect的核心是spawn expect send set

spawn 调用要执行的命令


expect 等待命令提示信息的出现,也就是捕捉用户输入的提示:


send 发送需要交互的值,替代了用户手动输入内容


set 设置变量值


interact 执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。
expect eof 这个一定要加,与spawn对应表示捕获终端输出信息终止,类似于if....endif

expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了。

设置expect永不超时
set timeout -1

设置expect 300秒超时,如果超过300没有expect内容出现,则推出
set timeout 300

expect编写语法,expect使用的是tcl语法。

一条Tcl命令由空格分割的单词组成. 其中, 第一个单词是命令名称, 其余的是命令参数
cmd arg arg arg

$符号代表变量的值. 在本例中, 变量名称是foo.
$foo

方括号执行了一个嵌套命令. 例如, 如果你想传递一个命令的结果作为另外一个命令的参数, 那么你使用这个符号
[cmd arg]

双引号把词组标记为命令的一个参数. "$"符号和方括号在双引号内仍被解释
"some stuff"

大括号也把词组标记为命令的一个参数. 但是, 其他符号在大括号内不被解释
{some stuff}

反斜线符号是用来引用特殊符号. 例如:n 代表换行. 反斜线符号也被用来关闭"$"符号, 引号,方括号和大括号的特殊含义

expect使用实例

1。首先确认expect的包要安置。

#rpm -qa | grep expect

如果没有则需要下载安装,

#yum install expect

2.安装完成后,查看expect的路径,可以用

#which expect

/usr/bin/expect

3.编辑脚本
#vi autosu.sh
添加如下内容

4.确定脚本有可执行权限

chmod +x autosu.sh

5.执行脚本 expect autosu.sh 或 ./autosu.sh

expect常用脚本

登陆到远程服务器

scp拷贝文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/expect
set  timeout   10
set  host   [ lindex   $ argv   0 ]          //第1个参数,其它2,3,4参数类似
set  username   [ lindex   $ argv   1 ]
set  password   [ lindex   $ argv   2 ]
set  src _file   [ lindex   $ argv   3 ]
set  dest _file   [ lindex   $ argv   4 ]
spawn  scp   $ src _file   $ username @ $ host : $ dest_file
expect   {
& quot ; ( yes / no ) ? & quot ;
    {
     send   & quot ; yesn & quot ;
     expect   & quot ; * assword : & quot ;   {   send   & quot ; $ passwordn & quot ; }
}
& quot ; * assword : & quot ;
{
send   & quot ; $ passwordn & quot ;
}
}
expect   & quot ; 100 % & quot ;
expect  eof


#!/bin/bash


set timeout 30
expect<<-END
spawn ssh-keygen -t rsa
expect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send "\n"
expect "Overwrite (y/n)? "
send "\n"
expect eof
exit
END


for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
       echo $i
expect<<-END
   spawn ssh root@$i "mkdir /root/.ssh/"
   expect "password: "
   send "du77\n"
expect eof
exit
END
expect<<-END
   spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
   expect "password: "
   send "du77\n"
expect eof
exit
END
expect<<-END
   spawn ssh root@$i "touch /root/.ssh/authorized_keys"
   expect "password: "
   send "du77\n"
expect eof
exit
END
expect<<-END
   spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
   expect "password: "
   send "du77\n"
expect eof
exit
END
done








#!/bin/bash


set timeout 30
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
        echo $i
expect<<-END
   spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
   expect "password: "
   send "123@123\n"
expect eof
exit
END
expect<<-END
   spawn ssh root@$i "touch /root/.ssh/authorized_keys"
   expect "password: "
   send "123@123\n"
expect eof
exit
END
expect<<-END
   spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
   expect "password: "
   send "123@123\n"
expect eof
exit
END
done
  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值