Esempio n. 1
0
        public static String DeleteOldestSSH(SSHInput SSHInput)
        {
            String result = "";

            using (var client = new SshClient(SSHInput.HostName, SSHInput.UserName, SSHInput.Password))
            {
                client.Connect();
                switch (SSHInput.SSHBashType)
                {
                case SSHBashType.WindowsCMD:
                    SshCommand winCommand           = client.RunCommand("dir " + SSHInput.PathToDirectory + " /O:D");
                    String     startSubString       = winCommand.Result.Substring(winCommand.Result.IndexOf("Directory of"));
                    String     firstFileEntryString = startSubString.Substring(startSubString.IndexOf('\n')).TrimStart('\n').TrimStart('\r').TrimStart('\n');
                    String[]   resultFilesArray     = firstFileEntryString.Split('\n');
                    result = resultFilesArray[0].Substring(36).TrimEnd();
                    SshCommand winCommandDelete = client.RunCommand(@"del """ + SSHInput.PathToDirectory + result + @"""");
                    break;

                case SSHBashType.LinuxBash:
                    SshCommand linuxCommand = client.RunCommand("find " + SSHInput.PathToDirectory + " -type f -printf '%T+ %p\n' | sort | head -n 1 ");
                    result = linuxCommand.Result.Substring(linuxCommand.Result.IndexOf("/")).TrimEnd();
                    SshCommand linuxCommandDelete = client.RunCommand("rm " + SSHInput.PathToDirectory + "/" + result);
                    break;
                }
                client.Disconnect();
            }
            return(result);
        }
Esempio n. 2
0
        public static JToken GetDirectoryInfoSSH(SSHInput SSHInput)
        {
            JObject returnObject = new JObject();
            double  result       = 0.00;
            double  freeSpace    = 0.00;
            double  totalSpace   = 0.00;

            using (var client = new SshClient(SSHInput.HostName, SSHInput.UserName, SSHInput.Password))
            {
                client.Connect();
                switch (SSHInput.SSHBashType)
                {
                case SSHBashType.LinuxBash:
                    SshCommand      linuxCommand   = client.RunCommand("df " + SSHInput.PathToDirectory);
                    String          splitResult    = linuxCommand.Result.TrimEnd('\n').Split('\n')[1];
                    Regex           rgx            = new Regex(@"\s(\d+%)");
                    MatchCollection matches        = rgx.Matches(splitResult);
                    double          availableSpace = 1 - (Convert.ToDouble(matches[0].Value.TrimStart().TrimEnd('%')) / 100);
                    result = availableSpace;
                    break;

                case SSHBashType.WindowsCMD:
                    SshCommand winCommand   = client.RunCommand("fsutil volume diskfree " + SSHInput.PathToDirectory);
                    String[]   winResult    = winCommand.Result.TrimEnd('\n').Split('\n');
                    String[]   regexResults = new string[3];

                    for (int i = 0; i < winResult.Length; i++)
                    {
                        Regex           rgx2     = new Regex(@"\s:\s(\d+)");
                        MatchCollection matches2 = rgx2.Matches(winResult[i]);
                        regexResults[i] = matches2[0].Value.TrimStart().TrimStart(':').TrimStart();
                    }

                    totalSpace = Math.Truncate(((Convert.ToDouble(regexResults[1])) / 1024 / 1024) * 100) / 100;
                    result     = Math.Truncate((Convert.ToDouble(regexResults[2]) / Convert.ToDouble(regexResults[1])) * 100) / 100;
                    freeSpace  = Math.Truncate(((Convert.ToDouble(regexResults[2]) / 1024 / 1024)) * 100) / 100;
                    break;
                }
                client.Disconnect();
            }

            returnObject.Add("FreeSpacePercentage", result);
            returnObject.Add("FreeSpaceMB", freeSpace);
            returnObject.Add("TotalSpaceMB", totalSpace);

            return(returnObject);
        }