当前位置:网站首页>Shell expect real cases
Shell expect real cases
2022-08-04 22:50:00 【51CTO】
1、The command needs to be executed to the machines in the cluster,Or modify a configuration
环境准备:三台虚拟机 ip 为 192.168.0.102 192.168.0.107 192.168.0.108
方法1采用ssh [email protected] “cmd”的方式
1) 开发expectautomatic interactive commands,文件名为exec_cmd1.exp
#!/usr/bin/expect
set ip [lindex $argv 0]The positional parameter is passed inip
set cmd [lindex $argv 1]的第2A command to execute with positional arguments
set password "zhangbichen"设置root的paasword
spawn ssh [email protected]$ip "$cmd"
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$password\r"}
}
expect eof
2) 使用shell循环在执行expect命令,batch_exec_cmd1.shInteractive execution on each of multiple hosts.
#!/usr/bin/bash
cmd=$1
if [ $# -ne 1 ]
then
echo $"usage:$0 cmd"
exit 1
fi
ip_list=(192.168.0.107 192.168.0.102 192.168.0.108)
for ip in ${ip_list[@]}
do
echo $ip
expect exec_cmd1.exp $ip "$cmd"执行expect脚本传入expectTwo positional parameters to set
done
方法2Send the command to execute when you log in to the command line interactively
[[email protected] scripts]# cat exec_cmd.exp
#!/usr/bin/expect
set host [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set root_password [lindex $argv 3]
spawn ssh [email protected]$host
set timeout 1
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$password\r"}
}
expect "$"
send "su - root\r"
expect "Password"
send "${root_password}\r"
expect "#" {send "cp /etc/profile /etc/profile.bak\r"}
expect eof
批量执行expect脚本
#!/urs/bin/bash
ip_list=(192.168.0.107 192.168.0.102)
for ip in ${ip_list[@]}
do
echo $ip
expect exec_cmd.exp $ip test01 yankefei yankefei
done
注意:The general production environment is to log in with a common user first,在切换root用户登录, Username and password can be defined directly to expect脚本中
2、Send files in batches
1)开发expect自动交互脚本
[[email protected] scripts]# cat send_file.exp
#!/usr/bin/expect
set file [lindex $argv 0]
set host [lindex $argv 1]
set remote_dir [lindex $argv 2]
set password "zhangbichen"
spawn scp -P22 -rp $file [email protected]$host:${remote_dir}
set timeout 1
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$password\r"}
}
expect eof
2)Development loop batch send script
[[email protected] scripts]# cat batch_send.sh
#!/urs/bin/bash
file=$1
remote_dir=$2
ip_list=(192.168.0.107 192.168.0.102)
for ip in ${ip_list[@]}
do
echo $ip
expect send_file.exp $file $ip ${remote_dir}
done
3、Batch execute on all serversshell脚本
1)按照章节2The batch file example will send the script to the specified directory on all servers
2)按照章节1to execute the command on all machines sh batch_exec_cmd1.sh “source /opt/108.sh”
边栏推荐
猜你喜欢
随机推荐
1、网页结构
xss总结
质量管理大师爱德华·戴明博士经典的质量管理14条原则
Service Mesh落地路径
How to make a video gif?Try this video making gif artifact
深度学习 RNN架构解析
panic: reflect: reflect.Value.SetString using value obtained using unexported field
BUG | 接口返回异常数据
双非读者,一举拿下阿里、字节、美团、京东、虾皮offer
360市值四年蒸发3900亿,政企安全能救命吗?
2022七夕程序员必备的表白黑科技(七夕限定款)
[Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
【3D建模制作技巧分享】zbrush贴图映射小技巧
Go 编程语言(简介)
线上虚拟展馆展示具有哪些优势
Pytest learning - fixtures
【3D建模制作技巧分享】ZBrush如何重新拓扑
养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习
使用代理对象执行实现类目标方法异常
Latex fast insert author ORCID









