jablonka.czprosek.czf

wifimon

Subversion Repositories:
[/] [trunk/] [wifimon] - Rev 25 Go to most recent revision

Compare with Previous - Blame - Download


#!/bin/bash
# authors      : Petr Simandl www.simandl.cz
#              : Fyzik, Covex, Tydyt, Thunder.m
# release date : 17/09/2005 
# name         : wifimon
# description  : hostap and madwifi powered card monitoring
# license      : GPL
######################################################################

wl_version="0.5.6pre2"

######################################################################
#custom settings

PATH=$PATH:$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin

wl_iwccmd=`which iwconfig`
wl_iwlcmd=`which iwlist`
wl_ifccmd=`which ifconfig`
wl_hnmcmd=`which hostname`
wl_awkcmd=`which awk`

wl_tptcmde=""
wl_clrcmde=""

wl_sysdev="/proc/sys/dev/"
wl_hostap="/proc/net/hostap/"
wl_wireless="/proc/net/wireless"
wl_arp="/proc/net/arp"
wl_dhcpcnf="/etc/dhcpd.conf"
wl_dhcplss="/var/lib/dhcp/dhcpd.leases"
# wl_dhcpcnf="/etc/dhcp3/dhcpd.conf"
# wl_dhcplss="/var/lib/dhcp3/dhcpd.leases"

wl_header_refresh="<html><head><meta HTTP-EQUIV=\"Refresh\" CONTENT=\"15\"><title>Wifimon @ `$wl_hnmcmd`</title></head><body BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"><pre>"
wl_header="<html><head><title>Wifimon @ `$wl_hnmcmd`</title></head><body BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"><pre>"
wl_footer="</pre></body></html>"

wlm_no_managed_or_master=" neni ani Master ani Managed..."
wlm_number_of_clients="Pocet asociovanych klientu : " 
wlm_unknown="Nezname parametry nebo neplatne jmeno rozhrani : "
#wlm_no_managed_or_master=" is not Master or Managed..."
#wlm_number_of_clients="Number of associated clients : " 

wlch_signal="="
wlch_noise="*"

######################################################################
#testing basic setings

if [ "$wl_iwccmd x" = " x" ]
    then
        echo "iwconfig not found"
        exit 1
fi

if [ "$wl_iwlcmd x" = " x" ]
    then
        echo "iwlist not found"
        exit 1
fi

if [ "$wl_ifccmd x" = " x" ]
    then
        echo "ifconfig not found"
        exit 1
fi

if [ "$wl_hnmcmd x" = " x" ]
  then
    echo "hostname not found"
    exit 1
fi

if [ "$wl_awkcmd x" = " x" ]
  then
  echo "awk not found"
  exit 1
fi

if [ -e $wl_wireless ]
  then 
    wl_nop=1  
  else 
  echo "$wl_wireless not found"
  exit 1
fi

if [ -e $wl_arp ]
  then 
    wl_nop=1  
  else 
  echo "$wl_arp not found"
  exit 1
fi

#dhcp support
#if not found trying to set alternative location
if [ ! -f $wl_dhcpcnf ]; then
      wl_dhcpcnf="/etc/dhcp3/dhcpd.conf"
fi

if [ ! -f $wl_dhcplss ]; then
  wl_dhcplss="/var/lib/dhcp3/dhcpd.leases"
fi

#if not found a message is printed and /dev/null is used instead
if [ -e $wl_dhcpcnf ]
  then 
    wl_nop=1  
  else 
  echo "$wl_dhcpcnf  not found"
  wl_dhcpcnf="/dev/null"
fi

if [ -e $wl_dhcplss ]
  then 
    wl_nop=1  
  else 
  echo "$wl_dhcplss not found"
  wl_dhcplss="/dev/null"
fi
######################################################################

if [ -d ${wl_hostap}wlan0 ]
  then
    wlf_hostap=1
  else 
#  echo "${wl_hostap}wlan0 not found"
    wlf_hostap=0
fi

if [ -d ${wl_sysdev}ath0 ]
  then 
    wlf_madwifi=1
  else 
#  echo "${wl_sysdev}ath0 not found"
    wlf_madwifi=0
fi

if [ "$wlf_hostap" -eq 0 ] && [ "$wlf_madwifi" -eq 0 ]
  then
    echo "${wl_hostap}wlan0 not found"
    echo "${wl_sysdev}ath0 not found"
    exit 1
fi

######################################################################
# procedures
######################################################################
w_col()
{
wlc_red=`tput setaf 1`          # ruda         (Red)
wlc_green=`tput setaf 2`        # zelena       (Lime)
wlc_brown=`tput setaf 3`        # zluta        (Yellow)
wlc_blue=`tput setaf 4`         # modra        (Blue)
wlc_magenta=`tput setaf 5`      # fialova      (Fuchsia)
wlc_lblue=`tput setaf 6`        # svetle modra (Aqua)
wlc_grey=`tput setaf 7`         # bila         (White)
wlc_std=`tput sgr0`

wlc_pkts=$wlc_red       # Rx,Tx info color
wlc_ip=$wlc_lblue       # ip address color
wlc_mac=$wlc_magenta    # mac address color
wlc_name=$wlc_magenta   # host name color
wlc_sig=$wlc_red        # signal number color
wlc_bar=$wlc_green      # signal bar color
wlc_noise=$wlc_red      # Signal noise color
}
######################################################################
w_colh()
{
  wlh_pkts="Olive"      # signal number color html
  wlh_ip="Blue"         # ip address color html
  wlh_mac="Fuchsia"     # mac address color html
  wlh_name="Fuchsia"    # Host name color html
  wlh_sig="Red"         # signal number color html
  wlh_bar="Lime"        # signal bar color html
  wlh_noise="Red"       # Signal noise color html

  wlc_pkts="<font color=\"$wlh_pkts\">"
  wlc_ip="<font color=\"$wlh_ip\">"
  wlc_mac="<font color=\"$wlh_mac\">"
  wlc_name="<font color=\"$wlh_name\">"
  wlc_sig="<font color=\"$wlh_sig\">"
  wlc_bar="<font color=\"$wlh_bar\">"
  wlc_noise="<font color=\"$wlh_noise\">"
  wlc_std="</font>"
}
######################################################################
w_bw()
{
  wlc_pkts=""
  wlc_ip=""
  wlc_mac=""
  wlc_sig=""
  wlc_bar=""
  wlc_noise=""
  wlc_std=""
}
######################################################################
w_bar()
{
  wl_neg=`expr $wl_cnt \< 0 `
  if [ "$wl_neg" -eq 1 ]
  then
    echo -n "${wlc_noise}"
    wl_cnt=$(($wl_cnt * -1 ))
    wl_bar=$wlch_noise
  else
    echo -n "${wlc_bar}"
    wl_bar=$wlch_signal
  fi
  until [ "$wl_cnt" -eq -1 ]
  do
    echo -n "$wl_bar"
    wl_cnt=$(($wl_cnt - 1 ))
  done  
  printf "        \n"
  echo -n "${wlc_std}"
#  tput sgr0
} # w_bar

######################################################################
w_pkts()
{
  wl_macpkts=`cat $wl_hostap$wl_iface/$wl_mac | grep -E 'rx\[|tx\[' | sed 's/=/ /g'`
  wl_nt1=`echo $wl_macpkts | $wl_awkcmd '{print $2}'`
  wl_nt2=`echo $wl_macpkts | $wl_awkcmd '{print $4}'`
  wl_nt5=`echo $wl_macpkts | $wl_awkcmd '{print $6}'`
  wl_nt11=`echo $wl_macpkts | $wl_awkcmd '{print $8}'`
  wl_nr1=`echo $wl_macpkts | $wl_awkcmd '{print $10}'`
  wl_nr2=`echo $wl_macpkts | $wl_awkcmd '{print $12}'`
  wl_nr5=`echo $wl_macpkts | $wl_awkcmd '{print $14}'`
  wl_nr11=`echo $wl_macpkts | $wl_awkcmd '{print $16}'`

  if [ $wcm_ppkts -eq 1 ]
  then
  wl_pksize=$((98))
  wl_nt=$(($wl_nt1 + $wl_nt2 + wl_nt5 + wl_nt11))
  if [ "$wl_nt1" -eq 0 ]
  then
    wl_nt1n="00"
  else
    wl_nt1n=$(($wl_pksize * $wl_nt1 / $wl_nt + 1))
    [ "$wl_nt1n" -lt 10 ] && wl_nt1n="0"$wl_nt1n
  fi
  if [ "$wl_nt2" -eq 0 ]
  then
    wl_nt2n="00"
  else
    wl_nt2n=$(($wl_pksize * $wl_nt2 / $wl_nt + 1))
    [ "$wl_nt2n" -lt 10 ] && wl_nt2n="0"$wl_nt2n
  fi
  if [ "$wl_nt5" -eq 0 ]
  then
    wl_nt5n="00"
  else
    wl_nt5n=$(($wl_pksize * $wl_nt5 / $wl_nt + 1))
    [ "$wl_nt5n" -lt 10 ] && wl_nt5n="0"$wl_nt5n
  fi
  if [ "$wl_nt11" -eq 0 ]
  then
    wl_nt11n="00"
  else
    wl_nt11n=$(($wl_pksize * $wl_nt11 / $wl_nt + 1))
    [ "$wl_nt11n" -lt 10 ] && wl_nt11n="0"$wl_nt11n
  fi
  wl_nr=$((1 + $wl_nr1 + $wl_nr2 + wl_nr5 + wl_nr11))
  if [ "$wl_nr1" -eq 0 ]
  then
    wl_nr1n="00"
  else
    wl_nr1n=$(($wl_pksize * $wl_nr1 / $wl_nr + 1))
    [ "$wl_nr1n" -lt 10 ] && wl_nr1n="0"$wl_nr1n
  fi
  if [ "$wl_nr2" -eq 0 ]
  then
    wl_nr2n="00"
  else
    wl_nr2n=$(($wl_pksize * $wl_nr2 / $wl_nr + 1))
    [ "$wl_nr2n" -lt 10 ] && wl_nr2n="0"$wl_nr2n
  fi
  if [ "$wl_nr5" -eq 0 ]
  then
    wl_nr5n="00"
  else
    wl_nr5n=$(($wl_pksize * $wl_nr5 / $wl_nr + 1))
    [ "$wl_nr5n" -lt 10 ] && wl_nr5n="0"$wl_nr5n
  fi
  if [ "$wl_nr11" -eq 0 ]
  then
    wl_nr11n="00"
  else
    wl_nr11n=$(($wl_pksize * $wl_nr11 / $wl_nr + 1))
    [ "$wl_nr11n" -lt 10 ] && wl_nr11n="0"$wl_nr11n
  fi
  echo -n "Tx"${wlc_pkts} $wl_nt1n $wl_nt2n $wl_nt5n $wl_nt11n ${wlc_std}""
  echo -n "Rx"${wlc_pkts} $wl_nr1n $wl_nr2n $wl_nr5n $wl_nr11n ${wlc_std}""
  fi 

  [ $wcm_pkts -eq 1 ] && echo -n "Tx" $wl_nt1 $wl_nt2 $wl_nt5 $wl_nt11 "Rx" $wl_nr1 $wl_nr2 $wl_nr5 $wl_nr11 ""

} # w_pkts
######################################################################
w_managed()
{ echo $wl_iwctxt | $wl_awkcmd '{print $1,$3,$4,$5"  "}' 
  echo -n `$wl_ifccmd $wl_iface | grep -E "inet addr|inet adr" | $wl_awkcmd '{print $2}' | sed s/ad*r://g`" " # IP adresa mastera z ifconfig 

  if [ $wcm_macoff -eq 0 ]
  then
    wl_mac=`echo -n $wl_iwctxt | $wl_awkcmd '{print $8}'` 
    echo -n "${wlc_mac}$wl_mac${wlc_std} "
  fi
  echo $wl_iwctxt | $wl_awkcmd '{print $6,$10"  "}' 

  wl_wtxt=`cat $wl_wireless | grep $wl_iface | $wl_awkcmd '{print $3,$4,$5}' | sed 's/\.//g'`
  wl_quality=`echo $wl_wtxt | $wl_awkcmd '{print $1}'`
  wl_signal=`echo $wl_wtxt | $wl_awkcmd '{print $2}'`
  wl_signal=$(($wl_signal - 256))
  wl_noise=`echo $wl_wtxt | $wl_awkcmd '{print $3}'`
  wl_noise=$(($wl_noise - 256))
  echo "Quality:${wlc_sig}$wl_quality${wlc_std}/92 Signal level:$wl_signal Noise level:$wl_noise "
  wl_cnt=$wl_quality
  w_bar
  echo
} # w_managed

######################################################################
w_master()
{ if [ $wcm_oneline -eq 0 ]
    then 
      echo $wl_iwctxt | $wl_awkcmd '{print $1,$3,$4,$5"  "}' 
      if [ $wcm_macoff -eq 1 ]
        then
          echo $wl_iwctxt | $wl_awkcmd '{print $6,$11"  "}' 
        else
          echo $wl_iwctxt | $wl_awkcmd '{print $6,$8,$11"  "}' 
      fi
    else 
      echo $wl_iwctxt | $wl_awkcmd '{print $1,$4,$5,$6,$11"  "}' | sed 's/ [^": ]*:/ /g'
  fi

  if [ -d $wl_hostap$wl_iface ]
    then
      wl_numcl=`ls $wl_hostap$wl_iface | grep -c 00`
      wl_macs=`ls $wl_hostap$wl_iface | grep 00`
  fi

  if [ -d $wl_sysdev$wl_iface ]
    then
      wl_numcl=`$wl_iwlcmd $wl_iface peers | grep -c 00`
      wl_macs=`iwlist $wl_iface peers | grep 00 | awk '{print $1}'`
  fi

  echo $wlm_number_of_clients $wl_numcl

  wl_numcllast=$(($wl_numcllast + $wl_numcl))

  for wl_mac in $wl_macs
  do
    if [ -d $wl_hostap$wl_iface ]
      then
        wl_macparam=`cat $wl_hostap$wl_iface/$wl_mac | grep -E 'last_rx|last_tx' | sed 's/last_//g'`
        wl_signal=`echo $wl_macparam | $wl_awkcmd '{print $6}' | sed 's/signal[=|:]//g'`
        wl_silence=`echo $wl_macparam | $wl_awkcmd '{print $4}' | sed 's/silence[=|:]//g'`
    fi

    if [ -d $wl_sysdev$wl_iface ]
      then
        wl_macparam=`$wl_iwlcmd $wl_iface peers | grep $wl_mac`
        wl_signal=`echo $wl_macparam | $wl_awkcmd '{print $5}' | sed 's/level[=|:]//g'`
        wl_silence=`echo $wl_macparam | $wl_awkcmd '{print $8}' | sed 's/level[=|:]//g'`
    fi

    wl_cnt=$(($wl_signal - $wl_silence))
    wl_ipc=`cat $wl_arp | grep -i $wl_mac | $wl_awkcmd '{print $1}'`   # IP adresa peeru z arp tabulky

    wl_name=`cat $wl_dhcpcnf | grep -i $wl_mac | $wl_awkcmd '{print $2}'`
    if [ "$wl_name" = "ethernet" ] || [ "$wl_name" = "" ]
    then
      wl_name=`cat $wl_dhcpcnf | grep -i $wl_mac -B 1 | $wl_awkcmd '{print$2,$3}' | grep -i $wl_mac -v | $wl_awkcmd '{print$1}'`
      if [ "$wl_name" = "" ]
      then
        wl_name=`cat $wl_dhcplss | grep -i $wl_mac -A 2 | grep -i client-hostname | $wl_awkcmd '{print$2}'`
        wl_name=`echo $wl_name | $wl_awkcmd '{print$1}' `
      fi
    fi
    if [ $wcm_ppkts -eq 1 ] || [ $wcm_pkts -eq 1 ] && [ -d $wl_hostap$wl_iface ]
    then
      w_pkts
    fi

    if [ $wcm_oneline -eq 0 ]
    then
      echo -n "${wlc_name}$wl_name${wlc_std} "
      echo -n "${wlc_ip}$wl_ipc${wlc_std} "
      if [ $wcm_macoff -eq 0 ]
      then
        echo -n "${wlc_mac}$wl_mac${wlc_std} "
      fi
      if [ $wcm_threeline -eq 1 ]
      then
        echo ""
        echo -n "                "
      fi 
      echo -n "Signal ${wlc_sig}$wl_cnt${wlc_std} "
      if [ $wcm_rxtx -eq 0 ]
      then
        echo ""
      else
        if [ -d $wl_hostap$wl_iface ]
         then
           echo $wl_macparam | $wl_awkcmd '{print $1,$2"  "}' 
         else
           echo ""
        fi
      fi
      w_bar
    else
      if [ $wcm_macoff -eq 0 ]
      then
        echo -n "${wlc_mac}$wl_mac${wlc_std} "
      fi
      echo -n "${wlc_sig}$wl_cnt${wlc_std} "
      echo -n "$wl_name "
      w_bar
    fi
  done

  echo
} # w_master

######################################################################
w_iface()
{ wl_iwctxt=`$wl_iwccmd $wl_iface 2>/dev/null | grep -v 'Device|Some|Extension' | sed 's/ GHz/GHz/' | sed 's/ Mb/Mb/' | sed 's/  Nick/_Nick/' | sed 's/Access Point/Access_Point/'` 
  wl_mode=`echo $wl_iwctxt | $wl_awkcmd '{print $5}' | sed 's/Mode://g'`
  if [ "a$wl_mode" = "aManaged" ]
  then
    w_managed
  elif [ "a$wl_mode" = "aAd-Hoc" ]
  then
    w_managed
  elif [ "a$wl_mode" = "aMaster" ]
  then
    w_master
  else
    echo $wl_iface $wlm_no_managed_or_master
    echo
  fi  
}

######################################################################
w_version()
{
  echo wifimon $wl_version
  exit 0
}
######################################################################
w_help()
{
  echo Monitorovani wifi karet pouzivajicich hostap nebo madwifi ovladace
  echo Pouziti: wifimon [param]
  echo param:
  echo -v vypise verzi
  echo -html generovani html stranky
  echo -col barevny vypis na obrazovku
  echo -once vypsani jen jednoho vypisu a pak program skonci
  echo -oneline vypsani klientu na jednu linku
  echo -threeline vypsani klienta na trech radcich
  echo -macoff vypnuti zobrazeni MAC adres
  echo -wlan0 -ath0 -wlan2 vypise jen uvedene karty a to v danem poctu a poradi 
  echo nasledujici parametry ovlivnuji jen karty s hostap: 
  echo -rxtxoff vypnuti zobrazeni objemu prenesenych dat klienta
  echo -ppkts vypsani procentniho pomeru 1M, 2M, 5.5M a 11M paketu pro jednotlive klienty
  echo -pkts vypsani  poctu 1M, 2M, 5.5M a 11M paketu pro jednotlive klienty
  echo bez parametru cyklicky vypis na obrazovku
  exit 0
}
######################################################################
######################################################################

wl_unknown=""
wl_cnt=1
wcm_clear=1
wcm_once=0
wcm_col=0
wcm_oneline=0
wcm_threeline=0
wcm_html=0
wcm_rxtx=1
wcm_ppkts=0
wcm_pkts=0
wcm_ifc=""
wcm_macoff=0

# parsing input parameters
while [ "a$1" != "a" ]
do
  case $1 in
  -v)
     w_version
  ;;
  -help)
     w_help
  ;;
  -once)
    wcm_once=1
    shift
  ;;
  -oneline)
    wcm_oneline=1
    shift
  ;;
  -threeline)
    wcm_threeline=1
    shift
  ;;
  -col)
    wcm_col=1
    shift
  ;;
  -html)
    wcm_html=1
    shift
  ;;
  -rxtxoff)
    wcm_rxtx=0
    shift
  ;;
  -ppkts)
    wcm_ppkts=1
    shift
  ;;
  -pkts)
    wcm_pkts=1
    shift
  ;;
  -macoff)
    wcm_macoff=1
    shift
  ;;
  *)
    wl_flg=0
#wlan check
    if [ $wlf_hostap -eq 1 ]
      then
        for wl_iface in `ls $wl_hostap`
          do 
            if [ "a-$wl_iface" = "a$1" ]
            then
              wcm_ifc="$wcm_ifc $wl_iface"
              wl_flg=1
            fi
          done
    fi
# ath check
    if [ $wlf_madwifi -eq 1 ]
      then
        for wl_iface in `ls $wl_sysdev | grep "ath[0-9]"`
          do
            if [ "a-$wl_iface" = "a$1" ]
              then
                wcm_ifc="$wcm_ifc $wl_iface"
                wl_flg=1
            fi
          done
    fi
    if [ $wl_flg -eq 0 ]
      then
        wl_unknown="$wl_unknown$1 "
    fi
    shift
  esac
done

# printing the list of bad parameters (if there are some)
if [ "a$wl_unknown" != "a" ]
then
  echo "$wlm_unknown $wl_unknown"
  w_help
fi
# if no interface is selected then all available are choosen
if [ "a$wcm_ifc" = "a" ]
then
  if [ $wlf_hostap -eq 1 ]
    then
      wcm_ifc=`ls $wl_hostap `
  fi
  if [ $wlf_madwifi -eq 1 ]
    then
      wcm_ifc=`echo $wcm_ifc ; ls $wl_sysdev | grep "ath[0-9]"`
  fi
fi

# setting for old and last same value to avoid double clear after start
wl_numcllast=0
wl_numclold=0
for wl_iface in $wcm_ifc
do 
  if [ -d $wl_hostap$wl_iface ]
    then
      wl_numcl=`ls $wl_hostap$wl_iface | grep -c 00`
      wl_numcllast=$(($wl_numcllast + $wl_numcl))
  fi
  if [ -d $wl_sysdev$wl_iface ]
    then
      wl_mode=`$wl_iwccmd $wl_iface | grep Mode | $wl_awkcmd '{print $1}' | sed 's/Mode://g'`
      if [ "a$wl_mode" = "aMaster" ]
        then
          wl_numcl=`$wl_iwlcmd $wl_iface peers | grep -c 00`
          wl_numcllast=$(($wl_numcllast + $wl_numcl))
      fi
  fi
done
wl_numclold=$wl_numcllast

if [ $wcm_col -eq 0 ]
then
  w_bw
else
  if [ $wcm_html -eq 1 ]
  then
    w_colh
  else
    w_col
  fi
fi

if [ $wcm_html -eq 1 ]
then
  [ $wcm_once -eq 1 ] && echo $wl_header || echo $wl_header_refresh
  for wl_iface in $wcm_ifc
  do 
    w_iface
  done
  echo $wl_footer
  exit 0
fi

#this is main loop and it will be executed forever
while [ 1 ]
  do

    # when the number of associated klients has changed we will set for clear screen
    if [ $wl_numcllast -ne $wl_numclold ]
    then
      wl_numclold=$wl_numcllast
      wcm_clear=1
    fi

    #if it is set for clear screen we will do it except we produce single report
    if [ $wcm_clear -eq 1 -a $wcm_once -eq 0 ]
    then
      echo -n $wl_clrcmde
      wcm_clear=0
    fi
    wl_numcllast=0

    #for cyclic report we are trying to set cursor to the top left screen corner
    if [ $wcm_once -eq 0 ]
    then
      echo -n $wl_tptcmde
    fi

    #make report for all desired interfaces
    for wl_iface in $wcm_ifc
    do 
      w_iface
    done

    #when a single report is needed than we are exiting
    if [ $wcm_once -eq 1 ]
    then
      exit 0
    fi 
    sleep 1
done

exit 0


Powered by WebSVN 2.2.1