1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379:
| using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices;
namespace ConsoleApplication1 { public class MyVars { public string inifile;
[DllImport("kernel32")] public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public static string GetString(string section, string key)
{ string inifile = Environment.CurrentDirectory + "\\settings.INI"; StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(section, key, "", temp, 255, inifile); return temp.ToString(); }
[DllImport("kernel32.dll")] public static extern Boolean SetConsoleCtrlHandler(HandlerRoutine Handler, Boolean Add);
public delegate Boolean HandlerRoutine(CtrlTypes CtrlType);
public enum CtrlTypes { CTRL_C_EVENT = 0, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT = 5, CTRL_SHUTDOWN_EVENT }
[DllImport("kernel32.dll")] public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId); public enum ConsoleCtrlEvent { CTRL_C = 0, CTRL_BREAK = 1, CTRL_CLOSE = 2, CTRL_LOGOFF = 5, CTRL_SHUTDOWN = 6 }
public static volatile bool SENDING_CTRL_C_TO_CHILD = false; public static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) { e.Cancel = SENDING_CTRL_C_TO_CHILD; } } class program { public static int countgetinput; public static int countwhile = 0; public static string returned1; private static StreamReader file = null; private static StreamWriter log = null; private static FileInfo ifile = null; private static Process myProc = null; private static FileInfo ilogfile = null; private static FileStream fs = null;
private static string Logfolder = "C:\\output\\Portping\\logs"; private static string stdLogfile = Logfolder + "\\log.txt"; private static string AppWorkingDir = "C:\\output\\PortPing"; private static string App = "C:\\output\\PortPing\\PortPing.exe"; private static string AppArg = "127.0.0.1 3389"; private static string Inputfile = AppWorkingDir + "\\input.txt";
private static int logsizeinhours = 3;
public static void myProc_ErrorDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine(e.Data); } public static void myProc_OutputDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine(e.Data); if (ilogfile == null) {ilogfile = new FileInfo(stdLogfile); } if (!File.Exists(stdLogfile)) { fs = ilogfile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Close(); } fs = ilogfile.Open(FileMode.Append, FileAccess.Write, FileShare.ReadWrite); log = new StreamWriter(fs); log.WriteLine(e.Data); log.Close(); fs.Close(); string datum = DateTime.Now.Day + "_" + DateTime.Now.Month + "_" + DateTime.Now.Year; string filedate = DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second; if (ilogfile.CreationTime < DateTime.Now.AddHours(-logsizeinhours)) {if (!File.Exists(Logfolder + "\\" + filedate + ".txt")) { if (File.Exists(stdLogfile)) { ilogfile.CreationTime = DateTime.Now; System.IO.File.Move(stdLogfile, Logfolder + "\\" + filedate + ".txt"); } } } }
public static string readcommands(int counter) { string line = ""; if (File.Exists(Inputfile)) { try { if (file != null) { file.Close(); } System.Threading.Thread.Sleep(1000); ifile = new FileInfo(Inputfile); FileStream fs = ifile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); file = new StreamReader(fs); while (counter.Equals(0)) { line = file.ReadLine(); counter++; } file.Close(); fs.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine("Exception"); } } else { try { FileInfo wfi = new FileInfo(Inputfile); FileStream fs = wfi.Create(); fs.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return line; } public static void Main() { if (File.Exists(Inputfile)) { System.IO.File.Delete(Inputfile); } Console.CancelKeyPress += new ConsoleCancelEventHandler(MyVars.Console_CancelKeyPress); if (File.Exists(Environment.CurrentDirectory + "\\settings.INI")) { readconfigfile(); } Console.WriteLine("readconfigfile from" + Environment.CurrentDirectory + "\\settings.INI done"); startprog();
} public static void startprog() { Console.WriteLine(Logfolder); Console.WriteLine(AppWorkingDir); Console.WriteLine(stdLogfile); Console.WriteLine(App); Console.WriteLine(AppArg); Console.WriteLine(Inputfile); Console.WriteLine(logsizeinhours); string command = ""; Console.WriteLine("process creation"); ProcessStartInfo P = new ProcessStartInfo(); Console.WriteLine("set filename"); P.FileName = App; P.Arguments = AppArg; Console.WriteLine("create logfolder"); if (!Directory.Exists(Logfolder)) {Directory.CreateDirectory(Logfolder); } Console.WriteLine("create appfolder"); if (!Directory.Exists(AppWorkingDir)) {Directory.CreateDirectory(AppWorkingDir); } Console.WriteLine("set appfolder"); P.WorkingDirectory = AppWorkingDir; P.UseShellExecute = false; Console.WriteLine("set redirects for process"); P.RedirectStandardOutput = true; P.RedirectStandardInput = true; P.RedirectStandardError = true; Console.WriteLine("start process"); Process myProc = Process.Start(P); Console.WriteLine(myProc.ProcessName + " " + myProc.Id); Console.WriteLine("Begin Outputread"); myProc.BeginOutputReadLine();
Console.WriteLine("EventHandler for data output"); myProc.OutputDataReceived += new DataReceivedEventHandler(myProc_OutputDataReceived); myProc.ErrorDataReceived += new DataReceivedEventHandler(myProc_ErrorDataReceived); Console.WriteLine("start while"); while (command != "exit") { countwhile++; Console.WriteLine(myProc.StandardError); command = getinput(1); myProc.StandardInput.WriteLine(command); if (command == "exit") { Console.WriteLine(command); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); MyVars.SENDING_CTRL_C_TO_CHILD = true; Console.WriteLine(MyVars.GenerateConsoleCtrlEvent(MyVars.ConsoleCtrlEvent.CTRL_C, myProc.Id)); Console.WriteLine(MyVars.GenerateConsoleCtrlEvent(MyVars.ConsoleCtrlEvent.CTRL_C, myProc.Id)); myProc.StandardInput.WriteLine("\x3"); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); myProc.StandardInput.WriteLine("\x03"); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); myProc.StandardInput.WriteLine("\0x03"); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); myProc.StandardInput.WriteLine(0x03); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); Console.WriteLine("\x3"); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); Console.WriteLine("\x03"); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); Console.WriteLine("\0x03"); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); Console.WriteLine("^C"); myProc.CloseMainWindow(); Console.WriteLine(0x03); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); Console.WriteLine("PID:" + myProc.Id); MyVars.SENDING_CTRL_C_TO_CHILD = false; System.Threading.Thread.Sleep(5000); Console.WriteLine("{0} is active: {1}", myProc.Id, !myProc.HasExited); myProc.StandardInput.Close(); myProc.Close(); }
} }
public static int getprog(string procsearch) { string procid; string procname; string procpathfile; int procreturn; procreturn=999; procid = ""; Process[] pp = Process.GetProcesses(); foreach (Process p in pp) { procname = Convert.ToString(p.ProcessName); if (procname.Equals(procsearch)) { procid = Convert.ToString(p.Id); procreturn = p.Id; procpathfile = Convert.ToString(p.MainModule.FileName); Console.WriteLine("Process ID = " + procid + " Processname: " + procname + " Abbildname = " + procpathfile); p.CloseMainWindow();
} } return procreturn;
}
public static string getinput(int countgetintput) { returned1 = null; do { returned1 = readcommands(0); if ((returned1 != "") && (returned1 != null)) { countgetinput = -1; } countgetinput = countgetinput + 1; } while (countgetinput !=0 );
try { if (returned1 != null) { System.IO.File.Delete(Inputfile); } } catch (Exception) { } return returned1;
}
static int readconfigfile() { Logfolder = MyVars.GetString("Option", "Logfolder"); stdLogfile = Logfolder + MyVars.GetString("Option", "stdLogfile"); AppWorkingDir = MyVars.GetString("Option", "AppWorkingDir"); App = MyVars.GetString("Option", "App"); Inputfile = AppWorkingDir + MyVars.GetString("Option", "Inputfile"); AppArg = MyVars.GetString("Option", "AppArg"); logsizeinhours = Convert.ToInt32(MyVars.GetString("Option", "logsizeinhours")); return 0; } } } |