Module1.vb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. Imports System.Net
  2. Module Module1
  3. Dim HostName As New ArrayList
  4. Dim IPv4 As New ArrayList
  5. Dim SubnetMask As New ArrayList
  6. Dim SystemGateWay As New ArrayList
  7. Dim DNS1 As New ArrayList
  8. Dim DNS2 As New ArrayList
  9. Dim NetworkCount As Integer = 0 'Count the number of network this PC is connected to. If more than two, require for selection
  10. Dim highPossibility As Integer = 0
  11. Dim results As New ArrayList
  12. Dim AOBFound As New ArrayList
  13. Dim threads As New ArrayList
  14. Sub Main()
  15. Console.WriteLine("Home Dynamic Local Area Network Scanning Utlities")
  16. GetAllIP()
  17. If NetworkCount > 1 Then
  18. 'Ask for which network to scan.
  19. Console.WriteLine("Multiple Network Found. Auto-selecting network " & highPossibility & "." & vbNewLine & "Selected gateway: " & SystemGateWay(highPossibility) & ".")
  20. End If
  21. pingAllIPsInSubnet(highPossibility)
  22. Console.WriteLine("IP Scanning Done.")
  23. results.Sort()
  24. Console.WriteLine("The following IPs respond to ping")
  25. FindAllAOB()
  26. Console.WriteLine("AOB System are located on these hosts: ")
  27. For k As Integer = 0 To AOBFound.Count - 1
  28. Console.WriteLine(AOBFound.Item(k))
  29. Next
  30. If AOBFound.Count = 1 Then
  31. Process.Start("http://" & AOBFound.Item(0))
  32. End If
  33. Console.WriteLine("DONE")
  34. Console.Read()
  35. End Sub
  36. Public Sub FindAllAOB()
  37. threads.Clear()
  38. For k As Integer = 0 To results.Count - 1
  39. Dim thisIP As String = results.Item(k)
  40. Dim newThread As New System.Threading.Thread(Sub() SendWebRequest(thisIP))
  41. threads.Add(newThread)
  42. newThread.Start()
  43. Console.WriteLine("Starting AOB connection on : " & thisIP)
  44. Next
  45. Dim running As Integer = results.Count - 1
  46. Dim lastvalue As Integer = 0
  47. While running > 0
  48. running = 0
  49. For i As Integer = 0 To threads.Count - 1
  50. If (threads.Item(i).IsAlive) Then
  51. running += 1
  52. End If
  53. Next
  54. If running <> lastvalue Then
  55. Console.WriteLine("Still waiting " & running & " threads to end.")
  56. End If
  57. lastvalue = running
  58. Threading.Thread.Sleep(200)
  59. End While
  60. End Sub
  61. Public Sub SendWebRequest(ip)
  62. Dim req As System.Net.WebRequest
  63. Dim res As System.Net.WebResponse
  64. req = System.Net.WebRequest.Create("http://" & ip & "/AOB")
  65. Try
  66. res = req.GetResponse()
  67. Console.WriteLine("AOB System Found on " & ip)
  68. AOBFound.Add(ip)
  69. Catch e As WebException
  70. Console.WriteLine("AOB not found on ip: " & ip)
  71. End Try
  72. End Sub
  73. Public Function getMaskValue(mask)
  74. If mask = "255.255.255.0" Then
  75. Return 1
  76. ElseIf mask = "255.255.0.0" Then
  77. Return 2
  78. Else
  79. Return -1 'Not supported for such a large network
  80. End If
  81. End Function
  82. Public Sub pingAllIPsInSubnet(networkid As Integer)
  83. Dim ip As String = IPv4.Item(networkid)
  84. Dim mk As String = SubnetMask.Item(networkid)
  85. Dim mkv As Integer = getMaskValue(mk)
  86. If (mkv <> -1) Then
  87. If mkv = 1 Then
  88. For d As Integer = 1 To 254
  89. Dim thisIP = ip.Substring(0, ip.LastIndexOf(".")) & "." & d.ToString
  90. Dim newThread As New System.Threading.Thread(Sub() CheckIfIpAlive(thisIP))
  91. threads.Add(newThread)
  92. newThread.Start()
  93. Console.WriteLine("Starting thread to connect : " & thisIP)
  94. 'Threading.Thread.Sleep(200)
  95. Next
  96. Dim running As Integer = 254
  97. Dim lastvalue As Integer = 0
  98. While running > 0
  99. running = 0
  100. For i As Integer = 0 To threads.Count - 1
  101. If (threads.Item(i).IsAlive) Then
  102. running += 1
  103. End If
  104. Next
  105. If running <> lastvalue Then
  106. Console.WriteLine("Still waiting " & running & " threads to end.")
  107. End If
  108. lastvalue = running
  109. Threading.Thread.Sleep(200)
  110. End While
  111. ElseIf mkv = 2 Then
  112. Console.Write("Currently not supported")
  113. End If
  114. Else
  115. Console.Write("Network too big. Please set up manually.")
  116. Return
  117. End If
  118. End Sub
  119. Public Sub CheckIfIpAlive(thisip)
  120. If tryPing(thisip) = True Then
  121. results.Add(thisip)
  122. End If
  123. End Sub
  124. Private Function FillZeros(val)
  125. If val < 10 Then
  126. Return "00" & val.ToString
  127. ElseIf val < 100 Then
  128. Return "0" & val.ToString
  129. Else
  130. Return val.ToString
  131. End If
  132. End Function
  133. Public Sub GetAllIP()
  134. 'On Error Resume Next
  135. HostName.Add(System.Net.Dns.GetHostName())
  136. For Each ip In System.Net.Dns.GetHostEntry(HostName.Item(NetworkCount)).AddressList
  137. If ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
  138. 'IPv4 Adress
  139. IPv4.Add(ip.ToString())
  140. For Each adapter As Net.NetworkInformation.NetworkInterface In Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
  141. For Each unicastIPAddressInformation As Net.NetworkInformation.UnicastIPAddressInformation In adapter.GetIPProperties().UnicastAddresses
  142. If unicastIPAddressInformation.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
  143. If ip.Equals(unicastIPAddressInformation.Address) Then
  144. 'Subnet Mask
  145. SubnetMask.Add(unicastIPAddressInformation.IPv4Mask.ToString())
  146. Dim adapterProperties As Net.NetworkInformation.IPInterfaceProperties = adapter.GetIPProperties()
  147. For Each gateway As Net.NetworkInformation.GatewayIPAddressInformation In adapterProperties.GatewayAddresses
  148. 'Default Gateway
  149. SystemGateWay.Add(gateway.Address.ToString())
  150. Next
  151. 'DNS1
  152. If adapterProperties.DnsAddresses.Count > 0 Then
  153. DNS1.Add(adapterProperties.DnsAddresses(0).ToString())
  154. End If
  155. 'DNS2
  156. If adapterProperties.DnsAddresses.Count > 1 Then
  157. DNS2.Add(adapterProperties.DnsAddresses(1).ToString())
  158. End If
  159. End If
  160. End If
  161. Next
  162. Next
  163. Console.WriteLine("IPv4: " & IPv4.Item(NetworkCount))
  164. Console.WriteLine("Subnet Mask: " & SubnetMask.Item(NetworkCount))
  165. If (SystemGateWay.Count <= NetworkCount) Then
  166. SystemGateWay.Add("N/A")
  167. Else
  168. highPossibility = NetworkCount
  169. End If
  170. Console.WriteLine("Gateway: " & SystemGateWay.Item(NetworkCount))
  171. Console.WriteLine("")
  172. NetworkCount += 1
  173. End If
  174. Next
  175. End Sub
  176. Public Function tryPing(ip As String)
  177. On Error GoTo errorPoint
  178. If My.Computer.Network.Ping(ip) Then
  179. Return True
  180. Else
  181. errorPoint:
  182. Return False
  183. End If
  184. End Function
  185. End Module