private void DoPortScan(CService service, int port, CNetwork.Protocols protocol = null, string stringsearch = null) { using (TcpClient client = new TcpClient()) { Log.Write(CFunctions.StringReplace("Checking " + protocol.ToString() + "://{0}:{1} for service presence ({2}).", service.IP.ToStringNz(), port.ToString(), stringsearch)); var result = client.BeginConnect(service.IP, port, null, null); var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(COptions.Connection_Timeout), true); bool found = false; if (success) { if (protocol == CNetwork.Protocols.HTTP) { try { WebRequest req = HttpWebRequest.Create("http://" + service.Name); req.Method = "GET"; using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream())) { if (stringsearch == null) { found = true; } // do not search if there is nothing to look for while (!reader.EndOfStream && !found) // otherwise search { string source = reader.ReadLine() + String.Empty; if (source.ToLower().Contains(stringsearch.ToLower())) { found = true; } } } } catch { found = false; } } else { found = true; } if (found) { lock (_ServerPortScanLock) { Log.Write("Service present (" + protocol.ToStringNz() + "): " + service.Name + " (" + service.Type + "); " + service.IP.ToStringNz()); _ServerPortScanResults.Add(service.Name + ";" + service.Type.ToString() + ";" + service.IP.ToStringNz() + ";" + port); } } } } }
private Dictionary <string, CService> CheckServiceIfRequired(Dictionary <string, CService> _ServicesToCheck) { List <CThread> ScanTasks = new List <CThread> { }; Dictionary <string, CService> _CheckedServers = new Dictionary <string, CService> { }; _ServerPortScanResults = new HashSet <string> { }; foreach (CService s in _ServicesToCheck.Values) { int port = 0; string search = null; CNetwork.Protocols protocol = null; if (s.Type == CServiceType.VCenterServer) { port = 80; search = "VMware vSphere"; protocol = new CNetwork.Protocols(CNetwork.Protocols.HTTP); } if (s.Type == CServiceType.HYVServer || s.Type == CServiceType.SCVMMServer) { port = 135; search = "RPC Endpoint Mapper"; protocol = new CNetwork.Protocols(CNetwork.Protocols.RPC); } if (port > 0) { CThread scan = new CThread() { Worker = new Thread(new ThreadStart(delegate() { DoPortScan(s, port, protocol, search); })) }; ScanTasks.Add(scan); Core.ThreadManager.Add(scan); } else { _CheckedServers.Add(s.Name + ";" + s.Type.ToString(), s); } } bool ScanTaskCompleted = false; do // wait till completed { bool status = true; for (int i = 0; i < ScanTasks.Count; i++) { if (!ScanTasks[i].Completed) { status = false; } } ScanTaskCompleted = status; Thread.Sleep(COptions.Session_Thread_Wait); } while (!ScanTaskCompleted); foreach (string s in _ServerPortScanResults) { string name = s.Split(';')[0]; string type = s.Split(';')[1]; _CheckedServers.Add(name + ";" + type, _ServicesToCheck[name + ";" + type]); } return(_CheckedServers); }