相关的基础指令介绍
基础介绍文档
示例一:
多台机器上安装软件, 脚本需要以root 身份去执行
目录结构
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 角色去修改这些文件。
目录结构:
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
效果:
遇到的问题:
主要原因 expect 的内容与预期符合