PDA

View Full Version : Ping an entire Lan and collect machine Mac Address



ErosOlmi
03-05-2007, 17:41
Today I needed to know which IP on our lan had what Mac address.
How to do? You can use Arp or GetMac or other tools around but you can make the job also using scripts, in this case a thinBasic script.

Here the code:

'---------
' Modules
'---
uses "inet"
uses "console"

'---------
' Constants
'---
%MaxIp = 255

%Pos_IP = 1
%Pos_Results = 2
%Pos_Mac = 3

'---------
' Variables
'---
dim Counter as long '---Used to count IPs
dim sIP as string '---Used to form full IP
dim sSubNet as string '---Store initial subnet

dim sResults(3, %MaxIp) as string '---Store results

'---------
' Script
'---

'---Indicate here your lan
sSubNet = "10.10.130."
'---

for Counter = 1 to %MaxIp
sIP = sSubNet & Counter

sResults(%Pos_IP , Counter) = sIP
sResults(%Pos_Results , Counter) = INET_Ping(sIP, 100, "abcdefghijklmnopqrstu")
sResults(%Pos_Mac , Counter) = INET_GetRemoteMACAddress(sIP)

Console_writeline _
format$(Counter, "000") & " " & _
using$("\ \", sResults(%Pos_IP, Counter)) & _
using$("\ \", parse$(sResults(%Pos_Results, Counter), $tab, 3)) & _
sResults(%Pos_Mac, Counter) & _
""

next

'---Here you can do what you need with IP and mac address pair.

console_waitkey

Petr Schreiber
03-05-2007, 18:19
This is nice script,

just few lines of code and so much information it can give.
Nice !


Petr

kryton9
04-05-2007, 04:43
That is very nice Eros. It showed me something I hadn't thought about. On my notebook, I have dual boot, windowsXP and Ubuntu, I have fixed IP's assigned to all my computers, but I did that in Windows. When I ran your cool tool, I see with Ubuntu running, it is getting the ip from the router via DHCP and not using the fixed ip.

Is there a way to get the WAN address coming into my router, currently the only way I can retrieve it is by going into the LAN configuration panels and looking at the status settings or running a program from a browser from a site outside that can tell me my WAN IP. It would be neat if a console app could tell you that info, all I could do was get the ip's on this side of the router/firewall.

ErosOlmi
04-05-2007, 07:27
Ken,

what about using an exteral service from the web and scan returned text file?
See example and tell me if it works. If ok I will add sample into thinBasic script arsenal :)

Ciao
Eros


uses "INet"
uses "File"

dim ret as number
dim OutFileName as string = APP_SOURCEPATH + "INet_GetWanIP.html"

ret = INET_URLDownLoad("http://checkip.dyndns.org/", OutFileName)

IF ret <> %TRUE THEN
msgbox 0, "An error occurred. Error code: " + ret)
stop
END IF

dim sBuffer as string = file_load(OutFileName)
dim sIP as string
dim lPosStart as long
dim lPosEnd as long

'---It seems we were able to get the html page. Now we need to parse it.
'---IP should be after : char and before </body> tag inside buffer
'---Buffer should be something like:
'---<html><head><title>Current IP Check</title></head><body>Current IP Address: a.b.c.d</body></html>
'---ATTENTION: returned buffer can change

'---Find position of :
lPosStart = instr(sBuffer, ":")

'---If found
if lPosStart <> 0 then
lPosStart += 1 '---Move position 1 char after

'---Find position of < after IP
lPosEnd = instr(lPosStart, sBuffer, "<")

'---Get the IP
sIP = trim$(mid$(sBuffer, lPosStart, lPosEnd - lPosStart ))

'---Show it
msgbox 0, "Your WAN ip should be: [" & sIP & "]"

else
msgbox 0, "Sorry, it was not possible to retrieve Wan IP."
end if

kryton9
04-05-2007, 08:24
Eros you are great. That works perfectly, very fast, thanks!!!