Module1.vb 9.0 KB

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