#!/bin/bash

PINGTIME=3
PINGCOUNT=1
PINGCMD="/bin/ping"
ARPCMD="/usr/sbin/arp"

processByPing(){
  if [ $# -eq 3 ]; then
    $PINGCMD -c $PINGCOUNT -q -W $PINGTIME $1 > /dev/null 
    if [ $? = 0 ]; then
      MACA=`$ARPCMD -n $1 | grep $1 |awk '{print $3}'`
      if [ `echo $MACA | tr [:upper:] [:lower:]` = `echo $3 | tr [:upper:] [:lower:]` ]; then
        iptables -D OUTPUT -p tcp -d $1 --dport $2 -j REJECT > /dev/null
        echo "Printer on IP: $1 port: $2 is accessible"
      fi
    else
      iptables -D OUTPUT -p tcp -d $1 --dport $2 -j REJECT > /dev/null
      iptables -I OUTPUT -p tcp -d $1 --dport $2 -j REJECT > /dev/null
      echo "Printer on IP: $1 port: $2 is NOT accessible"
    fi
  else
    echo "Bad parameters"
  fi
}

echo "Detect remote printer"
#every remote printer server or printers
#1st IP addres
#2nd port
#3rd MAC address of server or printer

processByPing 192.168.0.16 631 00:03:47:C4:0B:56

echo "DONE"
exit 0

