当前位置:网站首页>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”
边栏推荐
猜你喜欢
随机推荐
【2020】【论文笔记】超表面:多功能和可编程——
【3D建模制作技巧分享】ZBrush模型如何添加不同材质
SQL Server 调用 WebService
typeScript-部分应用函数
亿流量大考(3):不加机器,如何抗住每天百亿级高并发流量?
仪表板展示 | DataEase看中国:数据呈现中国资本市场
软测人面试 ,HR 会问到哪些问题?学会涨薪3000+
DREAMWEAVER8 部分问题解决方案
【项目实战】仿照Room实现简单管理系统
the warmest home
QT[一] 信号与槽
文章占位 文章占位
自从新来了个字节20K出来的,就见识到了什么是天花板
Controller层代码这么写,简洁又优雅!
阿里巴巴2022届秋招面试真题和答案!
PID控制器改进笔记之七:改进PID控制器之防超调设定
enumerate()函数
轮播图动态渲染
js中小数四则运算精度问题原因及解决办法
软件测试技术之如何编写测试用例(4)









