autoit 发表于 2008-5-3 14:20:03

为AMU的MacToIp写的扫描程序(以nbtscan为核心

/attachment/<a href="attachment/Uploadfiles/2007-2/226149874.rar">Uploadfiles/2007-2/226149874.rar</a><br />下载放到脚本目录,不然会提示缺少文件。 <p>扫描出来虽然有好多项,但保存的时候格式为AMU的MacToIp 2.3的配置文件格式</p><p>参照:<a href="http://autoit.8800.org/ShowPost.asp?id=1109" target="_blank">[原创]根据MAC地址对应表修改计算机名、IP、CS-CDKEY。</a> 。</p><p>下面为源代码:</p><p>opt(&quot;TrayAutoPause&quot;,0)<br />$g_szVersion = &quot;MacScaner ver 1.02 for AMU MacToIp 2.3 by LuRen&quot;<br />If WinExists($g_szVersion) Then Exit<br />#i nclude &lt;GUIConstants.au3&gt;<br />#i nclude &lt;GuiStatusBar.au3&gt;<br />#i nclude &lt;file.au3&gt;<br />#i nclude &lt;GuiListView.au3&gt;<br />$dllsize = FileGetSize(@ScriptDir &amp; &quot;&#92;cygwin1.dll&quot;)<br />$exesize = FileGetSize(@ScriptDir &amp; &quot;&#92;nbtscan.exe&quot;)<br />If $dllsize &lt;&gt; 1140617 or $exesize &lt;&gt; 84736 Then<br />&nbsp;$msgbox = msgbox(8192+16+4,&quot;错误&quot;,&quot;缺少关键文件,是否要到 <a href="http://autoit.8800.org/" target="_blank">HTTP://AUTOIT.8800.ORG</a> 发贴求助!&quot;)<br />&nbsp;IF $msgbox = 6 Then<br />&nbsp;&nbsp;run(@ProgramFilesDir &amp; &quot;&#92;Internet Explorer&#92;IEXPLORE.EXE <a href="http://autoit.8800.org/" target="_blank">HTTP://AUTOIT.8800.ORG</a>&quot;)<br />&nbsp;EndIf<br />&nbsp;Exit<br />EndIf<br />$Form1 = GUICreate($g_szVersion, 570 , 320,-1,-1)<br />$Button1 = GUICtrlCreateButton(&quot;扫描&quot;, 500, 20, 60,22 )<br />$Button2 = GUICtrlCreateButton(&quot;删除&quot;, 500, 50, 60,22)<br />$Button3 = GUICtrlCreateButton(&quot;清空&quot;, 500, 80, 60,22)<br />$Button4 = GUICtrlCreateButton(&quot;整理&quot;, 500, 110, 60, 22)<br />$Button5 = GUICtrlCreateButton(&quot;保存&quot;, 500, 140, 60,22)<br />$Button6 = GUICtrlCreateButton(&quot;退出&quot;, 500, 170, 60, 22)<br />$Combo1 = GUICtrlCreateCombo(&quot;&quot;,1,1,490,20,0x0003)<br />Dim $Description,$ServiceName,$cards,$myipaddress,$mygeteway,$mysubnetmask,$mydns<br />For $i= 1 to 4<br />&nbsp;$var =&nbsp; RegEnumKey(&quot;HKEY_LOCAL_MACHINE&#92;SOFTWARE&#92;Microsoft&#92;Windows NT&#92;CurrentVersion&#92;NetworkCards&quot;,$i)<br />&nbsp;If @error &lt;&gt; 0 then<br />&nbsp;&nbsp;ExitLoop<br />&nbsp;Else<br />&nbsp;&nbsp;$Description[$i] = regread(&quot;HKEY_LOCAL_MACHINE&#92;SOFTWARE&#92;Microsoft&#92;Windows NT&#92;CurrentVersion&#92;NetworkCards&#92;&quot; &amp; $var ,&quot;Description&quot;)<br />&nbsp;&nbsp;$ServiceName[$i] =&nbsp; regread(&quot;HKEY_LOCAL_MACHINE&#92;SOFTWARE&#92;Microsoft&#92;Windows NT&#92;CurrentVersion&#92;NetworkCards&#92;&quot; &amp; $var ,&quot;ServiceName&quot;)<br />&nbsp;&nbsp;If stringlen($Description[$i]) &gt; 0 Then<br />&nbsp;&nbsp;&nbsp;$cards = $cards &amp; $Description[$i] &amp; &quot;|&quot;<br />&nbsp;&nbsp;EndIf<br />&nbsp;EndIf<br />Next<br />GUICtrlSetData($Combo1,$cards, $Description)<br />getnetworkconfig($Description)<br />$label1 =&nbsp; GUICtrlCreatelabel(&quot;&quot;, 500, 200, 60, 22,0x0201 )<br />$listview1 = GUICtrlCreateListView(&quot;IP Address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |NetBIOS Name&nbsp;&nbsp;&nbsp; |Server&nbsp;&nbsp; |User&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |MAC Address&nbsp;&nbsp; &quot;,1,20,490 ,280 )<br />Local $gui, $StatusBar1<br />Local $a_PartsRightEdge = <br />Local $a_PartsText = [&quot;本机名:&quot; &amp; @ComputerName,&quot;本机IP:&quot; &amp; $myipaddress,&quot;本机MAC:&quot;&amp; _GetMACFromIP($myipaddress),&quot;技术支持:Http://AutoIt.8800.org&quot;]<br />$StatusBar1 = _GUICtrlStatusBarCreate ($Form1, $a_PartsRightEdge, $a_PartsText)</p><p>GUISetState(@SW_SHOW)</p><p>While 1<br />&nbsp;$msg = GuiGetMsg()<br />&nbsp;Select<br />&nbsp;&nbsp;Case $msg = $GUI_EVENT_CLOSE or $msg = $Button6<br />&nbsp;&nbsp;&nbsp;ExitLoop<br />&nbsp;&nbsp;Case $msg = $Button1<br />&nbsp;&nbsp;&nbsp;scan()<br />&nbsp;&nbsp;Case $msg = $Button2<br />&nbsp;&nbsp;&nbsp;deleteitem()<br />&nbsp;&nbsp;Case $msg = $Button3<br />&nbsp;&nbsp;&nbsp;clearall()<br />&nbsp;&nbsp;Case $msg = $Button4<br />&nbsp;&nbsp;&nbsp;clearup()<br />&nbsp;&nbsp;Case $msg = $Button5<br />&nbsp;&nbsp;&nbsp;saveas()<br />&nbsp;&nbsp;Case $msg = $Combo1<br />&nbsp;&nbsp;&nbsp;getnetworkconfig(GUICtrlRead($Combo1))<br />&nbsp;&nbsp;&nbsp;_GUICtrlStatusBarSetText($StatusBar1, &quot;本机IP:&quot; &amp; $myipaddress,1)<br />&nbsp;&nbsp;&nbsp;_GUICtrlStatusBarSetText($StatusBar1, &quot;本机MAC:&quot;&amp; _GetMACFromIP($myipaddress),2)<br />&nbsp;&nbsp;case $msg = $listview1<br />&nbsp;&nbsp;&nbsp;Dim $B_DESCENDING<br />&nbsp;&nbsp;&nbsp;_GUICtrlListViewSort($listview1, $B_DESCENDING, GUICtrlGetState($listview1))<br />&nbsp;EndSelect<br />WEnd<br />Exit<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func OnAutoItExit()&nbsp;&nbsp;&nbsp; ;;;脚本退出时清理临时文件<br />FileDelete(@TempDir &amp; &quot;&#92;scaneripadd.txt&quot;)<br />EndFunc<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func _GetMACFromIP($sIP)&nbsp;&nbsp;&nbsp; ;;根椐IP取MAC,此程序中只是为了取本机的MAC用以显示在状态栏中<br />&nbsp;Local $MAC, $MACSize<br />&nbsp;Local $i, $s, $r, $iIP<br />&nbsp;$MAC = DllStructCreate(&quot;byte&quot;)<br />&nbsp;$MACSize = DllStructCreate(&quot;int&quot;)<br />&nbsp;DllStructSetData($MACSize, 1, 6)<br />&nbsp;$r = DllCall(&quot;Ws2_32.dll&quot;, &quot;int&quot;, &quot;inet_addr&quot;, &quot;str&quot;, $sIP)<br />&nbsp;$iIP = $r<br />&nbsp;$r = DllCall(&quot;iphlpapi.dll&quot;, &quot;int&quot;, &quot;SendARP&quot;, &quot;int&quot;, $iIP, &quot;int&quot;, 0, &quot;ptr&quot;, DllStructGetPtr($MAC), &quot;ptr&quot;, DllStructGetPtr($MACSize))<br />&nbsp;$s = &quot;&quot;<br />&nbsp;For $i = 0 To 5<br />&nbsp;&nbsp;If $i Then $s = $s &amp; &quot;-&quot;<br />&nbsp;&nbsp;$s = $s &amp; Hex(DllStructGetData($MAC, 1, $i + 1), 2)<br />&nbsp;Next<br />&nbsp;Return $s<br />EndFunc&nbsp;&nbsp; ;==&gt;_GetMACFromIP<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func scan()&nbsp;&nbsp;&nbsp;&nbsp; ;;;扫描,创建列表<br />$pid = Run(@ComSpec &amp; ' /c &quot;' &amp; @ScriptDir &amp; '&#92;nbtscan.exe&quot; -r ' &amp; $myipaddress &amp; &quot;/&quot; &amp; CountSubnetMaskBit($mysubnetmask) &amp; &quot;&gt;&quot; &amp; @TempDir &amp; &quot;&#92;scaneripadd.txt&quot;,@TempDir,@SW_HIDE)<br />$x = True<br />&nbsp;Do<br />&nbsp;&nbsp;If $x Then<br />&nbsp;&nbsp;&nbsp;GUICtrlSetData($label1,&quot;扫描中&quot;)<br />&nbsp;&nbsp;&nbsp;GUICtrlSetBkColor($label1,0x00FF00)<br />&nbsp;&nbsp;Else<br />&nbsp;&nbsp;&nbsp;GUICtrlSetBkColor($label1,-1)<br />&nbsp;&nbsp;EndIf<br />&nbsp;&nbsp;$x = not $x<br />&nbsp;&nbsp;sleep(300)<br />&nbsp;Until Not ProcessExists($pid)<br />&nbsp;GUICtrlSetBkColor($label1,-1)<br />&nbsp;GUICtrlSetData($label1,&quot;处理中&quot;)<br />&nbsp;dim $arrayiplist<br />;~ &nbsp;dim $arrayiplist<br />&nbsp;If Not _FileReadToArray(@tempdir &amp; &quot;&#92;scaneripadd.txt&quot;,$arrayiplist) Then<br />&nbsp;&nbsp;SetError(1)<br />&nbsp;&nbsp;Return -1<br />&nbsp;EndIf<br />&nbsp;For $x = $arrayiplist to 1 step -1<br />&nbsp;&nbsp;$line = $arrayiplist[$x]<br />&nbsp;&nbsp;If $line = &quot;------------------------------------------------------------------------------&quot; Then<br />&nbsp;&nbsp;&nbsp;ExitLoop<br />&nbsp;&nbsp;ElseIf&nbsp; stringlen($line) = 0 Then<br />&nbsp;&nbsp;&nbsp;ContinueLoop<br />&nbsp;&nbsp;Else<br />&nbsp;&nbsp;&nbsp;$ipaddress = StringStripWS(stringleft($line,17),3)<br />&nbsp;&nbsp;&nbsp;$netbios&gt;&nbsp;&nbsp;&nbsp;$server = StringStripWS(stringmid($line,35,10),3)<br />&nbsp;&nbsp;&nbsp;$user = StringStripWS(stringmid($line,45,17),3)<br />&nbsp;&nbsp;&nbsp;$macaddress = StringUpper(stringmid($line,62,17))<br />&nbsp;&nbsp;&nbsp;GUICtrlCreateListViewItem($ipaddress &amp; &quot;|&quot; &amp; $netbios&gt;&nbsp;&nbsp;EndIf<br />&nbsp;Next<br />&nbsp;GUICtrlSetData($label1,&quot;完成&quot;)<br />EndFunc&nbsp;&nbsp; ;==&gt;scan<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func CountSubnetMaskBit($subnetmask)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;;;统计子网掩码的位数<br />&nbsp;$subnetmasksp = StringSplit($subnetmask, &quot;.&quot;)<br />&nbsp;If $subnetmasksp &lt;&gt; 4 Then<br />&nbsp;&nbsp;SetError(1)<br />&nbsp;&nbsp;Return 32<br />&nbsp;EndIf<br />&nbsp;$bit = 0<br />&nbsp;For $x = 1 To 4<br />&nbsp;&nbsp;If $subnetmasksp[$x] = 255 Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 8<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] = 254&nbsp; Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 7<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] = 252&nbsp; Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 6<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] = 248&nbsp; Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 5<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] = 240&nbsp; Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 4<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] = 224&nbsp; Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 3<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] = 192&nbsp; Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 2<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] = 128&nbsp; Then<br />&nbsp;&nbsp;&nbsp;$bit = $bit + 1<br />&nbsp;&nbsp;ElseIf $subnetmasksp[$x] &lt;&gt; 0 Then<br />&nbsp;&nbsp;&nbsp;SetError(1)<br />&nbsp;&nbsp;&nbsp;Return 32<br />&nbsp;&nbsp;EndIf<br />&nbsp;Next<br />&nbsp;If $bit &gt;= 1 And $bit &lt;= 32 Then<br />&nbsp;&nbsp;Return $bit<br />&nbsp;Else<br />&nbsp;&nbsp;SetError(1)<br />&nbsp;&nbsp;Return 32<br />&nbsp;EndIf<br />EndFunc&nbsp;&nbsp; ;==&gt;CountSubnetMaskBit<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func clearall()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;;清空<br />&nbsp;_GUICtrlListViewDeleteAllItems($listview1)<br />&nbsp;guictrlsetdata($label1,&quot;&quot;)<br />EndFunc&nbsp;&nbsp; ;==&gt;clearall<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func clearup()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;;整理重复<br />&nbsp;Local $count<br />&nbsp;$count = _GUICtrlListViewGetItemCount($listview1)<br />&nbsp;If $count Then<br />&nbsp;&nbsp;dim $itemarray[$count]<br />&nbsp;EndIf<br />&nbsp;for $x = 0 to $count - 1<br />&nbsp;&nbsp;$itemarray[$x] = _GUICtrlListViewGetItemText($listview1,$x)<br />&nbsp;Next<br />&nbsp;For $x = 0 to $count - 1<br />&nbsp;&nbsp;For $y = $x + 1 to $count - 1<br />&nbsp;&nbsp;&nbsp;If $itemarray[$x] = $itemarray[$y] Then<br />&nbsp;&nbsp;&nbsp;&nbsp;$itemarray[$y] = &quot;&quot;<br />&nbsp;&nbsp;&nbsp;EndIf<br />&nbsp;&nbsp;Next<br />&nbsp;Next<br />&nbsp;_GUICtrlListViewDeleteAllItems($listview1)<br />&nbsp;For $x = 0 to $count - 1<br />&nbsp;&nbsp;If StringLen($itemarray[$x]) &gt; 0 then<br />&nbsp;&nbsp;&nbsp;GUICtrlCreateListViewItem($itemarray[$x],$listview1)<br />&nbsp;&nbsp;EndIf<br />&nbsp;Next<br />&nbsp;guictrlsetdata($label1,&quot;&quot;)<br />EndFunc&nbsp;&nbsp; ;==&gt;clearup<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func deleteitem()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;;;;删除一项<br />&nbsp;If _GUICtrlListViewGetSelectedCount($listview1) Then<br />&nbsp;&nbsp;_GUICtrlListViewDeleteItemsSelected($listview1)<br />&nbsp;EndIf<br />EndFunc&nbsp;&nbsp; ;==&gt;deleteitem<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />Func saveas()&nbsp;&nbsp;&nbsp; ;;;保存<br />&nbsp;Local $count,$lines<br />&nbsp;$count = _GUICtrlListViewGetItemCount($listview1)<br />&nbsp;If $count &gt;=1 Then<br />&nbsp;&nbsp;Local $savetextarray<br />&nbsp;&nbsp;For $x = 0 to $count - 1<br />&nbsp;&nbsp;&nbsp;$savetextarray = _GUICtrlListViewGetItemTextArray($listview1,$x)<br />&nbsp;&nbsp;&nbsp;$lines = $lines &amp;&nbsp; $savetextarray &amp; &quot;=&quot; &amp; $savetextarray &amp; &quot;|&quot; &amp; $savetextarray &amp; @CRLF<br />&nbsp;&nbsp;Next<br />&nbsp;&nbsp;$lines = &quot;&quot; &amp; @CRLF &amp; &quot;Mask=&quot; &amp; $mysubnetmask &amp; @CRLF &amp; &quot;GateWay=&quot; &amp; $mygeteway &amp; @CRLF &amp; &quot;主DNS=&quot; &amp; $mydns &amp; @CRLF &amp; &quot;副DNS=&quot; &amp; $mydns &amp; @CRLF&nbsp; &amp; &quot;&quot; &amp; @CRLF &amp; $lines &amp; &quot;;;CSCDK自己根椐格式复制一下,太多了,不好写。&quot;<br />&nbsp;&nbsp;If fileexists(@ScriptDir &amp; &quot;&#92;Mac2Ip.ini&quot;) Then<br />&nbsp;&nbsp;&nbsp;$count = MsgBox(8192+256+32+4,&quot;文件已存在&quot;,&quot;文件&quot; &amp; @ScriptDir &amp; &quot;&#92;Mac2Ip.ini&quot; &amp; &quot;已经存在,是否覆盖?&quot;)<br />&nbsp;&nbsp;&nbsp;If $count = 6 Then<br />&nbsp;&nbsp;&nbsp;&nbsp;$inifile = fileopen(@ScriptDir &amp; &quot;&#92;Mac2Ip.ini&quot;,2)<br />&nbsp;&nbsp;&nbsp;&nbsp;$x = FileWrite($inifile,$lines)<br />&nbsp;&nbsp;&nbsp;&nbsp;FileClose($inifile)<br />&nbsp;&nbsp;&nbsp;&nbsp;If $x Then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GUICtrlSetData($label1,&quot;保存成功&quot;)<br />&nbsp;&nbsp;&nbsp;&nbsp;Else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GUICtrlSetData($label1,&quot;写入失败&quot;)<br />&nbsp;&nbsp;&nbsp;&nbsp;EndIf<br />&nbsp;&nbsp;&nbsp;EndIf<br />&nbsp;&nbsp;Else<br />&nbsp;&nbsp;&nbsp;&nbsp;$inifile = fileopen(@ScriptDir &amp; &quot;&#92;Mac2Ip.ini&quot;,2)<br />&nbsp;&nbsp;&nbsp;&nbsp;$x = FileWrite($inifile,$lines)<br />&nbsp;&nbsp;&nbsp;&nbsp;FileClose($inifile)<br />&nbsp;&nbsp;&nbsp;&nbsp;If $x Then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GUICtrlSetData($label1,&quot;保存成功&quot;)<br />&nbsp;&nbsp;&nbsp;&nbsp;Else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GUICtrlSetData($label1,&quot;写入失败&quot;)<br />&nbsp;&nbsp;&nbsp;&nbsp;EndIf<br />&nbsp;&nbsp;EndIf<br />&nbsp;EndIf<br />EndFunc&nbsp;&nbsp; ;==&gt;saveas<br />;;;;;;;;;;;;;;;<br />Func getnetworkconfig($tempstr)<br />&nbsp;for $i = 1 to 4<br />&nbsp;&nbsp;If $tempstr = $Description[$i] Then<br />&nbsp;&nbsp;&nbsp;$myipaddress = regread(&quot;HKEY_LOCAL_MACHINE&#92;SYSTEM&#92;CurrentControlSet&#92;Services&#92;Tcpip&#92;Parameters&#92;Interfaces&#92;&quot;&amp; $ServiceName[$i] ,&quot;IPAddress&quot;)<br />&nbsp;&nbsp;&nbsp;$mysubnetmask = regread(&quot;HKEY_LOCAL_MACHINE&#92;SYSTEM&#92;CurrentControlSet&#92;Services&#92;Tcpip&#92;Parameters&#92;Interfaces&#92;&quot;&amp; $ServiceName[$i] ,&quot;SubnetMask&quot;)<br />&nbsp;&nbsp;&nbsp;$mygeteway = regread(&quot;HKEY_LOCAL_MACHINE&#92;SYSTEM&#92;CurrentControlSet&#92;Services&#92;Tcpip&#92;Parameters&#92;Interfaces&#92;&quot;&amp; $ServiceName[$i] ,&quot;DefaultGateway&quot;)<br />&nbsp;&nbsp;&nbsp;$mydns = regread(&quot;HKEY_LOCAL_MACHINE&#92;SYSTEM&#92;CurrentControlSet&#92;Services&#92;Tcpip&#92;Parameters&#92;Interfaces&#92;&quot;&amp; $ServiceName[$i] ,&quot;NameServer&quot;)<br />&nbsp;&nbsp;&nbsp;$mydns = StringSplit($mydns,&quot;,&quot;)<br />&nbsp;&nbsp;&nbsp;ExitLoop<br />&nbsp;&nbsp;EndIf<br />&nbsp;Next<br />EndFunc&nbsp;&nbsp; ;==&gt;getnetworkconfig</p>

stardust 发表于 2008-8-31 19:57:53

居然没人回帖……:face (32):

楼主好象没讲清楚做什么用的。

haodd 发表于 2010-8-10 12:30:50

神人啊相当有用滴啊

tsui 发表于 2011-11-11 20:53:35

nbtscan生成列表太慢了啊,纠结
页: [1]
查看完整版本: 为AMU的MacToIp写的扫描程序(以nbtscan为核心