你好,伙计们,我正在为我的服务器编写一个wol脚本。当它的启动时,覆盆子圆周率每次执行。
我想这是一个语法错误,但我现在没有解决方案,所以我问你。我在第5行得到一个错误,但不知道如何纠正它。
#!/bin/bash
nas=[ping -c 1 192.192.168.222.5 &> /dev/null ]
until [ $nas = "1" ];do
python wol.py
sleep 2
nas=[ping -c 1 192.192.168.222.5 &> /dev/null ]
donewol.py是marc balmar的脚本,它发送wol包。
#!/usr/bin/env python
#coding: utf8
# Wake-On-LAN
#
# Copyright (C) 2002 by Micro Systems Marc Balmer
# Written by Marc Balmer, marc@msys.ch, http://www.msys.ch/
# Modified by saarnu for nerdoskop.wordpress.com
# This code is free software under the GPL
import struct, socket, time, os
def WakeOnLan(ethernet_address):
# Construct a six-byte hardware address
addr_byte = ethernet_address.split(':')
hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
int(addr_byte[1], 16),
int(addr_byte[2], 16),
int(addr_byte[3], 16),
int(addr_byte[4], 16),
int(addr_byte[5], 16))
# Build the Wake-On-LAN "Magic Packet"...
msg = '\xff' * 6 + hw_addr * 16
# ...and send it to the broadcast address using UDP
time.sleep(5)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(msg, ('<broadcast>', 9))
s.close()
WakeOnLan('C8:60:00:6D:CF:54') # MAC-Adresse der DiskStation我用powershell编写了类似的代码--这要简单得多--我真的想要复制它或者把它翻译成bash。
$NAS = test-connection -count 1 -quiet 192.168.222.5
if ($NAS -like "False"){
do
{
$Mac = "C8:60:00:6D:CF:54"
$MacByteArray = $Mac -split "[:-]" | ForEach-Object { [Byte] "0x$_"}
[Byte[]] $MagicPacket = (,0xFF * 6) + ($MacByteArray * 16)
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7)
$UdpClient.Send($MagicPacket,$MagicPacket.Length)
$UdpClient.Close()
Start-Sleep -s 5
$NAS = test-connection -count 1 -quiet 192.168.222.5
}
until
(
$NAS -like "True"
)
}发布于 2017-01-20 17:35:47
首先,使用http://www.shellcheck.net修复脚本中的语法问题。
用于命令替换的语法是错误的,使用ping的退出代码直接在until-loop中进行修复。
until ping -c 1 192.192.168.222.5 &> /dev/null
do
python wol.py
sleep 2
done应该能解决你的问题。man的ping页面说
如果ping完全没有收到任何回复数据包,那么它将使用代码1退出。如果指定了数据包计数和截止日期,并且在截止日期到来之前收到的数据包少于计数数据包,则它也将退出代码1。如果出现其他错误,则退出代码2。否则退出时使用代码0。这样就可以使用退出代码来查看主机是否还活着。
发布于 2017-01-20 17:51:28
嘿,你的bash脚本有几个错误,而不仅仅是一个。
#!/bin/bash
# This is a silent ping. The output is redirected to /dev/null. The only visible
# change in user-land will be the exit code, available as $?.
# The exit code "0" means success, the exit code "1" means some error.
ping -c 1 192.192.168.222.5 &> /dev/null
# You need double round parenthesis here to evaluate a comparison.
# Also a comparison, as almost everywhere in programming, is "==" instead of "=".
until (( $? == "0" ));
do
python wol.py
sleep 2
# There is the silent ping again. Updating $?.
ping -c 1 192.192.168.222.5 &> /dev/null
done有关退出代码,请参阅:Exit Shell Script Based on Process Exit Code
对于不同的括号/括号,请参阅通过man bash、How to use double or single brackets, parentheses, curly braces或https://stackoverflow.com/a/9666169/7236811编写的真正好的bash手册。
https://stackoverflow.com/questions/41769332
复制相似问题