public bool CheckPassswd() { string host = "c1t19440.itcs.hpicorp.net"; string svcAccount = "acostadu"; string passwd = "cat.bot-0"; string userAffected = "hrsamba"; string OS = ""; string cmdUpdatePasswd = "passwd -n 7 -x 30 " + userAffected; bool exit = false; SshConnection con = new SshConnection(host, svcAccount, passwd); try { SshClient sshCon = con.connect(); //Based on the OS will //Run the commmand to check if passwd expired and extends 30 days if necessary OS = sshCon.RunCommand("uname").Result; int diffDays = 0; int extendedDays = 0; if (OS == "Linux\n") { string startDate = sshCon.RunCommand("passwd -S " + userAffected + " | awk '{print $3}'").Result; DateTime startD = DateTime.Parse(startDate); if (startDate != "\n") { string currentDate = sshCon.RunCommand("date +%Y/%m/%d").Result; DateTime currentD = DateTime.Parse(currentDate); diffDays = Convert.ToInt32((currentD - startD).TotalDays); extendedDays = Convert.ToInt32(sshCon.RunCommand("passwd -S " + userAffected + " | awk '{print $5}' ").Result); //Change passwd if current date is newer than last passwd change //the diff is bigger than extended days and extendedDays is different than 30 if (diffDays > 0 && extendedDays < diffDays && extendedDays != 30) { sshCon.RunCommand(cmdUpdatePasswd); return(true); } } } if (OS == "HP-UX\n") { string startDate = sshCon.RunCommand("passwd -s " + userAffected + "| awk '{print $3}';").Result; DateTime startD = DateTime.Parse(startDate); if (startDate != "\n") { string currentDate = sshCon.RunCommand("date +%d/%m/%Y").Result; DateTime currentD = DateTime.Parse(currentDate); diffDays = Convert.ToInt32((currentD - startD).TotalDays); extendedDays = Convert.ToInt32(sshCon.RunCommand("passwd -S " + userAffected + " | awk '{print $5}' ").Result); //Change passwd if current date is newer than last passwd change //the diff is bigger than extended days and extendedDays is different than 30 if (diffDays > 0 && extendedDays < diffDays && extendedDays != 30) { sshCon.RunCommand(cmdUpdatePasswd); return(true); } } } } catch (Exception e) { } return(exit); }
public bool CheckDuplicate(Ticket ticket) { string host = ""; string path = ""; string fullFileName = ""; string fileNameWithoutExtension = ""; string extension = ""; string cmdRename = ""; string cmdCheck = ""; // ticket.RootText e.g. of content "File already exists: /opt/cloudhost/intbroker/integrations/archive/incoming/BI/FieldGlass-TimeSheet-20170309.csv already exists on the remote filesystem" int posPath = ticket.RootText.LastIndexOf("/"); if (posPath >= 0) { path = ticket.RootText.Substring(0, posPath); } // path e.g. of content "File already exists: /opt/cloudhost/intbroker/integrations/archive/incoming/BI" path = path.Substring(path.IndexOf("/"), path.Length - path.IndexOf("/")); // path e.g. of content "/opt/cloudhost/intbroker/integrations/archive/incoming/BI" int x = 0; //exit=true represents a rename applied and will check over the list of affected servers if multiple ( like a cluster ) while (exit != true && x < ticket.HostArray.Length) { host = ticket.HostArray[x]; fullFileName = ticket.RootText.Substring(ticket.RootText.LastIndexOf("/"), ticket.RootText.Length - ticket.RootText.LastIndexOf("/")); fullFileName = fullFileName.Substring(0, fullFileName.IndexOf(" ")); // fullFileName e.g. of content "/FieldGlass-TimeSheet-20170309.csv" //Saves extension of the file if it has to be used later while renaming int filePos = fullFileName.IndexOf("."); if (filePos >= 0) { fileNameWithoutExtension = fullFileName.Substring(0, filePos); } // fileNameWithoutExtension e.g. of content "/FieldGlass-TimeSheet-20170309" if (filePos != -1) { extension = fullFileName.Substring(filePos, fullFileName.Length - filePos); } // extension e.g. of content ".csv" //LINUX CONNECTION try { SshConnection con = new SshConnection(host, user, passwd); SshClient sshCon = con.connect(); if (path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); } string OS = sshCon.RunCommand("uname").Result; if (OS == "Linux\n") { //Run the commmand to check if file exists and rename if necessary cmdCheck = "/opt/pb/bin/pbrun bash -c 'ls -l " + path + fullFileName + ";'"; var command = sshCon.RunCommand(cmdCheck); if (command.ExitStatus == 0) { cmdRename = "/opt/pb/bin/pbrun bash -c 'cd " + path + ";" + "mv " + path + fullFileName + " " + path + fileNameWithoutExtension + "_$(date +%s)" + extension + ";'"; if (sshCon.RunCommand(cmdRename).ExitStatus == 0) { exit = true; return(exit); } } } if (OS == "HP-UX\n") { //Run the commmand to check if file exists and rename if necessary cmdCheck = "/opt/pb/bin/pbrun ksh -c 'ls -l " + path + fullFileName + ";'"; var command = sshCon.RunCommand(cmdCheck); if (command.ExitStatus == 0) { cmdRename = "/opt/pb/bin/pbrun ksh -c 'cd " + path + ";" + "mv " + path + fullFileName + " " + path + fileNameWithoutExtension + "_$(date +%s)" + extension + ";'"; if (sshCon.RunCommand(cmdRename).ExitStatus == 0) { exit = true; } } } } catch (Exception e) { } //WINDOWS CONNECTION try { string DomainSvcAccount = ""; string winUserSvcAccount = ""; string winPasswdSvcAccount = ""; ImpersonationHelper.Impersonate(DomainSvcAccount, winUserSvcAccount, winPasswdSvcAccount, delegate { //Your code here //If connection was done if (File.Exists(@"\\\iten\hpsb.txt")) { string origin = "\\" + host + path.Replace(@"/", @"\") + fileNameWithoutExtension.Replace(@"/", @"\"); string destination = "\\" + host + path.Replace(@"/", @"\") + fileNameWithoutExtension.Replace(@"/", @"\") + "1"; File.Move(@"\\hc4w00433\iten\hpsb.txt", @"\\hc4w00433\iten\hpsb.txt1"); exit = true; } }); } catch (Exception e) { } x++; } //Exit contains a vaule True if ticket was SOLVED return(exit); }