public static void UpdateDBField(string ip, string field, string value) { string updateQuery = @"UPDATE [Servers] SET " + field + " = @value WHERE IpAddress = @ip"; MorpheusController m = new MorpheusController();//so we can add log entries using (var context = new ServerContext()) { string currentValue = context.Database.SqlQuery <string>("Select " + field + " from Servers where IpAddress=" + "'" + ip + "'").FirstOrDefault <string>(); Console.WriteLine(currentValue); if (currentValue == value) { return; } } try { ServerContext db = new ServerContext();//instantiates a database context object db.Database.ExecuteSqlCommand( updateQuery, new SqlParameter("@value", value), new SqlParameter("@ip", ip)); } catch { //Output to the console Trace.WriteLine("ERROR: Server is offline: cannot retrieve active users for ip " + ip + " at this time."); m.logger.Error("ERROR: Server is offline: cannot retrieve active users for ip " + ip + " at this time."); } }
public ActionResult ForceActivate(string ipAddress, int choice) { string result = IsForceOn(ipAddress); if (choice == 1 && result.Equals("OFF"))//cover scenerio where force was manually turned on via server, after the Hydra gui was loaded when force was off { StartForce(ipAddress); string subject = "Force Activated on " + ipAddress; string body = "The force has been turned on by " + User.Identity.GetUserName() + " on " + ipAddress; string emailTo = "*****@*****.**"; MorpheusController.SendEmail(subject, body, emailTo); } else if (choice == 2 && result.Equals("ON")) { StopForce(ipAddress); string subject = "Force turned OFF on " + ipAddress; string body = "The force has been turned OFF by " + User.Identity.GetUserName() + " on " + ipAddress; string emailTo = "*****@*****.**"; MorpheusController.SendEmail(subject, body, emailTo); } else { Trace.WriteLine("ERROR: invalid choice parameter sent to ForceActivate() in ForceController - FORCE is already ON or OFF"); //string subject = "FORCE ERROR on " + ipAddress; //string body = "An attempt to turn the force on or off was made by " + User.Identity.GetUserName() + // " on " + ipAddress + "- The force may have already been activated or deactivated directly on the server." + // " Contact one of the following active users: " + MorpheusController.GetActiveUsers(ipAddress); //string emailTo = User.Identity.GetUserName() + "@playags.com"; //MorpheusController.SendEmail(subject, body, emailTo); forceInStateAlready = true; } return(View()); }
//Find all users Active and Disc on the server public static string ImpactedUsers(string ip) { List <string> UserList = new List <string>(); //Run Script to Query Users MorpheusController.CreateBatchFile("C:\\PSTools\\PSExec.exe \\\\%1 query user /server:%1", scriptPath + @"GetUsers.bat"); string cmdOutput = MorpheusController.RunBatchFile(scriptPath + @"GetUsers.bat", ip); string[] lines = cmdOutput.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); string usersFormatted = ""; //Go through each line and get the username line for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("Active") || lines[i].Contains("Disc")) { string[] activeUser = lines[i].Split(null); usersFormatted += activeUser[1] + ","; } } //If there are users, format it name,name,name. if (usersFormatted.Length > 0) { return(usersFormatted.Remove(usersFormatted.Length - 1, 1) + "."); } //else return none if there are no users. else { return("None."); } }
//Stop force - Disable-FOLGS public static string StopForce(string ip) { KillALLFO(ip); //kill notepads MorpheusController.CreateBatchFile("C:\\PSTools\\psexec.exe \\\\" + ip + " -s -i \"C:\\" + forcePath + "Disable-FOLGS.bat", scriptPath + @"FORCE-OFF.bat"); MorpheusController.RunBatchFile(scriptPath + @"FORCE-OFF.bat", ip); return(""); }
public static bool KillALLFO(string ip) { //Create script to kill notepads MorpheusController.CreateBatchFile("taskkill /s %1 /im \"notepad.exe\" /f", scriptPath + @"KillAllFO.bat"); //Run it MorpheusController.RunBatchFile(scriptPath + @"KillAllFO.bat", ip); return(true); }
public static string EchoIntoFO(List <string> pattern, string ip) { MorpheusController.CreateBatchFile("C:\\PSTools\\psexec.exe \\\\%1 cmd /c \"echo %2 > C:\\MTS\\Plugh\\FO.txt\"", scriptPath + @"FORCE-ON-create.bat"); MorpheusController.CreateBatchFile("C:\\PSTools\\psexec.exe \\\\%1 cmd /c \"echo %2 >> C:\\MTS\\Plugh\\FO.txt\"", scriptPath + @"FORCE-ON-append.bat"); if (pattern.Count > 0) { BatchWithEcho(ip, @"FORCE-ON-create.bat", pattern[0]); } if (pattern.Count > 1) { for (int i = 1; i < pattern.Count; i++) { BatchWithEcho(ip, @"FORCE-ON-append.bat", pattern[i]); } } return(""); }
public static string AppendToHostsFileOnServer(string ip) { //Create batch file to update hosts file MorpheusController.CreateBatchFile("find /c \"Secure-Service\" C:\\windows\\system32\\drivers\\etc\\hosts || (echo. >> C:\\windows\\system32\\drivers\\etc\\hosts && echo 10.0.32.32 Secure-Service >> C:\\windows\\system32\\drivers\\etc\\hosts)", scriptPath + @"hostsFileAppend.bat"); //Create batch file to run hosts file on server machine MorpheusController.CreateBatchFile("C:\\PSTools\\psexec.exe \\\\%1 C:\\" + forcePath + "hostsFileAppend.bat", scriptPath + @"Run-hostsFileAppend.bat"); //Copy batch script to remote server string copyFrom = scriptPath + @"hostsFileAppend.bat"; string copyTo = @"\\" + ip + @"\C$\" + forcePath + "hostsFileAppend.bat"; System.IO.File.Copy(copyFrom, copyTo, true); //Run batch script on remote server BatchWithEcho(ip, @"Run-hostsFileAppend.bat", ""); return(""); }
//We need to know what PID's have been assigned to WAP, because they can NOT be used for Local Area progressives. //This data can be found in the Bingo database, progressives table. //Progressive Type 102 is WAP (100 = local, 101 is Turboboost) //select Bingo.dbo.Progressives.GameOutcomeID from Bingo.dbo.Progressives where Bingo.dbo.Progressives.ProgressiveTypeLookupID = 102; public static string GetMultisitePIDsFromServer(string ip) { string pids = ""; MorpheusController.CreateBatchFile(@"sqlcmd -S " + ip + @" -d Bingo -E -o " + scriptPath + @"WAPPIDresult.txt -Q ""SELECT GameOutcomeID FROM Progressives WHERE ProgressiveTypeLookupID = 102"" ", scriptPath + @"GetWAPPids.bat"); //Run the batch file try { MorpheusController.RunBatchFile(scriptPath + @"GetWAPPids.bat", ip); } catch (Exception ex) { Trace.WriteLine(ex.StackTrace.ToString()); } //Parse the cmd output and return pids, comma delimited //Read WAPPIDresult.txt into a variable string[] lines = System.IO.File.ReadAllLines(scriptPath + @"WAPPIDresult.txt"); //Add desired indexes to the string int index = 0; foreach (string value in lines) { if (index > 1 && index < lines.Length - 2) { //pidLis.Add(value); //add to the string pids += value + ", "; } index++; } //delete last comma if (!pids.Equals("")) { pids = pids.Remove(pids.LastIndexOf(',')); return(pids); } else { pids = " - ERROR connecting to this server: WAP PID query failed as a result."; return(pids); } }
//Start the force - Enable-FOLGS batch file //ISSUE: The LGS cmd window doesnt stay open, which causes LGS service to 'exit' //It works fine when running public static string StartForce(string ip) { if (ip != null) { //Kill notepads KillALLFO(ip); //Copy force files over to server CopyForceFilesToServer(ip); //give host file access to mothership AppendToHostsFileOnServer(ip); //string serverPath = @"C:\hydra\HydraLive\trunk\Scripts\";//Location where the start service bat will end up //-s -i flags run as a system user and interactive window, so the LGS window doesn't close unexpectedly MorpheusController.CreateBatchFile("C:\\PSTools\\psexec.exe \\\\" + ip + " -s -i \"C:\\" + forcePath + "Enable-FOLGS.bat", scriptPath + @"FORCE-ON.bat"); MorpheusController.RunBatchFile(scriptPath + @"FORCE-ON.bat", ip); //Need to manually run NET START EGSSERVICES MorpheusController.CreateBatchFile("NET START EGSSERVICES", scriptPath + @"START-SERVICES.bat"); //Copy the bat file to server CopyStartServiceToServer(ip); //Run the file MorpheusController.RunBatchFile(scriptPath + @"START-SERVICES.bat", ip); } return(""); }
public static void GetPlayerVersionsOneIp(string ip) { string playerFolderName; string playerVersion; string playerRevision; string playerVersionRevision; string playerVersionFinal = ""; string pathCopyTo = scriptPath + @"cjplayer_CVD.xml"; MorpheusController m = new MorpheusController();//so we can add log entries try { //Declare the lists here so they clear out each time List <string> playerFolderNameList = new List <string>(); List <string> playerVersionsList = new List <string>(); string subFolderPath = @"\\" + ip + @"\C$\MTS\Player\CJ\CJOS64\3.3\"; //Get an array of directory names, within the 3.3 directory //TODO:check if all servers have a 3.3 directory, if this can be different, then handle that case foreach (string s in System.IO.Directory.GetDirectories(subFolderPath)) { playerFolderNameList.Add(s.Remove(0, subFolderPath.Length)); } foreach (string s in playerFolderNameList) { playerFolderName = s; //get the current player folder name string pathCopyFrom = @"\\" + ip + @"\C$\MTS\Player\CJ\CJOS64\3.3\" + playerFolderName + @"\CVD\cjplayer_CVD.xml"; //set path to copy from System.IO.File.Copy(pathCopyFrom, pathCopyTo, true); //copy the player xml file from server to this machine //Open xml file and save contents to a variable string readText = System.IO.File.ReadAllText(pathCopyTo); Trace.WriteLine("Contents of cjplayer_CVD.xml: " + readText); //Grab the player version playerVersion = GetBetween(readText, "<VERSION>V", "</VERSION>"); Trace.WriteLine("Contents of playerVersion: " + playerVersion); //Grab the player revision playerRevision = GetBetween(readText, "<REVISION>", "</REVISION>"); Trace.WriteLine("Contents of playerRevision: " + playerRevision); //Build the string that will be saved to a list of playerversions playerVersionRevision = playerVersion + ":" + playerRevision; Trace.WriteLine("Contents of playerVersion with Revision added, on server at ip: " + ip + " -> " + playerVersionRevision); //Add playerVersionRevision to a list playerVersionsList.Add(playerVersionRevision); } //Build a comma delimited string of player versions if (playerVersionsList.Count > 1)//if more than 1 player version exists in player versions list { foreach (string value in playerVersionsList) { playerVersionFinal += value + ", "; //15.3.11:98084 ,15.3.15:108209 ,15.4:110093 ,15.4.1:113486 } playerVersionFinal = playerVersionFinal.TrimEnd(' '); //get rid of extra spaces playerVersionFinal = playerVersionFinal.TrimEnd(','); //get rid of the last comma } else if (playerVersionsList.Count == 1) { foreach (string value in playerVersionsList) { playerVersionFinal += value; } } else { Trace.WriteLine("No players exist on ip " + ip); } //Save to database Trace.WriteLine("Saving playerVersionFinal to database..."); UpdateDBField(ip, "PlayerVersions", playerVersionFinal); playerVersionFinal = "";//clear variable contents } catch { Trace.WriteLine("BAM! There was a problem with locating the xml file on ip " + ip); m.logger.Error("BAM! There was a problem with locating the xml file on ip " + ip); } }
//Run cmd command query user /server:<ip> //Save result to a variable //Parse the string and grab names of active users public static string GetActiveUsers(string ip) { string cmdCommand = "query user /server:" + ip; string cmdOutput = ""; string activeUsers = ""; string updateQuery = @"UPDATE [Servers] SET ActiveUsers = @activeUsers WHERE IpAddress = @ip"; List <string> activeUserList = new List <string>(); MorpheusController m = new MorpheusController();//so we can add log entries //write and run .bat file. //System.IO.StreamWriter SW = new System.IO.StreamWriter("C:\\Users\\SHealy\\Documents\\Visual Studio 2013\\Projects\\Hydra-6-withEmail\\trunk\\QueryUsers.bat"); System.IO.StreamWriter SW = new System.IO.StreamWriter(scriptPath + @"QueryUsers.bat"); SW.WriteLine(@cmdCommand);//Add @ sign in front of string to prevent errors caused by unescaped backslash characters SW.Flush(); SW.Close(); SW.Dispose(); SW = null; // Start the child process. Process pr = new Process(); // Redirect the output stream of the child process pr.StartInfo.UseShellExecute = false; pr.StartInfo.RedirectStandardOutput = true; //pr.StartInfo.CreateNoWindow = true; pr.StartInfo.FileName = scriptPath + "QueryUsers.bat"; pr.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // Read the output stream first and then wait. cmdOutput = pr.StandardOutput.ReadToEnd(); pr.WaitForExit(); Trace.WriteLine("Users Query result: " + cmdOutput); //Parse the output for all active users //ParseActiveUsers will return a list of active user names activeUserList.AddRange(ParseActiveUsers(cmdOutput)); //Add each active user name to a string if (activeUserList.Count > 1)//if more than 1 name exists in active users list { foreach (string value in activeUserList) { activeUsers += value + ","; } activeUsers = activeUsers.TrimEnd(',');//get rid of the last comma } else if (activeUserList.Count == 1) { foreach (string value in activeUserList) { activeUsers += value; } } else { //do nothing, because there are no active users on the server } //Update the DB with active user names try { ServerContext db = new ServerContext();//instantiates a database context object db.Database.ExecuteSqlCommand( updateQuery, new SqlParameter("@activeUsers", activeUsers), new SqlParameter("@ip", ip)); } catch { //Output to the console Trace.WriteLine("ERROR: Server is offline: cannot retrieve active users for ip " + ip + " at this time."); m.logger.Error("ERROR: Server is offline: cannot retrieve active users for ip " + ip + " at this time."); } return(activeUsers); }