magicCmd.sh
shell
#!/bin/bash
#######################################################################
## Shell Script: Remote Command Executor
## Usage: sh magicCmd.sh "commands".
## e.g: sh magicCmd.sh "pwd; ls -l -a"
## Author: kuro@jiayeli.com
#######################################################################
EXECMD=$1
function executorCMD() {
cmdStr=$1
OLDIFS=$IFS
IFS=';'
read -ra cmds <<< "$cmdStr"
IFS=$OLDIFS
for cmd in "${cmds[@]}"
do
printf "[`whoami`@'localhost']# $cmd\n"
#$cmd
done
}
function remoteExecutor() {
for i in {1..5}; do
node="node0$i"
ssh "$node" <<EOF
echo "--------------------------------------------------------"
echo "登录 $node"
echo "--------------------------------------------------------"
IFS=';' read -ra cmds <<< "$EXECMD"
for cmd in "\${cmds[@]}"; do
printf "[\$(whoami)@${node}]# \$cmd \n"
eval "\$cmd"
printf "\n"
done
exit
EOF
done
}
remoteExecutor
最终效果如下:
shell
[kuro@node01 scripts]# sh magicCmd.sh "pwd;ls /opt"
Pseudo-terminal will not be allocated because stdin is not a terminal.
--------------------------------------------------------
登录 node01
--------------------------------------------------------
[kuro@node01]# pwd
/root
[kuro@node01]# ls /opt
cloudera
data
datax
doris
druid-0.22.1
jmxPromExported
prometheus-2.52.0
root
software
Pseudo-terminal will not be allocated because stdin is not a terminal.
--------------------------------------------------------
登录 node02
--------------------------------------------------------
[kuro@node02]# pwd
/root
[kuro@node02]# ls /opt
cloudera
doris-1.0.0.tar.gz
output
Pseudo-terminal will not be allocated because stdin is not a terminal.
--------------------------------------------------------
登录 node03
--------------------------------------------------------
[kuro@node03]# pwd
/root
[kuro@node03]# ls /opt
cloudera
doris
doris-1.0.0.tar.gz
Pseudo-terminal will not be allocated because stdin is not a ter
...