在一台linux机器上执行命令,Linux_Shell 利用ssh, expect 在多个机器上执行指令

相关的基础指令介绍

基础介绍文档

示例一:

多台机器上安装软件, 脚本需要以root 身份去执行

目录结构

0818b9ca8b590ca3270a3433284dd417.png

multiMain.sh

#!/bin/bash

##### the shell should run #############################

##### as role: root ############################

for((i=5;i<=16;i++));do

ssh 192.168.157.${i} "yum install expect.x86_64 -y"

done

实例二:

给多台机器 以  stm 角色  传送文件. 并切换到 root 角色去修改这些文件。

目录结构:

0818b9ca8b590ca3270a3433284dd417.png

multiMain.sh : 主程序

suRoot : 提升权限的脚本

test.txt : 需要被修改的测试文件

multiMain.sh

#!/bin/bash

##### the shell should run #############################

##### as role: stm ############################

for((i=5;i<=16;i++));do

echo "scp /home/stm/profile stm@192.168.157.${i}"

# ssh 192.168.157.${i} "yum install expect.x86_64 -y"

scp ./test.txt ./suRoot.sh stm@192.168.157.${i}:~/

done

for((i=5;i<=16;i++));do

echo "root@192.168.157.${i}"

ssh stm@192.168.157.${i} /bin/bash << EOF

cd /home/stm

source ./suRoot.sh

echo 789 >> ./test.txt

cat ./test.txt

rm -f ./suRoot.sh

exit

EOF

done

suRoot.sh   免输入提升至root 权限

#!/usr/bin/expect:/bin/bash

spawn su - root

#-c "cd /home/stm; echo 456 >> ./test.txt"

expect "Password: "

send "12345678\r"

interact

test.txt

123

456

效果:

0818b9ca8b590ca3270a3433284dd417.png

遇到的问题:

主要原因 expect 的内容与预期符合

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值