using System;
using System.Diagnostics;
class Program
{
// 定义root密码(请用你的实际密码替换)
private const string RootPassword = "your_root_password_here";
static void Main()
{
try
{
// 需要执行的命令列表
string[] commands = new string[]
{
$"echo {RootPassword} | sudo -S cat /proc/sys/fs/inotify/max_user_instances",
$"echo {RootPassword} | sudo -S cp /etc/sysctl.conf /etc/sysctl.conf.bak",
$"echo {RootPassword} | sudo -S bash -c \"echo 'fs.inotify.max_user_watches = 1638400' >> /etc/sysctl.conf\"",
$"echo {RootPassword} | sudo -S bash -c \"echo 'fs.inotify.max_user_instances = 1638400' >> /etc/sysctl.conf\"",
$"echo {RootPassword} | sudo -S sysctl -p",
$"echo {RootPassword} | sudo -S cat /proc/sys/fs/inotify/max_user_instances"
};
foreach (var cmd in commands)
{
ExecuteCommand(cmd);
}
Console.WriteLine("所有命令执行完毕。");
}
catch (Exception ex)
{
Console.WriteLine($"发生错误: {ex.Message}");
}
}
static void ExecuteCommand(string command)
{
try
{
// 创建进程启动信息
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "/bin/bash"; // 使用bash解释器
startInfo.Arguments = $"-c \"{command}\"";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
using (Process process = new Process())
{
process.StartInfo = startInfo;
process.Start();
// 读取命令输出
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
// 打印输出和错误(如果有)
Console.WriteLine($"命令: {command}");
if (!string.IsNullOrEmpty(output))
{
Console.WriteLine($"输出:\n{output}");
}
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine($"错误:\n{error}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"执行命令时出错: {ex.Message}");
}
}
}