Jump to content
Sign in to follow this  
allSystemsGo

RunWait not accepting variable?

Recommended Posts

allSystemsGo

I am trying to run an arp commend that will find the MAC address of a PC given an IP. I call the ARP command using RunWait, but it acts as tho it doesn't pass the variable ($ip). The command does complete however and outputs the correct file, but it is not the correct command. The file it creates is the equivalent of "arp -a |Find "-" > c:tempmac.txt", but the script should be passing the IP in the command. Here is my code thus far.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#include <ButtonConstants.au3>
#include <Filev1.au3>
#include <array.au3>


DirCreate("c:\temp")
netreport()

Func netreport()
    Dim $report[50][3]
    $report[0][0] = "Computer name"
    $report [0][1] = "IP Address"
    $report [0][2] = "MAC Address"

    $pcnames = "c:\temp\pcnames.txt"
    RunWait(@Comspec & " /c " & "net view > " & $pcnames, "" , @SW_HIDE)
    FileOpen($pcnames,0)
    $x= 1
;~  $c = 2
    For $i= 4 to UBound($report) - 1

        $name = FileReadLine($pcnames,$i)
        $name = StringTrimLeft($name,2)

        $ip = RunWait(@Comspec & " /c " & "ping " & $name & ' -n 1 |Find "[" > c:\temp\yip.txt', "", @SW_HIDE)
        $iptxt = FileOpen("c:\temp\yip.txt",0)
        $ip = FileRead($iptxt)
        $ip = StringRegExpReplace($ip,'.+\[([^]]+).+', " $1 ")

        $mac = RunWait(@ComSpec & " /c " & "arp -a " & $ip & ' |Find "-" > c:\temp\mac.txt',"",@SW_HIDE) ; does it skip the $ip?

        $mactxt = FileOpen("c:\temp\mac.txt",0)

        $mac = FileReadLine($mactxt, 2)
        Dim $mactable[50][2]
        _FileReadToArray($mactxt,$mactable)
        _ArrayDisplay($mactable)

        $mac = StringTrimLeft($mac,17)
        $mac =StringTrimRight($mac,10)




        $report[$x][0] = $name
        $report[$x][1] = $ip
        $report[$x][2] = $mac


;~      MsgBox(64,"test",$i)
        $x = $x + 1
;~      $c = $c + 1
    Next




    _ArrayDisplay($report)

    EndFunc

Share this post


Link to post
Share on other sites
BrewManNH

You should never ever use @SW_HIDE in your script until you know that the command is working, equally you should never ever use /c when using @Comspec until it is working.

You probably have an error, but you'll never know it because the console is hidden, and the console closes as soon as it runs the command.How do you expect to troubleshoot the problem when you can't even tell what the problem is yet?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Chimp

... a different way?

#include<Array.au3>
TCPStartup()
$NetNames = Net_View()
Local $HostID[UBound($NetNames)][3]
For $i = 0 To UBound($NetNames) - 1
    $HostID[$i][0] = $NetNames[$i]
    $HostID[$i][1] = TCPNameToIP($NetNames[$i])
    $HostID[$i][2] = _GetMACFromIP($HostID[$i][1])
    ConsoleWrite($HostID[$i][0] & @TAB & $HostID[$i][1] & @TAB & $HostID[$i][2] & @CRLF)
Next
_ArrayDisplay($HostID)

Func _GetMACFromIP($sIP)
    Local $MAC, $MACSize
    Local $i, $s, $r, $iIP
    $MAC = DllStructCreate("byte[6]")
    $MACSize = DllStructCreate("int")
    DllStructSetData($MACSize, 1, 6)
    $r = DllCall("Ws2_32.dll", "int", "inet_addr", "str", $sIP)
    $iIP = $r[0]
    $r = DllCall("iphlpapi.dll", "int", "SendARP", "int", $iIP, "int", 0, "ptr", DllStructGetPtr($MAC), "ptr", DllStructGetPtr($MACSize))
    $s = ""
    For $i = 0 To 5
        If $i Then $s = $s & ":"
        $s = $s & Hex(DllStructGetData($MAC, 1, $i + 1), 2)
    Next
    Return $s
EndFunc   ;==>_GetMACFromIP

Func Net_View()
    ; http://www.autoitscript.com/forum/topic/31735-net-view-results/#entry227580
    Local $s_Buf = '', $a_Buf, $i_Pid = Run(@ComSpec & ' /c net view', '', @SW_HIDE, 2 + 4)
    While Not @error
        $s_Buf &= StdoutRead($i_Pid)
    WEnd
    $a_Buf = StringRegExp($s_Buf, "\\\\([0-9a-zA-Z-]*)", 3)
    ProcessClose($i_Pid)
    Return $a_Buf
EndFunc   ;==>Net_View

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
water

Your script misses error checking. Always make sure that a function or external command doesn't return an error before processing the next statement.

Either check the return value or @error to catch possible errors.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
allSystemsGo

@PincoPanco

Woah, that blows my script out of the water. Very nice! I see what is going on, but I am not very knowledgeable in the use of dll calls. Do you care to elaborate or point me to a source where I can learn more about this?

Share this post


Link to post
Share on other sites
Chimp

@PincoPanco

Woah, that blows my script out of the water. Very nice! I see what is going on, but I am not very knowledgeable in the use of dll calls. Do you care to elaborate or point me to a source where I can learn more about this?

 

Hi allSystemsGo

I am glad to be of help :)

I just assembled together two pieces of code found on various forum posts.

Net_View Func () is in this post:

     , # entry227580

_GetMACFromIP Func () is in this post:

     ,

the search function of the forum is a good friend of mine and an excellent resource for learning interesting things

it is often better to use ready-made pieces of code instead of reinventing the wheel ;)

bye


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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  

×