/// <summary> /// Login with root privileges /// </summary> /// <param name="sshProtocol"><see cref=" SSHProtocol"/></param> private static void LoginAsRoot(SSHProtocol sshProtocol) { sshProtocol.Send("su -"); string resultString = sshProtocol.Send("\n"); if (resultString.Contains("Password", StringComparison.CurrentCultureIgnoreCase)) { sshProtocol.Send(PASSWORD); sshProtocol.Send("\n"); } }
/// <summary> /// Execute command on Linux server /// </summary> /// <param name="address">Linux server IP Address</param> /// <param name="command">Command to be executed</param> public static string ExecuteCommand(IPAddress address, string command) { TraceFactory.Logger.Debug("Executing command: {0}".FormatWith(command)); SSHProtocol sshProtocol = new SSHProtocol(USER_NAME, PASSWORD, address); if (sshProtocol.Connect()) { // Login as root before executing commands LoginAsRoot(sshProtocol); sshProtocol.Send(command); return(sshProtocol.Send("\n")); } else { return(string.Empty); } }