Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在远程计算机上执行脚本_Bash - Fatal编程技术网

Bash 在远程计算机上执行脚本

Bash 在远程计算机上执行脚本,bash,Bash,我有很多脚本文件需要在远程机器上执行。脚本文件以服务器名称开头。它们位于/opt/local目录中 比如说 server1.sh server2.sh server3.sh 使用循环,我需要遍历每个文件 从列表中提取名称(例如server1) 在服务器名称后追加.example.com 1 sshuser1@server1.example.com 执行/opt/local/server1.sh 如果成功写入/opt/files/success.log,则会成功执行server1.sh 6.如果

我有很多脚本文件需要在远程机器上执行。脚本文件以服务器名称开头。它们位于/opt/local目录中

比如说

server1.sh
server2.sh
server3.sh
使用循环,我需要遍历每个文件

  • 从列表中提取名称(例如server1)
  • 在服务器名称后追加.example.com 1
  • sshuser1@server1.example.com
  • 执行/opt/local/server1.sh
  • 如果成功写入/opt/files/success.log,则会成功执行server1.sh 6.如果未成功写入/opt/files/errors.log,则server1.sh未成功,包括无法登录、无法执行脚本或类似操作
  • '''


    ''

    您的脚本可能如下所示:

    dir=/opt/local
    for file in "$dir"/*; do
         # extract name from the list (e.g. server1)
         name="${file%%.*}"
         # append .example.com after the name of the server1
         addr="${name}.example.com"
         # execute /opt/local/server1.sh
         if ssh "user1@$addr" "bash -s" < "$file"; then
              # if successful write to /opt/files/success.log server1.sh successfully executed 6
              printf "%s %s\n" "$name" "successfully executed 6" >> /opt/files/success.log
         else
             # if not successful write to /opt/files/errors.log, server1.sh not successful including unable to login, unable to execute script or something like this.
             printf "%s %s\n" "$name" "not successful including unable to login, unable to execute script or something like this" >> /opt/files/error.log
         fi
    done
    
    dir=/opt/local
    对于“$dir”/*中的文件;做
    #从列表中提取名称(例如server1)
    name=“${file%%.*}”
    #在服务器名称后追加.example.com 1
    addr=“${name}.example.com”
    #执行/opt/local/server1.sh
    如果ssh“user1@$addr”“bash-s”<“$file”;然后
    #如果成功写入/opt/files/success.log server1.sh,则成功执行6
    printf“%s%s\n”“$name”“已成功执行6”>>/opt/files/success.log
    其他的
    #如果未成功写入/opt/files/errors.log,则server1.sh未成功,包括无法登录、无法执行脚本或类似的操作。
    printf“%s%s\n”“$name”“未成功,包括无法登录、无法执行脚本或类似以下内容“>>/opt/files/error.log”
    fi
    完成
    
    您所拥有的看起来非常接近。我可以看到您在for循环中缺少glob
    *
    。写入日志文件怎么样?@jordanm,我如何写入日志?
    #!/bin/bash
    MYNAME=$(basename $0)
    WRKDIR=/opt/local
    SUF=".sh"
    # LOGDIR: create and set permissions for user before running this script
    LOGDIR="/var/log/$MYNAME"
    LOGFILE="$LOGDIR/$MYNAME-$(date '+%Y-%m-%d').log"
    exec 1>>$LOGFILE
    exec 2>&1
    find $WRKDIR -name "*$SUF" | while read SCRIPTNAME
    do
       SERVER=$(basename $SCRIPTNAME $SUF)
       echo "$(date): started $SERVER ..."
       URL="$SERVER.example.com"
       echo CMD: ssh user1@$URL "bash -s" $SCRIPTNAME \
       && ssh user1@$URL "bash -s" $SCRIPTNAME
       echo "$(date): done $SERVER"
       echo "------------------------------------"
    done
    
    dir=/opt/local
    for file in "$dir"/*; do
         # extract name from the list (e.g. server1)
         name="${file%%.*}"
         # append .example.com after the name of the server1
         addr="${name}.example.com"
         # execute /opt/local/server1.sh
         if ssh "user1@$addr" "bash -s" < "$file"; then
              # if successful write to /opt/files/success.log server1.sh successfully executed 6
              printf "%s %s\n" "$name" "successfully executed 6" >> /opt/files/success.log
         else
             # if not successful write to /opt/files/errors.log, server1.sh not successful including unable to login, unable to execute script or something like this.
             printf "%s %s\n" "$name" "not successful including unable to login, unable to execute script or something like this" >> /opt/files/error.log
         fi
    done