Search code examples
pythonbashpowershellwake-on-lan

bash script maybe syntax error not sure trying a until loop


I am trying to code a wol script for my server. Which the raspberry pi executes every time when it's booting.

I guess its a syntax error but I don't now the solution so I am asking you. I am getting an error in line 5 but no clue how to correct it.

#!/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 ]
done

the wol.py is the script from Marc Balmar which sending the wol package

#!/usr/bin/env python
#coding: utf8

# Wake-On-LAN
#
# Copyright (C) 2002 by Micro Systems Marc Balmer
# Written by Marc Balmer, [email protected], 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

I wrote a similar code in PowerShell it was a lot easier I am literally trying to copy it or translate it to 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"
)
}

Solution

  • Firstly, use http://www.shellcheck.net to fix your syntax issues in script.

    Your syntaxes for command-substitution is wrong, with that fix and using exit code of ping directly in the until-loop

    until ping -c 1 192.192.168.222.5 &> /dev/null
    do
        python wol.py
        sleep 2
    done
    

    should solve your problem. The man page of ping says

    If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.