• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#使用进程调用bash,不断往bash内插入命令并执行

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

呃呃呃,遇到了一个非常恶心得问题,现在就是,有几十万条数据需要当作参数调用python脚本

一次一次输入命令过去缓慢,经过了多层考虑决定使用进程来解决(不知道线程怎么开启bash得)

原理非常简单,类似与打开一个cmd窗口不断地往cmd窗口内写入命令

参数如下:

bashCount:打开进程个数,也就是启动bash数量

command:命令

     public static void CreateBash(int bashCount, List<string> command)
        {
            try
            {
                var data = Convert.ToInt32(Math.Ceiling((double)command.Count / bashCount));
                Parallel.For(0, bashCount, (i) =>
                {
                    Process p = new Process();
                    //设置要启动的应用程序
                    p.StartInfo.FileName = "bash";
                    //是否使用操作系统shell启动
                    p.StartInfo.UseShellExecute = false;
                    // 接受来自调用程序的输入信息
                    p.StartInfo.RedirectStandardInput = true;
                    //输出信息
                    p.StartInfo.RedirectStandardOutput = true;
                    // 输出错误
                    p.StartInfo.RedirectStandardError = true;
                    //不显示程序窗口
                    p.StartInfo.CreateNoWindow = true;
                    //启动程序
                    p.Start();
                    var test = command.Skip(i * data).Take(data).ToArray();
                    var count = 0;
                    var commandStr = "";
                    var commandCount = Convert.ToInt32(Math.Ceiling((double)test.Count() / 10));
                    if (test.Count() > 0)
                    {

                        foreach (var item in test)
                        {
                            count++;
                            var filePath = item.Split(".").FirstOrDefault();
                 //执行命令
var demo= $"{Environment.CurrentDirectory.ToString()}/ScriptFile/ProAnalysis {item} {filePath}.pa 0"; var demo1 = $"{Environment.CurrentDirectory.ToString()}/ScriptFile/pcap2featuresjson {item} {filePath}.json {filePath}.bin {filePath}.hex"; commandStr += demo + ";" + demo1; commandStr += ";"; if (count >= 100) { p.StandardInput.WriteLine(commandStr); count = 0; commandStr = ""; } } } p.StandardInput.WriteLine("exit"); p.StandardInput.AutoFlush = true; string strOuput = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); }); } catch (Exception e) { Console.WriteLine("失败" + e.Message); } }

可以直接复用

详细讲解一下方法:Parallel.For让多个bash并行运行,并且每个取出相等的命令,往bash写入


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何用C/C++实现去除字符串头和尾指定的字符发布时间:2022-07-13
下一篇:
c#一些警告的处理方法发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap