private void btnAddComputer_Click(object sender, RoutedEventArgs e) { var clientListString = ""; clientListString += Convert.ToInt32(txRoomNr.Text) + " " + Convert.ToInt32(txtbNr.Text) + " " + txbName.Text + " " + "" + " " + txbMacAddress.Text + Environment.NewLine; var fileClients = new StreamWriter("clients.txt", true); fileClients.Write(clientListString); fileClients.Close(); var lc = new LabClient(Convert.ToInt32(txRoomNr.Text), txbName.Text, Convert.ToInt32(txtbNr.Text), txbMacAddress.Text); Service.GetInstance().clientlist.Add(lc); MessageBox.Show( "The LabClient has been added, if its turn on it should appear on the list with a white background"); Close(); }
public override bool Equals(System.Object obj) { // If parameter is null return false. if (obj == null) { return(false); } // If parameter cannot be cast to Point return false. LabClient p = obj as LabClient; if ((System.Object)p == null) { return(false); } // Return true if the fields match: return(ComputerName == p.ComputerName); }
// Used to get the current logged in user on a remote computer public void RunCmd(string target, LabClient client) { var s = ""; using (var process = new Process()) { process.StartInfo.FileName = target; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; var output = new StringBuilder(); var error = new StringBuilder(); var domainName = _service.Credentials.DomainName.ToUpper(); using (var outputWaitHandle = new AutoResetEvent(false)) using (var errorWaitHandle = new AutoResetEvent(false)) //Used to find whatever user that currently is online with the domain Credentials { process.OutputDataReceived += (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } else { if (e.Data.Contains(domainName)) { var index = e.Data.IndexOf(domainName); var temp = e.Data.Substring(index, e.Data.Length - index); s = temp.Substring(4); client.CurrentlyLoggedInUser = s; } } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) { errorWaitHandle.Set(); } else { error.Append(e.Data); } }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); var timeout = 15000; if (process.WaitForExit(timeout) && outputWaitHandle.WaitOne(timeout) && errorWaitHandle.WaitOne(timeout)) { //Could type some Process.Exist code here but it aint needed. Debug.Write(output); Debug.Write(error); } } } }
// Overloaded method used to store the threads ID and what file gets opened when CustomRun is ran. public void RunCmd(string target, string fileName, LabClient client) { var s = ""; using (var process = new Process()) { process.StartInfo.FileName = target; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; var output = new StringBuilder(); var error = new StringBuilder(); using (var outputWaitHandle = new AutoResetEvent(false)) using (var errorWaitHandle = new AutoResetEvent(false)) { process.OutputDataReceived += (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) { errorWaitHandle.Set(); } else { if (e.Data.Contains("ID")) { var index = e.Data.IndexOf("ID"); var temp = e.Data.Substring(index, e.Data.Length - index - 1); s = temp.Substring(3, temp.Length - 3); Debug.Write("TEST: ", s); var cp1 = new CompAndProcesses(); cp1.computer = client; cp1.processName = fileName; cp1.threadID = s; CompAndProcesseses.Add(cp1); } error.AppendLine(e.Data); } }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); var timeout = 15000; if (process.WaitForExit(timeout) && outputWaitHandle.WaitOne(timeout) && errorWaitHandle.WaitOne(timeout)) { //Could type some Process.Exist code here but it aint needed. Debug.Write(output); Debug.Write("ERROR:" + error); } } } }
/// <summary> /// Downloads the bridge's list of computers which have MAC and booth number. /// Then connects MACs to IP-s using local ARP pool, and looks up computer names using NBTSTAT from IP address. /// Throws exception if ARP list is not filled up sufficiently or the bridge's client list cannot be downloaded. /// </summary> /// <returns>List of clients</returns> public List<LabClient> GetLabComputersNew2(int labNo) { var clientlist = new List<LabClient>(); //Get MAC addresses from Bridge try { var contents = new WebClient().DownloadString("http://10.204.77.17:8000/?downloadcfg=1"); // Write bridge list to a file. var file0 = new StreamWriter("bridgelist.txt"); file0.WriteLine(contents); file0.Close(); } catch (Exception ex) { throw new WebException("Error trying to reach the bridge's client list. Error:", ex); } //Get MAC addresses from list using (var file = new StreamReader("bridgelist.txt")) { int roomNo; int boothNo; string line; string mac; while (((line = file.ReadLine()) != null) && (line.Length > 10)) { roomNo = int.Parse(line.Substring(0, 1)); mac = line.Substring(4); mac = mac.Replace(" ", string.Empty); mac = mac.Replace(":", string.Empty); mac = mac.Replace("\u0009", string.Empty); boothNo = int.Parse(line.Substring(2, 2).Trim()); var client = new LabClient(roomNo, "", boothNo, mac, ""); clientlist.Add(client); } } //Get local ARP list to match MAC addresses to IP-s var p = new Process(); p.StartInfo = new ProcessStartInfo(@"C:\Windows\System32\arp.exe", "/a") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, RedirectStandardInput = true, CreateNoWindow = true }; p.Start(); //Store the output to a string var Contents = p.StandardOutput.ReadToEnd(); // Write the string to a file. var file2 = new StreamWriter("arp.txt"); file2.WriteLine(Contents); file2.Close(); //Process file using (var file = new StreamReader("arp.txt")) { string line; var mac = ""; var ip = ""; var i = 1; var cutLines = 3; while ((line = file.ReadLine()) != null) { if (i <= cutLines) { i++; } else { if (line.Length > 20) { ip = line.Substring(0, 17); ip = ip.Trim(); mac = line.Substring(17, 25); mac = mac.Replace(" ", string.Empty); mac = mac.Replace("-", string.Empty); mac = mac.Replace("\u0009", string.Empty); } //Check for matching MACs, if found, update list of clients with IP foreach (var client in clientlist) { if (client.Mac == mac) { client.Ip = ip; } } } } //Get computer names, match with IP using NBTSTAT foreach (var client in clientlist) { var contents5 = ExecuteCommand("nbtstat.exe -a " + client.Ip, true); if (contents5.IndexOf("<00> UNIQUE") == -1) { throw new Exception(); } var until = contents5.Substring(0, contents5.IndexOf("<00> UNIQUE")).Trim(); var stringArray = until.Split(null); var name = ""; foreach (var str in stringArray) { name = str; } client.ComputerName = name; } //Write clientlist to file for testing var clientListString = ""; foreach (var client in clientlist) { clientListString += client.RoomNo + " " + client.BoothNo + " " + client.ComputerName + " " + client.Ip + " " + client.Mac + Environment.NewLine; var fileClients = new StreamWriter("clients.txt"); fileClients.WriteLine(clientListString); fileClients.Close(); } } return clientlist; }
/// <summary> /// Reads the clients.txt into the program for a list of computers in a specific lab. /// </summary> /// <returns>List of clients</returns> public List<LabClient> GetLabComputersFromStorage() { using (var file = new StreamReader("clients.txt")) { int roomNo; int boothNo; string line; string mac; string compname; string ip; while (((line = file.ReadLine()) != null)) { var data = line.Split(null); foreach (var temp in data) { Debug.WriteLine(temp); } roomNo = int.Parse(data[0]); boothNo = int.Parse(data[1]); compname = data[2]; ip = data[3]; mac = data[4]; var client = new LabClient(roomNo, compname, boothNo, mac, ip); Clientlist.Add(client); } } return Clientlist; }
// Overloaded method used to store the threads ID and what file gets opened when CustomRun is ran. public void RunCmd(string target, string fileName, LabClient client) { var pID = ""; using (var process = new Process()) { process.StartInfo.FileName = target; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; var output = new StringBuilder(); var error = new StringBuilder(); using (var outputWaitHandle = new AutoResetEvent(false)) using (var errorWaitHandle = new AutoResetEvent(false)) { process.OutputDataReceived += (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) { errorWaitHandle.Set(); } else { if (e.Data.Contains("ID")) { var index = e.Data.IndexOf("ID"); var temp = e.Data.Substring(index, e.Data.Length - index - 1); pID = temp.Substring(3, temp.Length - 3); var cp1 = new CompAndProcesses(); cp1.computer = client; cp1.processName = fileName; cp1.threadID = pID; CompAndProcesseses.Add(cp1); //Send a message to LabClients, if LabClients puts a respond message in the replyqueue. var path = "FormatName:Direct=OS:" + client.ComputerName.ToLower() + "\\private$\\requestqueue"; MessageQueue rmTmsq = new MessageQueue(path); System.Messaging.Message msg = new System.Messaging.Message("The purpose is to check whenever a process has been started"); MessageQueueTransaction trans = new MessageQueueTransaction(); try { trans.Begin(); rmTmsq.Send(msg, pID, trans); trans.Commit(); } catch (Exception ee) { throw ee; } finally { trans.Dispose(); } } error.AppendLine(e.Data); } }; try { process.Start(); } catch (Exception e) { } process.BeginOutputReadLine(); process.BeginErrorReadLine(); var timeout = 15000; if (process.WaitForExit(timeout) && outputWaitHandle.WaitOne(timeout) && errorWaitHandle.WaitOne(timeout)) { //Could type some Process.Exist code here but it aint needed. Debug.Write(output); Debug.Write("ERROR:" + error); } } } }
public void ProjectFilesAndRunIt(LabClient client, string appConfigName, string appName) { //-----local copy var copyPath = Path.Combine(TempPath, "localCopy.bat"); using (var file = new StreamWriter(copyPath)) { file.WriteLine("@echo off"); var srcDir = Path.GetDirectoryName(appConfigName); var dstDir = Path.Combine(Config.Sharednetworkdrive, appName); var line = @"xcopy """ + srcDir + @""" """ + dstDir + @""" /V /E /Y /Q /I"; file.WriteLine(line); } _service.StartNewCmdThread(copyPath); //-----end //---onecall to client: copy and run var copyPathRemote = Path.Combine(TempPath, "remoteCopyRun" + client.ComputerName + ".bat"); using (var file = new StreamWriter(copyPathRemote)) { file.WriteLine("@echo off"); var line = @"cmdkey.exe /add:" + client.ComputerName + @" /user:"******" /pass:"******"xcopy """ + srcDir + @""" """ + dstDir + @""" /V /E /Y /Q /I"; var runLocation = Path.Combine(dstDir, Path.GetFileName(appConfigName)); var runCmd = @"start """" """ + runLocation + @""""; line = Config.PsTools + @"\PsExec.exe -d -i 1 \\" + client.ComputerName + @" -u " + _service.Credentials.DomainSlashUser + @" -p " + _service.Credentials.Password + @" cmd /c (" + copyCmd + @" ^& " + runCmd + @")"; file.WriteLine(line); } _service.StartNewCmdThread(copyPathRemote); }
/// <summary> /// Reads the clients.txt into the program for a list of computers in a specific lab. /// </summary> /// <returns>List of clients</returns> public List<LabClient> GetLabComputersFromStorage() { using (var file = new StreamReader("clients.txt")) { clientlist = new List<LabClient>(); int roomNo; int boothNo; string line; string mac; string compname; while (((line = file.ReadLine()) != null)) { if (!string.IsNullOrEmpty(line)) { var data = line.Split(null); roomNo = int.Parse(data[0]); boothNo = int.Parse(data[1]); compname = data[2]; mac = data[3]; var client = new LabClient(roomNo, compname, boothNo, mac); clientlist.Add(client); } } } return clientlist; }