找回密码
 加入
搜索
查看: 3489|回复: 1

[网络通信] 怎么返回指定ip的计算机名?

[复制链接]
发表于 2012-8-16 21:33:00 | 显示全部楼层 |阅读模式
ping -a 192.168.0.10
这个有的地方不能返回,有的地方就可以。请问为什么?
另外有什么其他方法返回指定ip的计算机名吗?
或者返回一个局域网内所有的计算机名和IP对应的表呢?。



返回192.168.0.1~0.250的所有计算机ip和计算机名称。这个写的不好有地机器不能用在于】
$line = FileReadLine($file,2)
$w1 = StringSplit($line, "[")
$w2 = StringSplit($w1[1], " ")
$name = $w2[3]
这几段自己看着改吧
        #include <Process.au3>
FileDelete ("c:/ip.txt")
$ipd4 = "1"
$ipd123="192.168.0."
While $ipd4 <= 250
$namez = 0
$ipyn = 0
Dim $ip = $ipd123&$ipd4

$ipyn = Example($ip)

If $ipyn = 1 Then
        $namez = pcName($ip)
EndIf

        $ipd4+=1
        If $namez <> "bogon" Then

        IniWrite("c:\ip记录.ini", "ip", $ip, $namez)
        EndIf
WEnd        

Func Example($ip)

    ; Ping AutoIt 网站,超时时间为 250 毫秒.
    Local $iPing = Ping($ip, 250)
    If $iPing Then ; If a value greater than 0 was returned then display the following message.

                Return 1

    Else
;~ MsgBox(4096, "", "发生了一个错误, @error 值为: " & @error)
Return 0
    EndIf
Return 0
EndFunc   ;==>Example



Func pcName($ip)
_RunDos("ping -a "&$ip&" -n 1 >>c:\ip.txt")
$file = FileOpen("c:/ip.txt", 0)

; 检查文件是否正常打开
If $file = -1 Then
    MsgBox(0, "错误", "无法打开目标文件。")
    Exit
EndIf
$line = FileReadLine($file,2)
$w1 = StringSplit($line, "[")
$w2 = StringSplit($w1[1], " ")
$name = $w2[3]
FileClose($file)
FileDelete ("c:/ip.txt")
Return $name
EndFunc
        
发表于 2012-8-17 13:27:37 | 显示全部楼层
本帖最后由 qqgghh1 于 2012-8-17 13:29 编辑

分析ping -a  x.x.x.x  >>1.txt 效率和稳定性不太好,但也是一种方法。分享一段代码,论坛网友提供的。按理你用搜索完全能搜出很多结果来。这里是我的转帖:http://www.autoitx.com/thread-27535-1-1.html,主要是靠这两个函数实现你的需求,至于扫描整个局域网的,你参考上面链接再改改吧,应该不难。
Func __TCPIpToName($sIp, $iOption = Default, $hDll_Ws2_32 = Default)
        Local $vbinIP, $vaDllCall, $vptrHostent, $vHostent, $sHostnames, $vh_aliases, $i
        Local $INADDR_NONE = 0xffffffff, $AF_INET = 2, $sSeperator = @CR
        If $iOption = Default Then $iOption = 0
        If $hDll_Ws2_32 = Default Then $hDll_Ws2_32 = "Ws2_32.dll"
        $vaDllCall = DllCall($hDll_Ws2_32, "long", "inet_addr", "str", $sIp)
        If @error Then Return SetError(1, 0, "") ; inet_addr DllCall Failed
        $vbinIP = $vaDllCall[0]
        If $vbinIP = $INADDR_NONE Then Return SetError(2, 0, "") ; inet_addr Failed
        $vaDllCall = DllCall($hDll_Ws2_32, "ptr", "gethostbyaddr", "long*", $vbinIP, "int", 4, "int", $AF_INET)
        If @error Then Return SetError(3, 0, "") ; gethostbyaddr DllCall Failed
        $vptrHostent = $vaDllCall[0]
        If $vptrHostent = 0 Then
                $vaDllCall = DllCall($hDll_Ws2_32, "int", "WSAGetLastError")
                If @error Then Return SetError(5, 0, "") ; gethostbyaddr Failed, WSAGetLastError Failed
                Return SetError(4, $vaDllCall[0], "") ; gethostbyaddr Failed, WSAGetLastError = @Extended
        EndIf
        $vHostent = DllStructCreate("ptr;ptr;short;short;ptr", $vptrHostent)
        $sHostnames = __TCPIpToName_szStringRead(DllStructGetData($vHostent, 1))
        If @error Then Return SetError(6, 0, $sHostnames) ; strlen/sZStringRead Failed
        If $iOption = 1 Then
                $sHostnames &= $sSeperator
                For $i = 0 To 63 ; up to 64 Aliases
                        $vh_aliases = DllStructCreate("ptr", DllStructGetData($vHostent, 2) + ($i * 4))
                        If DllStructGetData($vh_aliases, 1) = 0 Then ExitLoop ; Null Pointer
                        $sHostnames &= __TCPIpToName_szStringRead(DllStructGetData($vh_aliases, 1))
                        If @error Then
                                SetError(7) ; Error reading array
                                ExitLoop
                        EndIf
                Next
                Return StringSplit(StringStripWS($sHostnames, 2), @CR)
        Else
                Return $sHostnames
        EndIf
EndFunc   ;==>__TCPIpToName

Func __TCPIpToName_szStringRead($iszPtr, $iLen = -1, $hDll_msvcrt = "msvcrt.dll")
        Local $aStrLen, $vszString
        If $iszPtr < 1 Then Return "" ; Null Pointer
        If $iLen < 0 Then
                $aStrLen = DllCall($hDll_msvcrt, "int:cdecl", "strlen", "ptr", $iszPtr)
                If @error Then Return SetError(1, 0, "") ; strlen Failed
                $iLen = $aStrLen[0] + 1
        EndIf
        $vszString = DllStructCreate("char[" & $iLen & "]", $iszPtr)
        If @error Then Return SetError(2, 0, "")
        Return SetError(0, $iLen, DllStructGetData($vszString, 1))
EndFunc   ;==>__TCPIpToName_szStringRead
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-4 19:15 , Processed in 0.078614 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表