Linux SSH远程执行脚本

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: [email protected]
#######################################################################
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
...
相关推荐
cg501715 小时前
Spring Boot 的配置文件
java·linux·spring boot
暮云星影15 小时前
三、FFmpeg学习笔记
linux·ffmpeg
rainFFrain16 小时前
单例模式与线程安全
linux·运维·服务器·vscode·单例模式
GalaxyPokemon16 小时前
Muduo网络库实现 [九] - EventLoopThread模块
linux·服务器·c++
mingqian_chu16 小时前
ubuntu中使用安卓模拟器
android·linux·ubuntu
GalaxyPokemon17 小时前
Muduo网络库实现 [十] - EventLoopThreadPool模块
linux·服务器·网络·c++
自由鬼17 小时前
开源虚拟化管理平台Proxmox VE部署超融合
linux·运维·服务器·开源·虚拟化·pve
瞌睡不来18 小时前
(学习总结32)Linux 基础 IO
linux·学习·io
inquisiter18 小时前
UEFI镜像结构布局
linux·spring