public static List <CPUInfo> GetCurrentCPUPerc(Renci.SshNet.SshClient sshClient, int delayMS = 1000) { List <CPUInfo> cpuDiffs = new List <CPUInfo>(); List <CPUInfo> cpus1 = FromProcStat(sshClient.RunCommand("cat /proc/stat").Result); TH.Thread.Sleep(delayMS); List <CPUInfo> cpus2 = FromProcStat(sshClient.RunCommand("cat /proc/stat").Result); foreach (CPUInfo c1 in cpus1) { CPUInfo c2 = cpus2.FirstOrDefault(c => c.Name == c1.Name); if (c2 != null) { CPUInfo cpuUsageDiff = new CPUInfo(); cpuUsageDiff.Name = c1.Name; cpuUsageDiff.IsTotalCPU = c1.IsTotalCPU; cpuUsageDiff.User = c2.User - c1.User; cpuUsageDiff.Nice = c2.Nice - c1.Nice; cpuUsageDiff.System = c2.System - c1.System; cpuUsageDiff.Idle = c2.Idle - c1.Idle; cpuUsageDiff.IOWait = c2.IOWait - c1.IOWait; cpuUsageDiff.IRQ = c2.IRQ - c1.IRQ; cpuUsageDiff.SoftIRQ = c2.SoftIRQ - c1.SoftIRQ; cpuDiffs.Add(cpuUsageDiff); } } return(cpuDiffs); }
public static List <NicInfo> GetCurrentNicStats(Renci.SshNet.SshClient sshClient, int delayMS = 1000) { List <NicInfo> nicDiffs = new List <NicInfo>(); List <NicInfo> nics1 = FromIfconfig(sshClient.RunCommand("ifconfig").Result); TH.Thread.Sleep(delayMS); List <NicInfo> nics2 = FromIfconfig(sshClient.RunCommand("ifconfig").Result); foreach (NicInfo c1 in nics1) { NicInfo c2 = nics2.FirstOrDefault(c => c.Name == c1.Name); if (c2 != null) { NicInfo nicUsageDiff = new NicInfo(); nicUsageDiff.MeasurementDelayMS = delayMS; nicUsageDiff.Name = c1.Name; nicUsageDiff.LocalLoopback = c1.LocalLoopback; nicUsageDiff.IpV4 = c1.IpV4; nicUsageDiff.IpV6 = c1.IpV6; nicUsageDiff.HWAddress = c1.HWAddress; nicUsageDiff.RxBytes = c2.RxBytes - c1.RxBytes; nicUsageDiff.TxBytes = c2.TxBytes - c1.TxBytes; nicDiffs.Add(nicUsageDiff); } } return(nicDiffs); }
private void cmdTest_Click(object sender, EventArgs e) { try { string machineName = ApplyConfigVarsOnField(txtMachineName.Text); string userName = ApplyConfigVarsOnField(txtUsername.Text); string password = ApplyConfigVarsOnField(txtPassword.Text); string privateKeyFile = ApplyConfigVarsOnField(txtPrivateKeyFile.Text); string passPhrase = ApplyConfigVarsOnField(txtPassPhrase.Text); using (Renci.SshNet.SshClient sshClient = SshClientTools.GetSSHConnection( optPrivateKey.Checked ? SSHSecurityOption.PrivateKey : optPassword.Checked?SSHSecurityOption.Password: SSHSecurityOption.KeyboardInteractive, machineName, (int)sshPortNumericUpDown.Value, userName, password, privateKeyFile, passPhrase)) { if (sshClient.IsConnected) { MessageBox.Show(string.Format("Success\r\n{0}", sshClient.RunCommand("cat /proc/version").Result), "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } sshClient.Disconnect(); } } catch (Exception ex) { MessageBox.Show(string.Format("Fail!\r\n{0}", ex.Message), "Test", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// runs multiple commands on the server public bool runCommands(string[] commands) { Renci.SshNet.SshCommand sshCommand; Renci.SshNet.SshClient sshClient = new Renci.SshNet.SshClient(this.ipAddress, 8888, this.username, this.password); sshClient.Connect(); foreach (string s in commands) { sshCommand = sshClient.RunCommand(s); Thread.Sleep(100); } sshClient.Disconnect(); return(true); }
public static List <DiskIOInfo> GetCurrentDiskStats(Renci.SshNet.SshClient sshClient, int delayMS = 1000) { List <DiskIOInfo> diskDiffs = new List <DiskIOInfo>(); List <DiskIOInfo> disks1 = FromProcDiskStats(sshClient.RunCommand("cat /proc/diskstats").Result); TH.Thread.Sleep(delayMS); List <DiskIOInfo> disks2 = FromProcDiskStats(sshClient.RunCommand("cat /proc/diskstats").Result); foreach (DiskIOInfo c1 in disks1) { DiskIOInfo c2 = disks2.FirstOrDefault(c => c.Name == c1.Name); if (c2 != null) { DiskIOInfo diskUsageDiff = new DiskIOInfo(); diskUsageDiff.MeasurementDelayMS = delayMS; diskUsageDiff.MajorNumber = c1.MajorNumber; diskUsageDiff.MinorNumber = c1.MinorNumber; diskUsageDiff.Name = c1.Name; diskUsageDiff.ReadsCompleted = c2.ReadsCompleted - c1.ReadsCompleted; diskUsageDiff.ReadsMerged = c2.ReadsMerged - c1.ReadsMerged; diskUsageDiff.SectorsRead = c2.SectorsRead - c1.SectorsRead; diskUsageDiff.TimeSpentReadingMS = c2.TimeSpentReadingMS - c1.TimeSpentReadingMS; diskUsageDiff.WritesCompleted = c2.WritesCompleted - c1.WritesCompleted; diskUsageDiff.WritesMerged = c2.WritesMerged - c1.WritesMerged; diskUsageDiff.SectorsWritten = c2.SectorsWritten - c1.SectorsWritten; diskUsageDiff.TimeSpentWritingMS = c2.TimeSpentWritingMS - c1.TimeSpentWritingMS; diskUsageDiff.IOsInProgress = c2.IOsInProgress - c1.IOsInProgress; diskUsageDiff.TimeDoingIOsMS = c2.TimeDoingIOsMS - c1.TimeDoingIOsMS; diskUsageDiff.WeightedTimeDoingIOsMS = c2.WeightedTimeDoingIOsMS - c1.WeightedTimeDoingIOsMS; diskDiffs.Add(diskUsageDiff); } } return(diskDiffs); }
private string RunCommand(Renci.SshNet.SshClient ssh, string line) { var resultStr = string.Empty; var cmd = ssh.RunCommand(line); if (!string.IsNullOrWhiteSpace(cmd.Error)) { resultStr += cmd.Error; } else { resultStr += cmd.Result; } return(resultStr); }
bool IProfileItem.Stop() { using (Renci.SshNet.SshClient client = new Renci.SshNet.SshClient(Value, Username, AES.DecryptString(Password))) { try { client.Connect(); var cmd = client.RunCommand(string.Format("{0}stop.sh", Path)); OnLog(new LogEventArgs(string.Format("Attempting to stop all serivices on {0}", Name))); client.Disconnect(); } catch (SocketException) { OnLog(new LogEventArgs(string.Format("Unable to connect to server {0}", Value))); } } return(true); }
public string ExecuteCommand() { string output = ""; try { Renci.SshNet.SshClient sshClient = SSHConnection.GetConnection(); //using (Renci.SshNet.SshClient sshClient = SshClientTools.GetSSHConnection(SSHConnection)) { try { //if (sshClient.IsConnected) { output = sshClient.RunCommand(CommandString).Result; } SSHConnection.CloseConnection(); if (ValueReturnType == SSHCommandValueReturnType.LineCount) { int lines = output.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Length; output = lines.ToString(); } else if (ValueReturnType == SSHCommandValueReturnType.TextLength) { int length = output.Length; output = length.ToString(); } } catch (Exception ex) { output = string.Format("The Command '{0}' failed to execute!\r\n{1}", CommandString, ex.ToString()); } } } catch (Exception ex) { throw new Exception(string.Format("Connection failed to '{0}' : {1}", SSHConnection.ComputerName, ex.Message)); } return(output); }
public static List <DiskIOInfo> FromProcDiskStats(Renci.SshNet.SshClient sshClient) { return(FromProcDiskStats(sshClient.RunCommand("cat /proc/diskstats").Result)); }
public static List <DiskInfo> FromDfTk(Renci.SshNet.SshClient sshClient) { return(FromDfTk(sshClient.RunCommand("df -Tk").Result)); }
public static MemInfo FromCatProcMeminfo(Renci.SshNet.SshClient sshClient) { return(FromCatProcMeminfo(sshClient.RunCommand("cat /proc/meminfo").Result)); }