Shell script สำหรับ toolwiz time freeze

จาก Wiki Opensource

ปรับปรุงครั้งล่าสุดวันที่ 3-9-2562
เขียนโดย วิบูลย์

  • ทดสอบกับ Ubuntu server 18.04
  • ทดสอบกับ Bash for Windows 10 พบว่า รัน shell script นี้ไม่ได้เพราะไม่มีคำสั่ง arp
  • shell script นี้จะติดต่อกับ Windows ที่ติดตั้ง Cygwin for Windows และเปิด Cygwin sshd แล้วเท่านั้น และ toolwiz time freeze ติดตั้งใน default directory คือ c:\Program Files\Toolwiz\Time Freeze 2017
  • พบปัญหาว่าหาก Lock หรือ Unlock Windows ในเวลาที่ Windows ทำการ Update ก็จะทำให้เกิด Blue Screen of death ได้


ขั้นตอน
1. ให้ download ตัวติดตั้ง shell scripts ดังนี้
wget ftp.psu.ac.th/pub/psu-installer/toolwiztimefreeze/installttf.sh

2. ให้ทำคำสั่งนี้
bash installttf.sh --dir ชื่อไดเรกทอรีใหม่
รันคำสั่งนี้เพื่อ download files เหล่านี้ sshkeygenforserver.sh, sshkeysendtoclient.sh, ttf-unlock.sh, ttf-lock.sh, ttf-killps.sh, room-mac.txt, shutdownclient.sh, rebootclient.sh และ wakeonlanclient.sh ลงในไดเรกทอรีใหม่นั้น
เช่นจะตั้งชื่อว่า ttf ก็รันคำสั่งว่า bash installttf.sh --dir ttf

3. ให้ใช้คำสั่ง change directory เข้าไปใน directory ใหม่นั้น เช่น cd ttf แล้วทำรันคำสั่งต่าง ๆ ได้

4. ให้คีย์ mac address ของห้อง ลงในไฟล์ room-mac.txt ถ้ามีอีกห้องก็ room-mac-2.txt

คำสั่งในการทำงานมีดังนี้

5. ใช้คำสั่ง bash sshkeygenforserver.sh เพื่อสร้าง ssh key ของ user ที่กำลังใช้งาน (ทำครั้งเดียวพอ)
กดคีย์ Enter ทุกคำถาม
จะได้

you@ubuntu:~/ttf$ ls -l ~/.ssh
total 16
-rw------- 1 you you 1675 Jun 28 16:02 id_rsa
-rw-r--r-- 1 you you  393 Jun 28 16:02 id_rsa.pub


ในข้อต่อไป หากติดตั้ง Toolwiz Time Freeze ไว้ก่อนแล้ว และ Lock ไว้แล้ว ก็ต้องไป manual unlock เพื่อจะได้ส่ง ssh key ได้

6. ใช้คำสั่ง bash sshkeysendtoclient.sh เพื่อส่ง id_rsa.pub ไปยัง client (Windowsต้นฉบับ)
หลังจากส่ง ssh key แล้ว จึงติดตั้งโปรแกรม Toolwiz Time Freeze 2017 ให้เสร็จ
จะได้สถานะสุดท้ายว่า Freeze (Lock) หรือ Unfreeze (Unlock) ขึ้นอยู่กับว่าทำตั้งค่าในโปรแกรมหลังจากติดตั้งโปรแกรมไว้อย่างไร

7. เมื่อต้องการ Unlock toolwiz time freeze ของเครื่องที่มี mac อยู่ในไฟล์ room-mac.txt ทำดังนี้
bash ttf-unlock.sh --file room-mac.txt

การใช้งานรูปแบบอื่น ๆ คำสั่ง bash ttf-unlock.sh จะมี help ดังนี้

Usage.1: bash ttf-unlock.sh --mac 08:00:27:11:22:33
 Unlock only one mac address.

Usage.2: bash ttf-unlock.sh --file path/to/filename
 path/to/filename is a file consists of one mac address per line i.e.
 08:00:27:11:22:33
 08:00:27:44:55:66

Usage.3: bash ttf-unlock.sh --auto
 Automatically find IP and mac address via command arp -e and unlock.


8. เมื่อต้องการ Lock toolwiz time freeze ของเครื่องที่มี mac อยู่ในไฟล์ room-mac.txt ทำดังนี้
bash ttf-lock.sh --file room-mac.txt

การใช้งานรูปแบบอื่น ๆ คำสั่ง bash ttf-lock.sh จะมี Help ดังนี้

Usage.1: bash ttf-lock.sh --mac 08:00:27:11:22:33
 Lock only one mac address.

Usage.2: bash ttf-lock.sh --file path/to/filename
 path/to/filename is a file consists of one mac address per line i.e.
 08:00:27:11:22:33
 08:00:27:44:55:66

Usage.3: bash ttf-lock.sh --auto
 Automatically find IP and mac address via command arp -e and lock.


9. ให้ใช้คำสั่งเพื่อล้าง Process ของ toolwiz time freeze ที่ค้างอยู่
bash ttf-killps.sh

นอกจากนี้ ก็ได้เตรียม shell script สำหรับ shutdown, reboot และ wakeonlan ไว้ให้ด้วย
10. ถ้าต้องการ shutdown PC ทำคำสั่งนี้
bash shutdownclient.sh

11. ถ้าต้องการ reboot PC ทำคำสั่งนี้
bash rebootclient.sh

12. ถ้าต้องการ wakeonlan PC ทำคำสั่งนี้
bash wakeonlanclient.sh

Source code ไฟล์
ttf-unlock.sh

#!/bin/bash

#Last updated: 1-08-2562 by Wiboon Warasittichai

#This script will unlock 1 mac address or unlock all mac addresses in a file.
#Also provided an auto mode to automatically find IP in network and unlock it.

exitusage()
{
  echo -e "\033[1;36mUsage.1:\033[0m \033[1;33mbash $0 --mac 08:00:27:11:22:33\033[0m"
  echo " Unlock only one mac address."
  echo " "
  echo -e "\033[1;36mUsage.2:\033[0m \033[1;33mbash $0 --file path/to/filename\033[0m"
  echo " path/to/filename is a file consists of one mac address per line i.e."
  echo " 08:00:27:11:22:33"
  echo " 08:00:27:44:55:66"
  echo " "
  echo -e "\033[1;36mUsage.3:\033[0m \033[1;33mbash $0 --auto\033[0m"
  echo " Automatically find IP and mac address via command arp -e and unlock."
  echo " "
  exit
}

test=" "

if [ "$1" = "--help" ] ; then
  exitusage
fi
if [ -z "$1" ] ; then
  exitusage
fi

case $1 in
--mac)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a mac address.\033[0m"
    exitusage
  else
    macaddr=$2
    test=mac
  fi;;
--file)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a filename.\033[0m"
    exitusage
  else
    filename=$2
    test=file
    if [ ! -f ${filename} ] ; then
      echo -e "\033[0;31mFile not found.\033[0m"
      exit
    fi
  fi;;
--auto)
  test=auto;;
*)
  exitusage;;
esac

echo -n "(1/2)Enter Windows username: "
read user

echo -n "(2/2)Enter Toolwiz Time Freeze Password:"
read -p " " password

while true; do
  echo -n "Are you ready to continue unlock PC? [y = continue, n = exit]"
  read -p " (y/n)" yn
  case $yn in
   [Yy]* ) CONFIRM_ANS="yes"; break;;
   [Nn]* ) CONFIRM_ANS="no"; break;;
       * ) echo "Please answer y or n.";;
  esac
done

if [ "${CONFIRM_ANS}" = "no" ]; then
  exit
else
  echo "Go.."
fi

case $test in
  mac)
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "No mac address $macaddr"
          echo "..."
        else
          echo "Unlock Toolwiz Time Freeze at $macaddr $ip"
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip  "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /unfreeze /usepass=$password" > /dev/null 2>&1 &
          echo "..."
        fi
        ;;
  file)
    for macaddr in $(cat ${filename}|grep -v '#'|grep -E -o '[[:xdigit:]]{2}(:[[:xdigit:]]{2}){5}')
    do
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "Not seen mac address $macaddr"
          echo "..."
        else
          echo "Unlock Toolwiz Time Freeze at $macaddr $ip"
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip  "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /unfreeze /usepass=$password" > /dev/null 2>&1 &
          echo "..."
          sleep 1
        fi
    done
    ;;
  auto)
    gateway=$(ip route | grep default | awk '{print $3}')
    arp -e | grep -v 'gateway\|Address\|incomplete' | awk '{print $1 "-" $3}' > /tmp/arp.$$
    for line in $(cat /tmp/arp.$$)
    do
        ip=$(echo $line | awk -F- '{print $1}')
        macaddr=$(echo $line | awk -F- '{print $2}')
        if [ -z "$ip" ]; then
          echo "Not seen $macaddr"
          echo "..."
        else
          if [ ! "$ip" = "$gateway" ]; then
            echo "Unlock Toolwiz Time Freeze at $macaddr $ip"
            ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip  "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /unfreeze /usepass=$password" > /dev/null 2>&1 &
            echo "..."
            sleep 1
          fi
        fi
    done
    rm -f /tmp/arp.$$
    ;;
esac
# End.


ttf-lock.sh

#!/bin/bash

#Last updated: 1-08-2562 by Wiboon Warasittichai

#This script will lock 1 mac address or lock all mac addresses in a file.
#Also provided an auto mode to automatically find IP in network and lock it.

exitusage()
{
  echo -e "\033[1;36mUsage.1:\033[0m \033[1;33mbash $0 --mac 08:00:27:11:22:33\033[0m"
  echo " Lock only one mac address."
  echo " "
  echo -e "\033[1;36mUsage.2:\033[0m \033[1;33mbash $0 --file path/to/filename\033[0m"
  echo " path/to/filename is a file consists of one mac address per line i.e."
  echo " 08:00:27:11:22:33"
  echo " 08:00:27:44:55:66"
  echo " "
  echo -e "\033[1;36mUsage.3:\033[0m \033[1;33mbash $0 --auto\033[0m"
  echo " Automatically find IP and mac address via command arp -e and lock."
  echo " "
  exit
}

test=" "

if [ "$1" = "--help" ] ; then
  exitusage
fi
if [ -z "$1" ] ; then
  exitusage
fi

case $1 in
--mac)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a mac address.\033[0m"
    exitusage
  else
    macaddr=$2
    test=mac
  fi;;
--file)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a filename.\033[0m"
    exitusage
  else
    filename=$2
    test=file
    if [ ! -f ${filename} ] ; then
      echo -e "\033[0;31mFile not found.\033[0m"
      exit
    fi
  fi;;
--auto)
  test=auto;;
*)
  exitusage;;
esac

echo -n "(1/2)Enter Windows username: "
read user

echo -n "(2/2)Enter Toolwiz Time Freeze Password:"
read -p " " password

while true; do
  echo -n "Are you ready to continue lock PC? [y = continue, n = exit]"
  read -p " (y/n)" yn
  case $yn in
   [Yy]* ) CONFIRM_ANS="yes"; break;;
   [Nn]* ) CONFIRM_ANS="no"; break;;
       * ) echo "Please answer y or n.";;
  esac
done

if [ "${CONFIRM_ANS}" = "no" ]; then
  exit
else
  echo "Go.."
fi

case $test in
  mac)
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "No mac address $macaddr"
          echo "..."
        else
          echo "Lock Toolwiz Time Freeze at $macaddr $ip"
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /freeze /usepass=$password" > /dev/null 2>&1 &
          echo -n 3..
          sleep 3
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /freezealways /usepass=$password" > /dev/null 2>&1 &
          echo -n 2..
          sleep 3
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -r -f -t 0" > /dev/null 2>&1 &
          echo "1"
          echo "..."
        fi
        ;;
  file)
    for macaddr in $(cat ${filename}|grep -v '#'|grep -E -o '[[:xdigit:]]{2}(:[[:xdigit:]]{2}){5}')
    do
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "Not seen mac address $macaddr"
          echo "..."
        else
          echo "Lock Toolwiz Time Freeze at $macaddr $ip"
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /freeze /usepass=$password" > /dev/null 2>&1 &
          echo -n 3..
          sleep 3
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /freezealways /usepass=$password" > /dev/null 2>&1 &
          echo -n 2..
          sleep 3
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -r -f -t 0" > /dev/null 2>&1 &
          echo 1
          echo "..."
        fi
    done
    ;;
  auto)
    gateway=$(ip route | grep default | awk '{print $3}')
    arp -e | grep -v 'gateway\|Address\|incomplete' | awk '{print $1 "-" $3}' > /tmp/arp.$$
    for line in $(cat /tmp/arp.$$)
    do
        ip=$(echo $line | awk -F- '{print $1}')
        macaddr=$(echo $line | awk -F- '{print $2}')
        if [ -z "$ip" ]; then
          echo "Not seen $macaddr"
          echo "..."
        else
          if [ ! "$ip" = "$gateway" ]; then
            echo "Lock Toolwiz Time Freeze at $macaddr $ip"
            ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /freeze /usepass=$password" > /dev/null 2>&1 &
            echo -n 3..
            sleep 3
            ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "/cygdrive/c/Program\ Files/Toolwiz\ Time\ Freeze\ 2017/ToolwizTimeFreeze.exe /freezealways /usepass=$password" > /dev/null 2>&1 &
            echo -n 2..
            sleep 3
            ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -r -f -t 0" > /dev/null 2>&1 &
            echo 1
            echo "..."
          fi
        fi
    done
    rm -f /tmp/arp.$$
    ;;
esac
# End.


ttf-killps.sh

ps a | grep Toolwiz | awk '{print $1}' | xargs kill -9 > /dev/null 2>&1


room-mac.txt

11:60:77:a9:02:c4
11:60:cb:99:37:a0


rebootclient.sh

#!/bin/bash

#Last updated: 7-08-2562 by Wiboon Warasittichai

#This script will reboot 1 mac address or all mac addresses in a file.
#Also provided an auto mode to automatically find IP in network and reboot it.

exitusage()
{
  echo -e "\033[1;36mUsage.1:\033[0m \033[1;33mbash $0 --mac 08:00:27:11:22:33\033[0m"
  echo " Reboot client only one mac address."
  echo " "
  echo -e "\033[1;36mUsage.2:\033[0m \033[1;33mbash $0 --file path/to/filename\033[0m"
  echo " path/to/filename is a file consists of one mac address per line i.e."
  echo " 08:00:27:11:22:33"
  echo " 08:00:27:44:55:66"
  echo " "
  echo -e "\033[1;36mUsage.3:\033[0m \033[1;33mbash $0 --auto\033[0m"
  echo " Automatically find IP and mac address via command arp -e and reboot client."
  echo " "
  exit
}

test=" "

if [ "$1" = "--help" ] ; then
  exitusage
fi
if [ -z "$1" ] ; then
  exitusage
fi

case $1 in
--mac)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a mac address.\033[0m"
    exitusage
  else
    macaddr=$2
    test=mac
  fi;;
--file)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a filename.\033[0m"
    exitusage
  else
    filename=$2
    test=file
    if [ ! -f ${filename} ] ; then
      echo -e "\033[0;31mFile not found.\033[0m"
      exit
    fi
  fi;;
--auto)
  test=auto;;
*)
  exitusage;;
esac

echo -n "Enter Windows username: "
read user

while true; do
  echo -n "Are you ready to continue reboot PC? [y = continue, n = exit]"
  read -p " (y/n)" yn
  case $yn in
   [Yy]* ) CONFIRM_ANS="yes"; break;;
   [Nn]* ) CONFIRM_ANS="no"; break;;
       * ) echo "Please answer y or n.";;
  esac
done

if [ "${CONFIRM_ANS}" = "no" ]; then
  exit
else
  echo "Go.."
fi

case $test in
  mac)
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "No mac address $macaddr"
          echo "..."
        else
          echo "Reboot client at $macaddr $ip"
          echo -n 3..
          sleep 1
          echo -n 2..
          sleep 1
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -r -f -t 0" > /dev/null 2>&1 &
          echo "1"
          echo "..."
        fi
        ;;
  file)
    for macaddr in $(cat ${filename}|grep -v '#'|grep -E -o '[[:xdigit:]]{2}(:[[:xdigit:]]{2}){5}')
    do
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "Not seen mac address $macaddr"
          echo "..."
        else
          echo "Reboot client at $macaddr $ip"
          echo -n 3..
          sleep 1
          echo -n 2..
          sleep 1
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -r -f -t 0" > /dev/null 2>&1 &
          echo 1
          echo "..."
        fi
    done
    ;;
  auto)
    gateway=$(ip route | grep default | awk '{print $3}')
    arp -e | grep -v 'gateway\|Address\|incomplete' | awk '{print $1 "-" $3}' > /tmp/arp.$$
    for line in $(cat /tmp/arp.$$)
    do
        ip=$(echo $line | awk -F- '{print $1}')
        macaddr=$(echo $line | awk -F- '{print $2}')
        if [ -z "$ip" ]; then
          echo "Not seen $macaddr"
          echo "..."
        else
          if [ ! "$ip" = "$gateway" ]; then
            echo "Reboot client at $macaddr $ip"
            echo -n 3..
            sleep 1
            echo -n 2..
            sleep 1
            ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -r -f -t 0" > /dev/null 2>&1 &
            echo 1
            echo "..."
          fi
        fi
    done
    rm -f /tmp/arp.$$
    ;;
esac
# End.


shutdownclient.sh

#!/bin/bash

#Last updated: 19-07-2562 by Wiboon Warasittichai

#This script will shutdown 1 mac address or all mac addresses in a file.
#Also provided an auto mode to automatically find IP in network and shutdown it.

exitusage()
{
  echo -e "\033[1;36mUsage.1:\033[0m \033[1;33mbash $0 --mac 08:00:27:11:22:33\033[0m"
  echo " Shutdown client only one mac address."
  echo " "
  echo -e "\033[1;36mUsage.2:\033[0m \033[1;33mbash $0 --file path/to/filename\033[0m"
  echo " path/to/filename is a file consists of one mac address per line i.e."
  echo " 08:00:27:11:22:33"
  echo " 08:00:27:44:55:66"
  echo " "
  echo -e "\033[1;36mUsage.3:\033[0m \033[1;33mbash $0 --auto\033[0m"
  echo " Automatically find IP and mac address via command arp -e and shutdown client."
  echo " "
  exit
}

test=" "

if [ "$1" = "--help" ] ; then
  exitusage
fi
if [ -z "$1" ] ; then
  exitusage
fi

case $1 in
--mac)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a mac address.\033[0m"
    exitusage
  else
    macaddr=$2
    test=mac
  fi;;
--file)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a filename.\033[0m"
    exitusage
  else
    filename=$2
    test=file
    if [ ! -f ${filename} ] ; then
      echo -e "\033[0;31mFile not found.\033[0m"
      exit
    fi
  fi;;
--auto)
  test=auto;;
*)
  exitusage;;
esac

echo -n "Enter Windows username: "
read user

while true; do
  echo -n "Are you ready to continue shutdown PC? [y = continue, n = exit]"
  read -p " (y/n)" yn
  case $yn in
   [Yy]* ) CONFIRM_ANS="yes"; break;;
   [Nn]* ) CONFIRM_ANS="no"; break;;
       * ) echo "Please answer y or n.";;
  esac
done

if [ "${CONFIRM_ANS}" = "no" ]; then
  exit
else
  echo "Go.."
fi

case $test in
  mac)
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "No mac address $macaddr"
          echo "..."
        else
          echo "Shutdown client at $macaddr $ip"
          echo -n 3..
          sleep 1
          echo -n 2..
          sleep 1
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -s -f -t 0" > /dev/null 2>&1 &
          echo "1"
          echo "..."
        fi
        ;;
  file)
    for macaddr in $(cat ${filename}|grep -v '#'|grep -E -o '[[:xdigit:]]{2}(:[[:xdigit:]]{2}){5}')
    do
        ip=$(arp -e | grep $macaddr | awk '{print $1}')
        if [ -z "$ip" ]; then
          echo "Not seen mac address $macaddr"
          echo "..."
        else
          echo "Shutdown client at $macaddr $ip"
          echo -n 3..
          sleep 1
          echo -n 2..
          sleep 1
          ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -s -f -t 0" > /dev/null 2>&1 &
          echo 1
          echo "..."
        fi
    done
    ;;
  auto)
    gateway=$(ip route | grep default | awk '{print $3}')
    arp -e | grep -v 'gateway\|Address\|incomplete' | awk '{print $1 "-" $3}' > /tmp/arp.$$
    for line in $(cat /tmp/arp.$$)
    do
        ip=$(echo $line | awk -F- '{print $1}')
        macaddr=$(echo $line | awk -F- '{print $2}')
        if [ -z "$ip" ]; then
          echo "Not seen $macaddr"
          echo "..."
        else
          if [ ! "$ip" = "$gateway" ]; then
            echo "Shutdown client at $macaddr $ip"
            echo -n 3..
            sleep 1
            echo -n 2..
            sleep 1
            ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip "shutdown -s -f -t 0" > /dev/null 2>&1 &
            echo 1
            echo "..."
          fi
        fi
    done
    rm -f /tmp/arp.$$
    ;;
esac
# End.


wakeonlanclient.sh

#!/bin/bash

#Last updated: 3-09-2562 by Wiboon Warasittichai

#This script will boot pc via wake on lan 1 mac address or all mac addresses in a file.

exitusage()
{
  echo -e "\033[1;36mUsage.1:\033[0m \033[1;33mbash $0 --mac 08:00:27:11:22:33\033[0m"
  echo " Boot client only one mac address."
  echo " "
  echo -e "\033[1;36mUsage.2:\033[0m \033[1;33mbash $0 --file path/to/filename\033[0m"
  echo " path/to/filename is a file consists of one mac address per line i.e."
  echo " 08:00:27:11:22:33"
  echo " 08:00:27:44:55:66"
  echo " "
  exit
}

test=" "

if [ "$1" = "--help" ] ; then
  exitusage
fi
if [ -z "$1" ] ; then
  exitusage
fi

case $1 in
--mac)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a mac address.\033[0m"
    exitusage
  else
    macaddr=$2
    test=mac
  fi;;
--file)
  if [ -z "$2" ] ; then
    echo -e "\033[0;31mNeed a filename.\033[0m"
    exitusage
  else
    filename=$2
    test=file
    if [ ! -f ${filename} ] ; then
      echo -e "\033[0;31mFile not found.\033[0m"
      exit
    fi
  fi;;
*)
  exitusage;;
esac

whichwol="$(which wakeonlan|grep wakeonlan)"
if [ -z "${whichwol}" ]; then
  echo "Please install wakeonlan."
  echo "sudo apt-get -y install wakeonlan"
  echo " "
  exit
fi

while true; do
  echo -n "Are you ready to continue boot PC? [y = continue, n = exit]"
  read -p " (y/n)" yn
  case $yn in
   [Yy]* ) CONFIRM_ANS="yes"; break;;
   [Nn]* ) CONFIRM_ANS="no"; break;;
       * ) echo "Please answer y or n.";;
  esac
done

if [ "${CONFIRM_ANS}" = "no" ]; then
  exit
else
  echo "Go.."
fi

case $test in
  mac)
    echo "Boot client at $macaddr"
    wakeonlan $macaddr
    sleep 1
    echo "..."
    ;;
  file)
    for macaddr in $(cat ${filename}|grep -v '#'|grep -E -o '[[:xdigit:]]{2}(:[[:xdigit:]]{2}){5}')
    do
          echo "Boot client at $macaddr $ip"
          wakeonlan $macaddr
          sleep 1
          echo "..."
    done
    ;;
esac
# End.