#!/bin/sh 
# 
# HangUp pppd 
# 
# usage: 
#      pppd_kill IP_address 
# 
# Add next string to /etc/sudoers: 
# 
# apache   ALL = NOPASSWD: /usr/local/abills/misc/pppd_kill 
# 
# For remote hangup 
# /etc/inetd.conf
# hangup    stream  tcp     nowait  root    /usr/local/abills/misc/ppp_kill ppp_kill server 
# /etc/services
# hangup	30000/tcp

#Debug mode
VERSION=0.2
DEBUG=0;
LOG_FILE=/var/log/pppd_kill.log
ALLOW_IP=""

if [ w$1 = wserver ] ; then
  read IP_ADDR
else if [ w$1 = w ] ; then
  echo "Linux pppd kill script"
  echo "pppd_kill [IP_ADDRESS]";
  exit;
else
  IP_ADDR=$1 
fi
fi

IP_STRIP=`echo "$IP_ADDR" | /usr/bin/tr -d '\r'` 

# only one from following two strings must be uncommented 
INTERFACE=`/sbin/ifconfig | /usr/bin/awk -v RS='\n\n'  "/$IP_STRIP / {print \\$1}"` 

# INTERFACE=`/sbin/ip a | /bin/grep $IP_STRIP/32 | /usr/bin/tr -s ' ' | /bin/cut -f 8 -d ' '` 

if [ -f /var/run/$INTERFACE.pid ]; then 
  PPP_PID=`cat /var/run/$INTERFACE.pid` 
  kill -1 $PPP_PID 
else
  echo "Can't find PID file '/var/run/$INTERFACE.pid'"
fi;

echo "killed PID: $PPP_PID INTERFACE: $INTERFACE  IP: $IP_STRIP" 

#if debug mode write output to file
if [ w${DEBUG} = w1 ]; then
   DATE=`date "+%Y-%m-%d %H-%M-%S"`;
   echo "${DATE} HOST: ${REMOTE_HOST} killed PID: $PPP_PID INTERFACE: $INTERFACE  IP: $IP_STRIP"  >> ${LOG_FILE}
fi;

