您的位置:首页 > 运维架构 > Shell

shell中的expect软件(自动交互式程序)的使用

2017-12-13 20:45 405 查看
expect的使用:
例:
for i in {1..254}
do
ssh 172.40.51.$i “rm -rf /*”
done
提示:手动输入密码

例:
免交互发邮件(内容12
Qwe
Asd
Zxc

mail -s Error root << EOF
12
Qwe
Asd
Zxc
EOF

fdisk /dev/vda <<EOF
p
q
EOF

spawn 监视屏幕
expect 期待(等待 )
\n 回车

expect的问题
~/.ssh/known_hosts(存放连接过的所有主机的信息)
1.有yes提示有时没有
删除改文件
2.ssh比较慢
1)加快ssh(修改ssh配置文件)
2)让expect多等待一会
set timeout 30
3.expect最后一条命令不执行
远程到192.168.4.207创建一个叫qq的文件
#!/bin/bash
rm /root/.shh/known_hosts
i=192.168.4.207
expect << EOF
spawn ssh $i
set timeout 60
expect yes { send "yes\n"}
expect password { send "123\n"}
expect # { send "touch /qq\n"}
expect # { send "exit\n"}
EOF

字符串处理:
${变量:0:2}截取 ${变量/旧/新} ${ // / }替换
${变量#:}掐头 ${变量%:}去尾 ${ ## } ${ %% }
数组:a=(1 2 3 4) ${a[0]} ${a[*]}
expect自动交互软件
yum -y install expect
vim test.sh
expect << EOF
spawn fdisk /dev/vda
expect : {send “p\n”}
expect :{send “exit\n”}
EOF
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell expect 使用