当前位置:网站首页>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”
边栏推荐
猜你喜欢
随机推荐
历史上的今天:PHP公开发布;iPhone 4 问世;万维网之父诞生
各行各业都受到重创,游戏行业却如火如荼,如何加入游戏模型师职业
FinClip崁入式搭建生态平台,降低合作门槛
good luck
今天是七夕,来看看程序员的土味情话。
How to right use of WebSocket in project
MQTT[一]基础知识介绍
得不到你的心,就用“分布式锁”锁住你的人
【无标题】
Shell编程之循环语句与函数的使用
逆序对的数量
PAN3020 Sub-1G无线收发芯片
自从新来了个字节20K出来的,就见识到了什么是天花板
【2020】【论文笔记】超表面:多功能和可编程——
线性DP(下)
DREAMWEAVER8 part of the problem solution
How to make a video gif?Try this video making gif artifact
视频gif如何制作?试试这个视频制作gif神器
3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊
2022精选最新金融银行面试真题——附带答案









