Register
It is currently Mon Sep 22, 2014 10:14 am

Show LAN Members IP and MAC


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon May 23, 2011 1:53 am   

Joined: Wed May 18, 2011 6:59 am
Posts: 2
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 ~


Top
 Profile  
 PostPosted: Mon May 23, 2011 12:27 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Some questions and tips:
The nmap that I have an 'nmap -sP' does nothing more than 'ping' a host (or range of hosts), there is no MAC address or NIC information available (nmap 5.00).
Your issue with having a different number of lines in your 3 output files, could be solved by running only 1 'nmap' command and saving the output thereof in a file. (as far as I can see you run 3 nmap's with the same parameters)
Finally I consider it quite ironic that you run 3 technically the same commands, separate the output to 3 different files and finally concatenate them back together again. I would try to look into parsing the output of the 1 nmap command line by line into the output you want to achieve.


Top
 Profile  
 PostPosted: Wed May 25, 2011 4:52 am   

Joined: Wed May 18, 2011 6:59 am
Posts: 2
Hello Patsie

Thanks for your intressts and tips.
I don't know where your problem is. I am using Nmap 5.21. You may upgrade it and test again?

I execute the same command three times because I don't know how I can use the pipe to make three files by three different greps. My idea is just to save the whole output into one file and parse it out from there. How can I do that straight in the pipe?


Greez Tectu


Top
 Profile  
 PostPosted: Wed May 25, 2011 12:03 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Code:
command > "$tempfile"
grep "something" "$tempfile"
grep "some other thing" "$tempfile"
grep "the last thing" "$tempfile"
rm "$tempfile"

:)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP