Sign in to follow this  
Followers 0
cowsmanaut

collect Ip and Mac addresses

12 posts in this topic

dunno if this is usefull to anyone but I made this thing after plenty of help from others and the wonderfull help file :D

I basically takes a list of local computers and sends a request for their IP and MAC address and then pipes all that information from them to a Text file.

I tried to fully comment everything so it should be easy to read. You need psexec.exe to get it to work. This is part of the free pstools suite. http://www.sysinternals.com/Utilities/PsTools.html

enjoy

#include <array.au3>
#include <file.au3>

;setting the array for the list of computers you want checked
Dim $DigCtrl[1]
_ArrayAdd($DigCtrl, "digi01")
_ArrayAdd($DigCtrl, "digi02")
_ArrayAdd($DigCtrl, "digi03")
_ArrayAdd($DigCtrl, "digi04")
_ArrayAdd($DigCtrl, "digi05")
_ArrayAdd($DigCtrl, "digi06")
$DigCtrl[0] = UBound($DigCtrl) - 1

Dim $DigData[$DigCtrl[0] + 1]
$DigData[0] = $DigCtrl[0]

    ;Making a file to place the collected info
    _FileCreate ( "c:\IPlist.txt" )
$fileb = FileOpen("c:/IPlist.txt", 1)

; Display original array
For $var = 1 To UBound($DigCtrl) -1
    $data = $DigCtrl[$var]
    
            ;send ipconfig request to selected computer using psexec.exe from my c:/au3_tool folder. 
            Run(@ComSpec & ' /c c:\au3_tool\psexec \\' & $data & ' -u Administrator -p PASSWORD ipconfig /all > c:/ip.txt', '', @SW_HIDE)
            
            ;this can be commented out it's for feedback so you know it's collecting them or not
            MsgBox ( 0, $data , ipget () )
    
    ;Writing the info to the file
    FileWrite($fileb, ipget () & @CRLF)

Next

;Close the file because we are done with it
FileClose($fileb)

func ipget()
        ;open the file   
        $file = FileOpen("c:/ip.txt", 0)

            ; Check if file opened for reading OK
            If $file = -1 Then
                MsgBox(0, "Error", "Unable to open file.")
                Exit
            EndIf

            ;Timer Variable set at 0 outside loop to reset it
            $ass = 0
            
            ; Read in lines 34 and 40
            $vara = FileReadLine($file, 34)
            If @error = -1 Then
                do 
                    ;start timer
                    $ass = $ass + 1
                        ;send ipconfig request to selected computer again untill it works or I give up
                    Run(@ComSpec & ' /c c:\au3_tool\psexec \\' & $data & ' -u Administrator -p bu330ff ipconfig /all > c:/ip.txt', '', @SW_HIDE)
                    $vara = FileReadLine($file, 34)
                    
                ;reads from the string which should be        Pyhisical address ...... etc  for the first 9 characters
                $result = StringLeft($vara, 9)
                
                ;there is a lot of spaces and then the P so it will look for that or keep counting to 50
                Until $result = "        P" or $ass = 50
            EndIf
            
            ;if it gives up set values to "unknown"
            $varb = FileReadLine($file, 40)
            If @error = -1 Then $varb = ": unknown" 
            If @error = -1 Then $vara = ": unknown" 
                
                ;removes un-needed text
                $chara = StringRight($vara, 20)
                $charb = StringRight($varb, 14)
                
                ;Setting it to a readable format replacing the MAC and IP titles
                $ipinf = $data & @CRLF & "Mac: " & $chara & @cr & "IP: " & $charb
        
        ;MsgBox(0, $data, $data & " information:" & @cr  & @cr  & $chara & @cr & $charb)
                Return $ipinf
        FileClose($file)
        
        
    EndFunc

Share this post


Link to post
Share on other sites



@cowsmanaut

nice script but it's a little overkill to use an external appl. to get the info, if you can without it.

; ----------------------------------------------------------------------------
;
; IPConfig Data V1.0 by ptrex
;
; ----------------------------------------------------------------------------

#include <date.au3>

Dim $strComputer = "IP ADDRESS HERE"
Dim $objWMIService, $colAdapters, $n
Dim $utcLeaseObtained, $utcLeaseObtained, $utcLeaseExpired,  $strLeaseObtained, $utcLeaseExpires, $strLeaseExpires


 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

 $colAdapters = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 

$n = 1

 
For $objAdapter in $colAdapters
   ConsoleWrite ("Network Adapter " & $n & @CR)
   ConsoleWrite ("================="& @CR)
   ConsoleWrite ("  Description: " & $objAdapter.Description& @CR)
 
   ConsoleWrite ("  Physical (MAC) address: " & $objAdapter.MACAddress& @CR)
   ConsoleWrite ("  Host name:              " & $objAdapter.DNSHostName& @CR)
 
   If Not ($objAdapter.IPAddress) = " " Then
      For $i = 0 To UBound($objAdapter.IPAddress)
         ConsoleWrite ("  IP address:             " & $objAdapter.IPAddress($i)& @CR)
      Next
   EndIf
 
   If Not ($objAdapter.IPSubnet) = " " Then
      For $i = 0 To UBound($objAdapter.IPSubnet)
         ConsoleWrite ("  Subnet:                 " & $objAdapter.IPSubnet($i)& @CR)
      Next
   EndIf
 
   If Not ($objAdapter.DefaultIPGateway) = " " Then
      For $i = 0 To UBound($objAdapter.DefaultIPGateway)
         ConsoleWrite ("  Default gateway:        " & _
             $objAdapter.DefaultIPGateway($i)& @CR)
      Next
   EndIf
 
   ConsoleWrite ("  DNS"& @CR)
   ConsoleWrite ("  ---"& @CR)
   ConsoleWrite ("    DNS servers in search order:"& @CR)
 
   If Not ($objAdapter.DNSServerSearchOrder) = " " Then
      For $i = 0 To UBound($objAdapter.DNSServerSearchOrder)
         ConsoleWrite ("      " & $objAdapter.DNSServerSearchOrder($i)& @CR)
      Next
   EndIf
 
   ConsoleWrite ("    DNS domain: " & $objAdapter.DNSDomain& @CR)
 
   If Not ($objAdapter.DNSDomainSuffixSearchOrder) = " " Then
      For $i = 0 To UBound($objAdapter.DNSDomainSuffixSearchOrder)
         ConsoleWrite ("    DNS suffix search list: " & _
             $objAdapter.DNSDomainSuffixSearchOrder($i)& @CR)
      Next
   EndIf
 
   ConsoleWrite ("  DHCP"& @CR)
   ConsoleWrite ("  ----"& @CR)
   ConsoleWrite ("    DHCP enabled:        " & $objAdapter.DHCPEnabled& @CR)
   ConsoleWrite ("    DHCP server:         " & $objAdapter.DHCPServer& @CR)
 
   If Not ($objAdapter.DHCPLeaseObtained) = " " Then
      $utcLeaseObtained = $objAdapter.DHCPLeaseObtained
      $strLeaseObtained = WMIDateStringToDate($utcLeaseObtained)
   Else
      $strLeaseObtained = ""
   EndIf
   ConsoleWrite ("    DHCP lease obtained: " & $strLeaseObtained& @CR)
 
   If Not ($objAdapter.DHCPLeaseExpires) = " " Then
      $utcLeaseExpires = $objAdapter.DHCPLeaseExpires
      $strLeaseExpires = WMIDateStringToDate($utcLeaseExpires)
   Else
      $strLeaseExpires = ""
   EndIf
   ConsoleWrite ("    DHCP lease expires:  " & $strLeaseExpires& @CR)
 
   ConsoleWrite ("  WINS"& @CR)
   ConsoleWrite ("  ----"& @CR)
   ConsoleWrite ("    Primary WINS server:   " & $objAdapter.WINSPrimaryServer& @CR)
   ConsoleWrite ("    Secondary WINS server: " & $objAdapter.WINSSecondaryServer& @CR)
 
   $n = $n + 1
 
Next
 
Func WMIDateStringToDate($utcDate)
    Local $Return
   $Return = (StringMid($utcDate, 5, 2)  & "/" & _
       StringMid($utcDate, 7, 2)  & "/" & _
           StringLeft($utcDate, 4)    & " " & _
               StringMid ($utcDate, 9, 2) & ":" & _
                   StringMid($utcDate, 11, 2) & ":" & _
                      StringMid($utcDate, 13, 2))
    Return $Return
EndFunc

Enjoy !!

Share this post


Link to post
Share on other sites

ERROR:

$colAdapters = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

$colAdapters = $objWMIService.E^

Unable to parse line

I get this error when I try to run

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

for the computer I want? The idea here was to find the IP and MAC from the computers by only knowing their network name.

so if you have a series of computers, as I do in my lab but they get IP through DHCP and sometimes the computer services people change stuff.. then I can find out what ip if any they are getting.

The reason I needed to use PSexec is to run ipconfig on a networked machine rather than having to be at it's console. Not because it was needed to get the IP or MAC... also because it's a secure network you need to submit admin account and password to get to each machine which PSexec supports by adding the flag -u and -p for user and password.

I don't fully understand your script at the moment but seems like it's for the computre you are currently sitting at. Or I could be completely confused.. and it does look at all local networked machines and gives me their info? I guess it's just a matter of fully understanding all the commands you are using.

I'm certainly open to a better way if you have one. As I would like to not have to use PSexec for this. However, based on the criteria I didn't know how else to get it working.

Share this post


Link to post
Share on other sites

Have you wrote this for a range of IP address - that way you would only have to know the subnet and it would then tell you each machine on the network - maybe add the DNS name. Like a network scanner, but made with AutoIT.


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

@cowsmanaut

The idea here was to find the IP and MAC from the computers by only knowing their network name

Fill in the NETWORK NAME or IP ADDRESS.

Works both ways.

Why did you not try it before replying ? :wacko:

What' s there to understand it gives you the same details as you would get from the CONSOLE ?

Why make life complex by using external applicatiosns if AutoIT can give it all without depending in anything else. :D

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@cowsmanaut

Fill in the NETWORK NAME or IP ADDRESS.

Works both ways.

Why did you not try it before replying ? :wacko:

What' s there to understand it gives you the same details as you would get from the CONSOLE ?

Why make life complex by using external applicatiosns if AutoIT can give it all without depending in anything else. :D

I didn't try because the computers were at work and I wasn't there for a while to be able to try it. I did try it now however, I tried both IP address and network name and I again get the "unable to parse" error for line 16.

used

Dim $strComputer = "10.115.7.51"

and then tried

Dim $strComputer = "\\digi02"

and also tried

Dim $strComputer = "digi02"

I should point out that while I am learning and trying to use this scripting. I'm by no means a computer programmer. I'm a 3D artist. Limited scripting is requried in 3D and in this particular case I'm trying to do something to manage the lab I'm working in because of some IP conflicts when we reinstall all the computers. It's more of a learning out of necessity thing for me. So I'm liable to be a bit slow in the understanding of some concepts.

Also the amount of time I use this for is usually a couple of months a year when we go through and update the process and change scripts etc. Then I don't do it again for the rest of theyear and forget half of what I learned in the first place. So please be patient with me. :D

if you would like to see what I normally do for fun then you can look here. http://cowsmanaut.gfxartist.com

some of you guys script for fun.. I paint.

Edited by cowsmanaut

Share this post


Link to post
Share on other sites

to cowsmanaut :wacko:

You don't need any ip address (IPA) or computer name (CN) to test the code, just do this :

Dim $strComputer = "Localhost"

LocalHost means there is always an IPA : 127.0.0.1 if not connected to a network or an IPA automatically taken by your network adaptator from your modem (router,...), or one that you statically encoded.

TO see the result of ipconfig.au3, make this changes in the source (I'm using scite)

1. replace all @CR by @CRLF (carriage return / Line feed)

--> in scite CTRL+H --> find What = @CR and Replace with = @CRLF --> then replace all :D

2. ALWAYS compile all your au3 files using the latest version (including beta !!!) so check download section for beta installs

3. once compiled (ALT+F7 in scite), go to the directory in "DOS mode" : click on Windows Start / choose Run and type :

%comspec% and then press Enter

A black "Dos" Windows is now running command line

if you have compiled ipconfig.au3 in c:\MyProgs\ipconfig.exe then just type, in this "Dos box", the following commands :

CD\MyProgs (ENTER Key)

IPCONFIG >test.txt (ENTER Key)

exit

To see the results, uses Windows Explorer to open the file test.txt

Nota.: if there is spaces in your path, do this : "CD\My Prog Path With Spaces\Other Dir\" --> quotes needed !

Hope every things right ... :D


Logicae Homo EstFemina Homo EstFemina Logicae Est

Share this post


Link to post
Share on other sites

Why not use ARP to retrieve the MAC address?

Share this post


Link to post
Share on other sites

Fine code but still external to autoit ...

:whistle:

This is a Batch Scritp, you can add it to apps by reading the TEXT file.

It will also display the NIC, i always use it with autoit as it shows the main NIC settings.

-------------------------------------network.bat-----------------------------------------

@ECHO OFF

REM **********************************

REM * Author: Donald Muir *

REM * Date: 12 June 2006 *

REM * *

REM **********************************

DEL NET.txt

ipconfig/all | FIND /I "IP Address">> IP.txt

for /f " tokens=14* delims=, " %%i in (IP.txt) DO SET IPAddress=%%j

echo %IPAddress%>> NET.txt

DEL IP.txt

ipconfig/all | FIND /I "Subnet Mask">> SUB.txt

for /f " tokens=14* delims=, " %%a in (SUB.txt) DO SET SUBAddress=%%b

echo %SUBAddress%>> NET.txt

DEL SUB.txt

ipconfig/all | FIND /I "Default Gateway">> GATE.txt

for /f " tokens=12* delims=, " %%k in (GATE.txt) DO SET GATAddress=%%l

echo %GATAddress%>> NET.txt

DEL GATE.txt

NBTSTAT -a %IPAddress% | FIND /I "MAC Address">> MAC.txt

for /f " tokens=3* delims=, " %%i in (MAC.txt) DO SET MACAddress=%%j

echo %MACAddress%>> NET.txt

DEL MAC.txT

ipconfig/all | FIND /I "Description">> card.txt

for /f " tokens=13* delims=, " %%k in (card.txt) DO SET netcard=%%l

echo %netcard%>> NET.txt

DEL card.txt

ipconfig/all | FIND /I "DNS Servers">> dns.txt

for /f " tokens=14* delims=, " %%k in (dns.txt) DO SET netdns1=%%l

echo %netdns1%>> NET.txt

DEL dns.txt

----------------------------------------------end network.bat-----------------------------------

Cheers

Dwalf


Logicae Homo EstFemina Homo EstFemina Logicae Est

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0