用C#调用程序执行ssh
来源:互联网 发布:java往数组中添加元素 编辑:程序博客网 时间:2024/06/04 00:20
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace CmdProcess
{
class Program
{
public static void Main(string[] args)
{
InitProcess(@"C:\Windows\System32\cmd.exe", "", null);
p.StandardInput.WriteLine(@"C:Windows\plink.exe root@192.168.2.15");
Console.ReadKey(true);
}
private static Process p = new Process();
private static void InitProcess(string file, string args, string dir)
{
try
{
p.StartInfo.FileName = file;
p.StartInfo.Arguments = args;
if (!string.IsNullOrEmpty(dir))
{
p.StartInfo.WorkingDirectory = dir;
}
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
// // Handle Standard Output
// p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
// p.BeginOutputReadLine();
//
// p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
// p.BeginErrorReadLine();
new Thread(new ThreadStart(GetStdErr)).Start();
new Thread(new ThreadStart(GetStdOut)).Start();
new Thread(new ThreadStart(SetStdIn)).Start();
}
catch (Exception e)
{
Console.Error.WriteLine("SetStdIn: " + e.Message);
//System.Diagnostics.Debug.Print("SetStdIn: " + e.Message);
}
}
private static void CloseProcess()
{
p.Close();
p.Dispose();
}
private static void SetStdIn()
{
string line = "";
try
{
StreamReader reader = new StreamReader(Console.OpenStandardInput());// p.StandardOutput;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
p.StandardInput.WriteLine(line);
}
}
catch (Exception e)
{
Console.Error.WriteLine("SetStdIn: " + e.Message);
//System.Diagnostics.Debug.Print("SetStdIn: " + e.Message);
}
}
private static void GetStdOut()
{
string line = "";
try
{
StreamReader reader = p.StandardOutput;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
Console.WriteLine(line);
//System.Diagnostics.Debug.Print(line);
}
}
catch (Exception e)
{
Console.Error.WriteLine("GetStdOut: " + e.Message);
//System.Diagnostics.Debug.Print("GetStdOut: " + e.Message);
}
}
private static void GetStdErr()
{
string line = "";
try
{
StreamReader reader = p.StandardError;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
Console.WriteLine(line);
//System.Diagnostics.Debug.Print(line);
}
}
catch (Exception e)
{
Console.Error.WriteLine("GetStdErr: " + e.Message);
//System.Diagnostics.Debug.Print("GetStdErr: " + e.Message);
}
}
}
}
- 用C#调用程序执行ssh
- C#调用执行程序
- c#调用CMD执行程序
- C#如何调用cmd程序的执行
- C#程序中调用cmd执行命令
- C#程序调用cmd.exe执行命令
- c# 同步调用外部程序及终止执行
- 在C#的winForm程序中调用和执行javascript
- 在C#的winForm程序中调用和执行javascript
- ssh client 程序执行命令
- ssh 后台执行java 程序
- ssh 后台执行java 程序
- 批处理调用执行程序
- 用C#程序调用java程序
- C# 定时执行程序
- C#定时执行程序
- C# - 程序的执行
- C#定时执行程序
- giuseppe zanotti sneakers men bus and truck collision 19 people were injured two drivers were insis
- linux文件系统命令(2)---df和du
- 关于Object-C 底层实现self isa 的理解
- 通过Intel XDK编写跨平台app(一)
- Test
- 用C#调用程序执行ssh
- 第十三章 13.6.1节练习
- Android/Java随机序列
- 锁相环的组成和工作原理
- MFC Edit Control 捕捉回车用法
- 【记录】int-->CString
- Http协议
- “软工视频”第二章总结
- android开发包官网高速下载