4.expect交互式工具

一、expect交互式工具

1、作用

  • 捕获交互式命令的输出,让交互式命令自动执行
[root@node01 ~]# yum install -y expect

[root@node01 ~]# rpm -q expect
expect-5.45-14.el7_1.x86_64

2、expect单独使用

#!/usr/bin/expect

set timeout 10

spawn passwd root

expect "New password:"
send "redhat\n"
expect "Retype new password:"
send "redhat\n"

expect eof

3、expect在shell脚本中的使用

[root@node01 work]# cat test02.sh 
#!/bin/bash
#

useradd joker

/usr/bin/expect << eof
set timeout 10
spawn passwd joker
expect "New password:"
send "123456\n"
expect "Retype new password:"
send "123456\n"
expect eof
eof

需求: 配置ssh免密

#!/bin/bash
#

# 判断是否存在密钥
if [ -f ~/.ssh/id_rsa ]; then
   rm -rf ~/.ssh/id_rsa*
fi

# 生成密钥对
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" &> /dev/null

# 判断known_hosts文件中是否包含对方主机的密钥
> ~/.ssh/known_hosts

for i in 11 12; do
/usr/bin/expect << eof
set timeout 10
spawn ssh-copy-id root@192.168.140.$i
expect "(yes/no)?"
send "yes\n"
expect "password:"
send "redhat\n"
expect eof
eof
done

echo "-------------------------------------------"
# 验证
for i in 11 12; do
   ssh root@192.168.140.$i hostname
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值