Sign in to follow this  
Followers 0
neebo

getting computername via ping -a

8 posts in this topic

Hello again

Working on a script which copies certain file over multiple local networks.

At phase 1 the script pings local network addressess 1>>255, creating an array with a value "ONLINE" or "offline"

At phase 2 it attempts to map an "ONLINE"-computer and transfer a file, ignoring "offline"-computers

However there are certain computers in network which are to be excluded from transfer. Problem is that their IP's vary.

These could be identified by the network name... but how this network name can be transferred to my script?

#include <array.au3>
#include <string.au3>

$logtitle = "Untitled - Notepad"
$myip = @IPAddress1 ; my ip-address
$array1 = _StringExplode($myip, ".", 0)
$add1 = $array1[0]
$add2 = $array1[1]
$add3 = $array1[2]
;$add4 = $array1[3]
$localIp = $add1 & "." & $add2 & "." & $add3 & "."


Local $ipArray[255]
For $i = 1 to 255 Step 1
    $var = Ping($localip & $i)
    If NOT $var Then
        $ipArray[$i-1] = "offline"

    ElseIf
        ;if computers "network name" begins with "FOO" ie "FOO1", "FOO2" etc, it is "offline"
    Else
        $ipArray[$i-1] = "ONLINE"

    EndIf
    Sleep(250)

Next

;adding constant ips that are always blacklisted as "offline" in every network
$ipArray[0] = "offline" 
$ipArray[14] = "offline"
$ipArray[30] = "offline"

Share this post


Link to post
Share on other sites



You can use WMI to get the hostname.

Try this:

MsgBox(0, "Test", WMI_DNSHostName("localhost"))

Func WMI_DNSHostName($srv)
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $srv & "\root\cimv2")
    Local $DNSHostName, $colItems, $colItem, $ping
    $ping = Ping($srv)
    If $ping Then
        $colItems = $objWMIService.ExecQuery("SELECT DNSHostName FROM Win32_ComputerSystem", "WQL", 0x30)
        If IsObj($colItems) Then
            For $objItem In $colItems
                $DNSHostName = $objItem.DNSHostName
            Next
            SetError(0)
            Return $DNSHostName
        Else
            SetError(1)
            Return "Error!"
        EndIf
    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Wow, thanks!!!

Gave it a try. With "localhost" of "127.0.0.1" it worked fine. When attempting to query another known computer in local network, no go.

MsgBox(0, "Test", WMI_DNSHostName("*.*.*.*")) (* stands for part of ip-add)

-neeb

Share this post


Link to post
Share on other sites

What does "no go" mean exactly? Do you get any error message?

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ah, sorry.

Nothing happens. No error-message, no nothing. After awhile the script closes itself.

When compiling to exe and running it I get an error:" Line7 - Variable must be of type "Object".: "

os: Windows7 Enterprise 64-bit eng

Edited by neebo

Share this post


Link to post
Share on other sites

Not the question you where asking, but ...

Since each PC's online/offline status may change while your script is running, may I suggest transferring the files as right after detecting online status, rather than caching the status, risking a change by the time you get to it.

Ex:

$MySubnet = "192.168.1."
For $i = 1 to 255
  If Ping($MySubnet & $i) > 0 Then TransferFiles($MySubnet & $i)
Next

Func TransferFiles($IP)
  ;Check to see if PC should be skipped
  ;Transfer files
EndFunc

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Try this to get an em on any error:

$oMyError = ObjEvent("AutoIt.Error", "oMyError") ; Install a custom error handler

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

MsgBox(0, "Test", WMI_DNSHostName($ip))

Func WMI_DNSHostName($srv)
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $srv & "\root\cimv2")
    Local $DNSHostName, $colItems, $colItem, $ping
    $ping = Ping($srv)
    If $ping Then
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", 0x30)
    If IsObj($colItems) Then
    For $objItem In $colItems
    $DNSHostName = $objItem.DNSHostName
    Next
    SetError(0)
    Return $DNSHostName
    Else
    SetError(1)
    Return "Error!"
    EndIf
    Else
    SetError(1)
    Return "Host not reachable"
    EndIf
EndFunc

Func oMyError()
 Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"   & @CRLF & @CRLF & _
    "err.description is: "  & @TAB & $oMyError.Description  & @CRLF & _
    "err.windescription:"   & @TAB & $oMyError.WinDescription & @CRLF & _
    "err.number is: "   & @TAB & Hex($oMyError.Number, 8) & @CRLF & _
    "err.lastdllerror is: " & @TAB & $oMyError.LastDllError & @CRLF & _
    "err.scriptline is: "   & @TAB & $oMyError.Scriptline   & @CRLF & _
    "err.source is: "   & @TAB & $oMyError.Source   & @CRLF & _
    "err.helpfile is: "     & @TAB & $oMyError.Helpfile     & @CRLF & _
    "err.helpcontext is: "  & @TAB & $oMyError.HelpContext _
    )

    Local $err = $oMyError.number
    If $err = 0 Then $err = -1

    $g_eventerror = $err ; to check for after this function returns
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

This method is slow but it yields an array (1 based) in which the odd numbers are names and the even numbers are IP addresses, if unreachable it notes that twice to occupy the spaces.

so yes it is possible with ping -a :graduated:

#Include <File.au3>
#Include <Array.au3>

$pinglog = "c:\ping.txt"

Dim $ipArray[254]
Dim $NameArr[1]
$i = 42

While $i < 47

    $var = Ping("192.168.205." & $i)
    If NOT $var Then
       _ArrayAdd ($NameArr , $i & " is offline")
        _ArrayAdd ($NameArr , $i & " is offline")

Else

runwait ("cmd /c Ping -a 192.168.205." & $i & " > " & $pinglog)

Dim $pingArr
_FileReadToArray ($pinglog , $pingArr)

Dim $name
$name = _ArrayFindAll ($pingArr , "Pinging" , 0 , 0 , 0 , 1)

$NameString = $pingArr[$name[0]]


$NameString1 = StringRegExpReplace ($NameString , "[[]" , '' , 0)
$NameString2 = StringRegExpReplace ($NameString1 , "[]]" , '' , 0)
$NameString3 = StringRegExpReplace ($NameString2 , "Pinging " , '' , 1)
$NameString4 = StringRegExpReplace ($NameString3 , "with 32 bytes of data:" , '' , 1)
$NameStringFinal = stringtrimright ($NameString4 , 1)

$SplitArr = StringSplit ($NameStringFinal , " " , 2)

_ArrayAdd ($NameArr , $SplitArr[0])
_ArrayAdd ($NameArr , $SplitArr[1])

Endif
$i += 1
Wend


_ArrayDisplay ($NameArr)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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