当前位置:网站首页>Shell expect 实战案例
Shell expect 实战案例
2022-08-04 22:41:00 【51CTO】
1、需要向集群的机器中执行命令,或者修改某个配置
环境准备:三台虚拟机 ip 为 192.168.0.102 192.168.0.107 192.168.0.108
方法1采用ssh [email protected] “cmd”的方式
1) 开发expect自动交互命令,文件名为exec_cmd1.exp
#!/usr/bin/expect
set ip [lindex $argv 0]的位置参数传入ip
set cmd [lindex $argv 1]的第2个位置参数需要执行的命令
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.sh在各个多个主机上交互执行。
#!/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脚本传入expect设置的两个位置参数
done
方法2采用交互登录到命令行时发送命令执行
[[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
注意:一般生产环境都是先用普通用户登录,在切换root用户登录, 用户名和密码可以直接定义到expect脚本中
2、批量发送文件
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)开发循环批量发送脚本
[[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、批量在所有服务器上执行shell脚本
1)按照章节2批量发送文件的例子将将脚本发送到所有服务器的指定目录
2)按照章节1的在所有机器上执行命令 sh batch_exec_cmd1.sh “source /opt/108.sh”
边栏推荐
猜你喜欢

SSM整合完整流程讲解

现在学习次世代3D游戏建模还能找到高薪好工作吗

2022七夕程序员必备的表白黑科技(七夕限定款)

【2020】【Paper Notes】Metasurfaces: Multifunctional and Programmable——

Latex快速插入作者ORCID

If you can't get your heart, use "distributed lock" to lock your people

湖仓一体电商项目(五):内网穿透工具-网云穿

力扣24-两两交换链表中的节点——链表

软测人面试 ,HR 会问到哪些问题?学会涨薪3000+

测试薪资这么高?刚毕业20K,仅需3.5个月
随机推荐
各行各业都受到重创,游戏行业却如火如荼,如何加入游戏模型师职业
rk3399 驱动屏参的几种方式
good luck
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
轮播图动态渲染
阿里巴巴2022届秋招面试真题和答案!
【TCP/IP 五 ICMP】
力扣19-删除链表的倒数第 N 个结点——链表
养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习
单片机原理[一] 学好单片机必会的五张图
Leaflets of three bouquet of roses
年薪40W测试工程师成长之路,你在哪个阶段?
【3D建模制作技巧分享】ZBrush如何使用Z球
Acwing3593. 统计单词
边缘检测——(纯享版)
一招包治pycharm DEBUG报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode
rk3399-0.0 svc command
PowerBI scripture series
ANT1.7 download and configuration method
【游戏建模模型制作全流程】使用ZBrush制作骷髅王