Cmd重定向
1、执行单条cmd命令
public static string ExecuteCmd(string command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
//p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
p.Start();//启动程序
//消除三行启动信息
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
string error = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
p.WaitForExit();
p.Close();
return result;
}
2、一次执行多条cmd命令
public static string ExecuteCmd(string[] commands)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
//p.StandardInput.AutoFlush = true;//每次写入命令后刷新,报未StandardInput未重定向
//p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
p.Start();//启动程序
//消除三行启动信息
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine(); //向cmd窗口发送输入信息
foreach (string cmd in commands)
{
p.StandardInput.WriteLine(cmd);
p.StandardInput.Flush();
}
p.StandardInput.WriteLine("exit");
string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
string error = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
p.WaitForExit();
p.Close(); return result;
}
3、利用cmd重定向注册DLL、创建服务、加载证书
public static string RegsvrFile(string filePath, bool reg = true, bool fileBit64 = false)
{
string result;
string[] commands = { "", "" };
if (!fileBit64 && Environment.Is64BitOperatingSystem)
commands[0] = "cd /d %WinDir%\\SysWOW64";
if (reg)
commands[1] = "regsvr32 /s \"" + filePath + "\"";
else
commands[1] = "regsvr32 /u /s \"" + filePath + "\"";
if (string.IsNullOrWhiteSpace(commands[0]))
result = CmdOper.ExecuteCmd(commands[1]);
else
result = CmdOper.ExecuteCmd(commands);
return result;
} public static string ScCreate(string filePath, string serviceName, string displayName, string description, string start="auto", string depend=null)
{
string result;
string[] commands = { "", "" };
if (string.IsNullOrWhiteSpace(depend))
{
commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\"",
serviceName, filePath, start, displayName);
}
else
{
commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\" depend= {4}",
serviceName, filePath, start, displayName, depend);
}
if (!string.IsNullOrWhiteSpace(description))
{
commands[1] = string.Format("sc description {0} \"{1}\"", serviceName, description);
}
if (string.IsNullOrWhiteSpace(commands[1]))
result = CmdOper.ExecuteCmd(commands[0]);
else
result = CmdOper.ExecuteCmd(commands); return result;
} public static string ScStart(string serviceName)
{
string command = "net start " + serviceName;
return CmdOper.ExecuteCmd(command);
} public static string ScStop(string serviceName)
{
string command = "net stop " + serviceName;
return CmdOper.ExecuteCmd(command);
} public static string ScDelete(string serviceName)
{
string[] commands = { "net stop " + serviceName, "sc delete " + serviceName };
return CmdOper.ExecuteCmd(commands);
} public static string CertFile(string filePath, bool install=true)
{
string result;
string[] commands = { "", "" };
if (Environment.Is64BitOperatingSystem)
commands[0] = "cd /d %WinDir%\\SysWOW64";
if (install)
commands[1] = "certutil -addstore root \"" + filePath + "\"";
else
commands[1] = "certutil -delstore root \"" + filePath + "\"";
if (string.IsNullOrWhiteSpace(commands[0]))
result = CmdOper.ExecuteCmd(commands[1]);
else
result = CmdOper.ExecuteCmd(commands);
return result;
}
Cmd重定向的更多相关文章
- cmd 重定向
关于cmd 命令的重定向输出 2>&1 mycommand >mylog.txt 2>&1 应该是最经典的用法了. 命令的结果可以通过" %> &qu ...
- <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序
最近学习加密算法,需要生成素数表,一开始使用简单的循环,从2开始判断.代码如下: #include<iostream> #include<cstdio> #include< ...
- Windows 常用Cmd命令行 (持续更新...)
查看IP ipconfig 查看WIFI密码 netsh wlan show profiles wifi_name key = clear 系统探针 systeminfo CMD重定向 输出符号> ...
- Kali Linux渗透基础知识整理(四):维持访问
Kali Linux渗透基础知识整理系列文章回顾 维持访问 在获得了目标系统的访问权之后,攻击者需要进一步维持这一访问权限.使用木马程序.后门程序和rootkit来达到这一目的.维持访问是一种艺术形式 ...
- NetCat使用手册
简介: 在网络工具中有“瑞士军刀”美誉的NetCat(以下简称nc),在我们用了N年了至今仍是爱不释手.因为它短小精悍(这个用在它身上很适合,现在有人已经将其修改成大约10K左右,而且功能不减少) ...
- IPC$命令详解
一 摘要二 什么是ipc$三 什么是空会话四 空会话可以做什么五 ipc$所使用的端口六 ipc管道在hack攻击中的意义七 ipc$连接失败的常见原因八 复制文件失败的原因九 关于at命令和xp对i ...
- 445port入侵具体解释
445port入侵具体解释 关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...
- 445port入侵详细解释
445port入侵具体解释 关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...
- 空连接ipc$入侵
使用命令 net use url=file://\\IP\ipc$\\IP\ipc$ "" /user:"" 就可以简单地和目标建立一个空连接(需要目标开放ip ...
随机推荐
- FR嵌套报表(Nested Report)
//主界面只是说明放置了哪些东西(3个ADOQuery不必放): //MasterSource.MasterField的设置如下: 1) Customer.Orders.Items 的 MasterS ...
- [每日一题2020.06.08]洛谷P1605 DFS
今天cf又杯具的只写出2题, 虽然AB题20分钟左右就搞定了, 但是CD写了2个小时也没写出来 D题我用到了DFS, 虽然必不正确, 但是我至少发现了一个问题, 那就是我连DFS都忘了, 于是怒找DF ...
- 03 . Jenkins构建之代码扫描
Sonar简介 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson/Jenkins 等)不 ...
- Git创建多个ssh key
在使用git的时候,遇到需要创建多个ssh key的需求,一个用来git hub项目,一个用来git lab项目: 之前如果已将创建过一个ssh key,那么在创建第二个的时候,要修改默认名称,然后增 ...
- BUAA_OO_2020_Unit1_总结博客
BUAA_OO_2020_Unit1_总结 2020年春季学期第四周,OO第一单元落下帷幕,几多欢喜几多愁,现做如下总结(按每次作业的递进顺序) 一.第一次作业(基础的幂函数求导) 基于度量的程序结构 ...
- rust 编译器工作流
将源代码转为高级中间表示,在将其转为中级中间表示,在将其转为LLVM IR, 最终输出机器码. rust 租借检查 选项优化,代码生成(宏, 范型) , 都是在MIR层.
- .Net Core微服务入门全纪录(五)——Ocelot-API网关(下)
前言 上一篇[.Net Core微服务入门全纪录(四)--Ocelot-API网关(上)]已经完成了Ocelot网关的基本搭建,实现了服务入口的统一.当然,这只是API网关的一个最基本功能,它的进阶功 ...
- v-model指令的学习(双向绑定)
v-bind 只能实现数据的单项绑定,从data到view,无法实现双向绑定 v-model可以实现表单元素和model中数据的双向绑定 注意:model只能运用到表单元素中 如:input sele ...
- oracle 索引失效原因_汇总
1) 没有查询条件,或者查询条件没有建立索引 2) 在查询条件上没有使用引导列 3) 查询的数量是大表的大部分,应该是30%以上. 4) 索引本身失效 5) 查询条件使用函数在索引列上,或者对索引列进 ...
- 网络编程 套接字socket TCP UDP
网络编程与套接字 网络编程 网络编程是什么: 网络通常指的是计算机中的互联网,是由多台计算机通过网线或其他媒介相互链接组成的 编写基于网络的应用程序的过程序称之为网络编程. 网络编程最主要的工 ...