expect模块的使用
第一步首先下载expect 模块,yum list |grep expect ,安装下面的模块.
expect.x86_64 5.44.1.15-5.el6_4 @base
expect-devel.x86_64 5.44.1.15-5.el6_4 @base
安装完成后,可以在程序which expect下面可以查看环境/usr/bin/expect,需要expect和tcl两个包
rpm -qa tcl
tcl-8.5.7-6.el6.x86_64
[root@ob ~]# rpm -qa | grep expect
expect-5.44.1.15-5.el6_4.x86_64
expect-devel-5.44.1.15-5.el6_4.x86_64
贴两个代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | cat test1.sh #!/usr/bin/expect SERVERS = "192.168.79.129 192.168.79.131" PASSWORD = 123456 auto_ssh_copy_id() { echo "2" expect - c " set timeout - 1 ; spawn scp - r / root / app root@$ 1 : / root / ; expect { * (yes / no) * {send - - yes\r;exp_continue;} * assword: * {send - - $ 2 \r;exp_continue;} eof {exit 0 ;} }"; } sh_copy_id_to_all() { echo "111" for SERVER in $SERVERS do auto_ssh_copy_id $SERVER $PASSWORD echo ${SERVER} done } sh_copy_id_to_all |
next code
1 2 3 4 5 6 7 8 9 10 11 12 13 | SERVER = "192.168.79.129 192.168.79.131" auto_zookp_cp() { expect { * yes / no * {send "yes\r" } expect { * password * {send "123456\r" } } for myserver in ${SERVER} do scp - r / root / app root@${myserver}: / root / auto_zookp_cp done |
use sh to execute the shell,otherwise it wiil produce an error.
时来天地皆同力,运去英雄不自由
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!