使用bash脚本使用列表将单个文件发送到多台计算机

时间:2014-09-30 17:13:04

标签: linux bash

首先要做的事情:谢谢大家的参与,并将我的知识提供给我的大部分缺乏。所以,我接受了David C. Rankin的建议并尝试重写他在这里提供的内容,并遇到了一些问题。

这是我尝试运行新代码时收到的终端输出。所有权限都是正确的,我还有其他可以执行的脚本。

[root@srvr-5 ~]# ls -al wakupMAC.sh 
-rwxrwxr-x 1 root root 2816 Oct  3 11:33 wakupMAC.sh
[root@srvr-5 ~]# 
[root@srvr-5 ~]# ./wakupMAC.sh 
bash: ./wakupMAC.sh: /bin.bash: bad interpreter: No such file or directory
[root@srvr-5 ~]#

这是我用它取代的代码。它应该是大卫的副本(大多数),但有些事情是错的,我不知道它是什么。我实际上有阵列中的109台机器可供选择,但为了尺寸减少了它。那我搞砸了什么?

#!/bin.bash
#wakeupMAC.sh
#Written Oct 3, 2014

#Array of 109 machine MAC addresses to wake up
WS=( E4:11:5B:31:25:22 
00:1c:23:47:5a:14 
00:1c:23:50:1a:c6 
00:1c:23:4a:d9:67 
00:1c:23:4b:2b:ad )

if [ "$1" = "-a" ] || [ "$1" = "--all" ]; then #Corrected
   for i in ${WS[@]}; do
      /sbin/ether-wake -i eth0 "$i"
     printf " called -> /sbin/ether-wake -i eth0 %s\n" "$i"
   done
   exit 0
fi

#Create prompt and a menu for selectable launch
PS3='Selection: '
while [ ! $name ]; do
   printf "\nSelect the WS number from the following menu to launch:\n\n"
   select name in ${WS[@]} All quit; do
      if [ "$name" = "All" ]; then
         for i in ${WS[@]}; do
            /sbin/ether-wake -i eth0 "$i"
            printf " called -> /sbin/ether-wake -i eth0 %s\n" "$i"
         done
         break
      elif [ "$name" = "quit" ]; then
         break
      else
         /sbin/ether-wake -i eth0 "$name"
    printf " called -> /sbin/ether-wake -i eth0 %s\n" "$name"
      fi
   done
done

exit 0

3 个答案:

答案 0 :(得分:0)

添加以下代码应该有效:

ARRAY=(E4:11:5B:31:78:1D   E4:11:5B:31:25:22     E4:11:5B:3B:60:4C       E4:11:5B:3A:6E:21      E4:11:5B:31:D5:2A       E4:11:5B:31:D6:61 )

echo 'List of machines: '
echo "E4:11:5B:31:78:1D   E4:11:5B:31:25:22     E4:11:5B:3B:60:4C       E4:11:5B:3A:6E:21      E4:11:5B:31:D5:2A       E4:11:5B:31:D6:61 "
echo 'Enter number of machine to wake up (0-5): '

read input1
echo "Waking up: " ${ARRAY[$(($input1))]}
#/sbin/ether-wake -i eth0  ${ARRAY[$(($input1))]}   # uncomment this to test

exit

答案 1 :(得分:0)

使用bash select menu功能提供了一个干净的解决方案:

#!/bin/bash

## array of machine mac addresses
macs=(  E4:11:5B:31:78:1D        
        E4:11:5B:31:25:22        
        E4:11:5B:3B:60:4C        
        E4:11:5B:3A:6E:21        
        E4:11:5B:31:D5:2A        
        E4:11:5B:31:D6:61 )

## accept -a or --all to wake all without menu
if [ "$1" = "-a" ] || [ "$1" = "--all" ]; then
    for i in ${macs[@]}; do
        /sbin/ether-wake -i eth0 "$i"
        printf "  called -> /sbin/ether-wake -i eth0 %s\n" "$i"
    done
    exit 0
fi

## set prompt for select menu
PS3='Selection: '

## create a select menu allowing individual or all to be awaken
while [ ! $name ]; do 
    printf "\nSelect the computer from the following menu:\n\n"
    select name in ${macs[@]} All quit; do 
        if [ "$name" = "All" ]; then
            for i in ${macs[@]}; do
                /sbin/ether-wake -i eth0 "$i"
                printf "  called -> /sbin/ether-wake -i eth0 %s\n" "$i"
            done
            break
        elif  [ "$name" = "quit" ]; then
            break
        else
            /sbin/ether-wake -i eth0 "$name"
            printf "  called -> /sbin/ether-wake -i eth0 %s\n" "$name"
        fi
    done
done

exit 0

<强>输出:

选择单个机器:

$ bash selectmac.sh

Select the computer from the following menu:

1) E4:11:5B:31:78:1D  4) E4:11:5B:3A:6E:21  7) All
2) E4:11:5B:31:25:22  5) E4:11:5B:31:D5:2A  8) quit
3) E4:11:5B:3B:60:4C  6) E4:11:5B:31:D6:61
Selection: 4
  called -> /sbin/ether-wake -i eth0 E4:11:5B:3A:6E:21
Selection: 6
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:D6:61
Selection: 8

选择所有机器:

$ bash selectmac.sh

Select the computer from the following menu:

1) E4:11:5B:31:78:1D  4) E4:11:5B:3A:6E:21  7) All
2) E4:11:5B:31:25:22  5) E4:11:5B:31:D5:2A  8) quit
3) E4:11:5B:3B:60:4C  6) E4:11:5B:31:D6:61
Selection: 7
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:78:1D
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:25:22
  called -> /sbin/ether-wake -i eth0 E4:11:5B:3B:60:4C
  called -> /sbin/ether-wake -i eth0 E4:11:5B:3A:6E:21
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:D5:2A
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:D6:61

从命令行唤醒所有内容:

$ bash selectmac.sh -a
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:78:1D
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:25:22
  called -> /sbin/ether-wake -i eth0 E4:11:5B:3B:60:4C
  called -> /sbin/ether-wake -i eth0 E4:11:5B:3A:6E:21
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:D5:2A
  called -> /sbin/ether-wake -i eth0 E4:11:5B:31:D6:61

答案 2 :(得分:0)

感谢大家,特别是大卫兰金,它有效。一个简单的sytax错误就是阻止它工作。

  

!/ bin.bash

我将点(。)修改为正确的斜杠(/)语法#!/bin/bash,并且它的工作方式应该如此。我知道新秀的错误。

只需花时间查看代码以查找错误的位置。我知道它必须是一个简单的东西,如空间或破折号或平等的地方,它不应该是。

但错误

  

[root @srvr-5~]#。/ wakupMAC.sh bash:./ wakupMAC.sh:/bin.bash:bad   解释器:没有这样的文件或目录

一直告诉我它不在代码的正文中。这导致我在

中复制和粘贴
#!/bin/bash
ERROR=165

来自另一个脚本。工作完成后,我仔细查看了差异,发现了语法错误。

我现在可以有选择地在100多台机器的网络上唤醒我想要的任何机器。