Hi all,
This is my first script, I've ever made.
It uses nmap to get all MAC Addresses and all IPs in the LAN. The informations are storen in /tmp/ip.ls and /tmp/mac.ls. Then the script uses 'paste' to paste the matching IP to the matching MAC.
I have a few issues with it, where you may help:
- I don't get the Hostnames, it tells only the vendor of the interface (/tmp/host.ls).
- Sometimes, the formating isn't properly. Or we have more IPs than MACs, or more MACs than IPs etc.
- It don't show all Members, there are a few times IPs, that are in the LAN, but not listet. I may use another tool than nmap, but which one? Any ideas?
Here the Script:
Code:
#!/bin/bash
# Copyright by Joel Bodenmann <jeybee66@gmail.com>. Licensed under the terms of the GNU GPLv3.
HOST_FILE="/tmp/host.ls"
IP_FILE="/tmp/ip.ls"
MAC_FILE="/tmp/mac.ls"
rm $HOST_FILE $IP_FILE $MAC_FILE > /dev/null
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ $# -eq 1 ]; then
IP_RANGE=$1
else
OS=`uname`
case $OS in
Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
FreeBSD|OpenBSD) IP=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
*) IP="Unknown";;
esac
IP_RANGE=$(echo $IP | sed 's/\.[^\.]*$/\.\*/')
fi
clear
echo
echo "-----------------------"
echo start scanning now:
echo
nmap -sP $IP_RANGE | grep "MAC" | awk -F " " '{printf("%s ",$1);for(i=1;i<=NF;i++) printf("%s ",$i);printf("\n")}' | awk '{for(i=5; i<=NF; i++) printf $i" ";printf("\n")}' | sed s/\(/""/g | sed s/\)/""/g > $HOST_FILE
nmap -sP $IP_RANGE | grep $IP_RANGE | grep -v $IP | awk -F " " '{print $NF}' | sed s/\(/""/g | sed s/\)/""/g > $IP_FILE
nmap -sP $IP_RANGE | grep "MAC" | awk -F " " '{print $3}' > $MAC_FILE
#awk '{ print length($0)-1 }' /tmp/host.ls
LINES="$(wc -l $IP_FILE|awk '{print $1}')"
echo "We got $LINES other hosts in the LAN!";echo;echo
echo -e "<IP>\t\t<MAC>\t\t\t<Host>";echo
paste $IP_FILE $MAC_FILE $HOST_FILE
echo
echo "-----------------------"
echo
And of course an example output:
Code:
-----------------------
start scanning now:
We got 26 other hosts in the LAN!
<IP> <MAC> <Host>
172.17.0.1 40:4A:03:6D:D4:51 ZyXEL Communications
172.17.0.11 00:30:05:30:DB:F1 Fujitsu Siemens Computers
172.17.0.12 E0:CB:4E:DA:3E:D1 Unknown
172.17.0.14 00:0E:A6:3F:71:B1 Asustek Computer
172.17.0.15 00:1D:7D:79:B2:B1 Giga-byte Technology Co.
172.17.0.20 00:00:74:C1:98:A3 Ricoh Company
172.17.0.25 00:02:CF:B5:A6:08 ZyGate Communications
172.17.0.50 00:22:41:31:15:BB Apple
172.17.0.51 00:22:6B:1A:77:48 Cisco-Linksys
172.17.0.65 00:1D:60:90:4B:C1 Asustek Computer
172.17.0.66 00:1D:60:90:52:F4 Asustek Computer
172.17.0.67 00:1D:60:90:53:44 Asustek Computer
172.17.0.68 00:1C:42:9C:49:BC Parallels
172.17.0.69 00:1D:60:90:53:34 Asustek Computer
172.17.0.70 00:1D:60:8F:57:99 Asustek Computer
172.17.0.71 00:1D:60:DB:07:22 Asustek Computer
172.17.0.74 00:1D:60:90:53:3B Asustek Computer
172.17.0.75 00:1D:60:90:53:38 Asustek Computer
172.17.0.76 00:1D:60:DB:1B:DA Asustek Computer
172.17.0.77 00:1D:60:20:F5:22 Asustek Computer
172.17.0.81 D8:75:33:57:08:31 Unknown
172.17.0.85 00:1D:60:DA:D9:86 Asustek Computer
172.17.0.101 00:26:4A:09:5A:F4 Apple
172.17.0.135 00:22:6B:1A:4F:3C Cisco-Linksys
172.17.0.200 00:02:44:52:27:41 Surecom Technology Co.
172.17.0.211 00:50:C2:37:2C:72 Ieee Registration Authority
-----------------------
I hope you enjoy it and please feel free to help and contribute

Greez Tectu ~