whoisconnected

#!/bin/bash

#Affiche les connexions actives.
#Plus pour montrer l'utilisation de /proc/net/tcp
#étant donné que netstat permet facilement d'obtenir
#ces informations.

convertIP()
{
    IP=""
    for i in 1 2 3 4
    do
        car1=`expr $i "*" 2 - 1`
        car2=`expr $i "*" 2`
        number=`echo $1 | cut -c $car1-$car2 | hex2 d`
        if [ -n "$IP" ]
        then
          IP=$number.${IP}
        else
          IP=$number
        fi
    done
    echo $IP
}

echo -e "\tSource\t\t\tDestination"
echo

for connexion in `cat /proc/net/tcp | grep -v local | sed 's/ /_/g'`
do
    connexion=`echo $connexion | sed 's/_/ /g'`
    source=`echo $connexion | gawk '{print $2}'`
    dest=`echo $connexion | gawk '{print $3}'`

    source_addr=`echo $source | gawk -F ":" '{print $1}'`
    source_port=`echo $source | gawk -F ":" '{print $2}'`

    dest_addr=`echo $dest | gawk -F ":" '{print $1}'`
    dest_port=`echo $dest | gawk -F ":" '{print $2}'`

    if [ "$source_addr" != "00000000" -a "$dest_addr" != "00000000" ]
    then
      source_addr=`convertIP $source_addr`
      dest_addr=`convertIP $dest_addr`
      source_port=`echo $source_port | hex2 d`
      dest_port=`echo $dest_port | hex2 d`

      echo -e "${source_addr}:${source_port}\t\t${dest_addr}:${dest_port}"
      echo "`host $source_addr | gawk '{print $5}'` - `host $dest_addr | gawk '{print $5}'`"
      echo
    fi
done