当前位置:网站首页>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”
边栏推荐
猜你喜欢
智慧养老整体解决方案
Will we still need browsers in the future?(feat. Maple words Maple language)
【3D建模制作技巧分享】ZBrush如何使用Z球
逆序对的数量
2022年全网最全接口自动化测试框架搭建,没有之一
各行各业都受到重创,游戏行业却如火如荼,如何加入游戏模型师职业
Reconfigure the ffmpeg plugin in chrome
关于el-table列表渲染
Rt-thread [三] link.lds链接脚本详解
【论文笔记KDD2021】MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
随机推荐
Qt中的常用控件
typeScript-promise
Pytest learning - fixtures
Shell编程之循环语句与函数的使用
2022年全网最全接口自动化测试框架搭建,没有之一
逆序对的数量
Linux系统重启和停止Mysql服务教程
promise详解
enumerate()函数
【转载】kill掉垃圾进程(在资源管理器占用的情况下)
MySQL的JSON 数据类型2
Use ngrok to optimize web pages on raspberry pi (1)
Redisson
软测人面试 ,HR 会问到哪些问题?学会涨薪3000+
Deep Learning RNN Architecture Analysis
PID控制器改进笔记之七:改进PID控制器之防超调设定
「津津乐道播客」#397 厂长来了:怎样用科技给法律赋能?
视频gif如何制作?试试这个视频制作gif神器
temp7777
VC bmp文件总结