C# 使用 plink (putty) 处理输出重定向
C# process output redirection with plink (putty)
这让我抓狂...我想编写一个简单的 C# 应用程序(带 GUI),它通过 SSH 登录到我们的 Cisco 无线局域网控制器(简称 WLC)并创建来宾用户。 SSH.NET 如果不是 Cisco 出于某种原因决定不允许非交互式登录,这将非常容易 - 这意味着您无法传递用户名和密码,但必须在控制台中输入。更糟糕的是:他们在实际用户提示之前放置了一个额外的(非功能性)用户提示。神...
无论如何,我使用 plink.exe 解决了这个问题,它工作得很好,但是我想检查终端输出,这样我就可以确定是否正确执行了一些命令。我也想使用登录的输出,因为提示看起来有点慢,我不能依赖恒定的时间。
我找到了一些关于输出重定向的文章(例如 Process.start: how to get the output?),但它就是不起作用。我只能在 plink.exe 关闭时收集输出,但我仍需要使用 plink 实时读取输出 运行。
到目前为止,这是我的代码:
namespace WIFI
{
public class Program
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myForm());
}
public static String ProcessInput(string user)
{
GlobalVar.hasError = false;
GlobalVar.status = "";
if (user.Length < 5 || user.Length > 10)
{
GlobalVar.status = "Username ungültig";
GlobalVar.hasError = true;
}
else
{
WLCconnection WLC = new WLCconnection();
WLC.ConnectWLC();
if (!GlobalVar.hasError) WLC.UserExists(user);
if (!GlobalVar.hasError) WLC.CreateUser(user);
WLC.CloseWLC();
}
return GlobalVar.status;
}
}
public static class GlobalVar
{
public static Boolean hasError;
public static String status;
}
public class WLCconnection
{
Process terminal = new Process();
StringBuilder terminalOutput = new StringBuilder();
Boolean telnet = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["telnet"]);
String WLChostname = System.Configuration.ConfigurationManager.AppSettings["WLChostname"];
String WLCusername = System.Configuration.ConfigurationManager.AppSettings["WLCusername"];
String WLCpassword = System.Configuration.ConfigurationManager.AppSettings["WLCpassword"];
public void ConnectWLC()
{
terminal.StartInfo.FileName = @System.Configuration.ConfigurationManager.AppSettings["plinkpath"];
terminal.StartInfo.UseShellExecute = false;
terminal.StartInfo.RedirectStandardInput = true;
terminal.StartInfo.RedirectStandardOutput = true;
terminal.StartInfo.RedirectStandardError = true;
terminal.StartInfo.CreateNoWindow = true;
if (telnet) terminal.StartInfo.Arguments = "-telnet " + WLChostname;
else terminal.StartInfo.Arguments = "-ssh " + WLChostname;
terminal.OutputDataReceived += (s, e) => terminalOutput.Append(e.Data);
//terminal.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
terminal.ErrorDataReceived += (s, e) => terminalOutput.Append(e.Data);
//terminal.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
terminal.Start();
terminal.BeginOutputReadLine();
terminal.BeginErrorReadLine();
int runs = 0;
if (!telnet)
{
while (!terminalOutput.ToString().Contains("login as:") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write("workaroundSSHproblem\n");
}
runs = 0;
while (!terminalOutput.ToString().Contains("User:") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write(WLCusername + "\n");
runs = 0;
while (!terminalOutput.ToString().Contains("Password:") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write(WLCpassword + "\n");
}
public void CloseWLC()
{
terminal.StandardInput.WriteLine("logout");
terminal.WaitForExit();
terminal.CancelOutputRead();
terminal.Close();
terminal.Dispose();
}
public Boolean UserExists(String user)
{
int runs = 0;
while (!terminalOutput.ToString().Contains("(Cisco Controller) >") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write("show netuser detail " + user);
if (!terminalOutput.ToString().Contains("blabla"))
{
GlobalVar.status = "User " + user + " bereits aktiviert";
GlobalVar.hasError = true;
}
return GlobalVar.hasError;
}
public void CreateUser(String user)
{
int runs = 0;
while (!terminalOutput.ToString().Contains("(Cisco Controller) >") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write("config netuser add " + user + " " + System.Configuration.ConfigurationManager.AppSettings["guestpassword"] + " wlan 3 userType guest lifetime 86400");
if (!GlobalVar.hasError)
{
if (UserExists(user))
{
GlobalVar.status = "";
GlobalVar.hasError = false;
}
else
{
GlobalVar.status = "User " + user + " konnte nicht aktiviert werden";
GlobalVar.hasError = true;
}
}
}
protected void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
terminalOutput.Append(outLine.Data);
}
}
}
非常感谢您的帮助!
对谁有帮助:我再次使用 SSH.NET 解决了这个问题,但这次没有使用内置函数登录,而是结合 ShellStream 手动处理 in- 和输出。到目前为止效果很好。
这让我抓狂...我想编写一个简单的 C# 应用程序(带 GUI),它通过 SSH 登录到我们的 Cisco 无线局域网控制器(简称 WLC)并创建来宾用户。 SSH.NET 如果不是 Cisco 出于某种原因决定不允许非交互式登录,这将非常容易 - 这意味着您无法传递用户名和密码,但必须在控制台中输入。更糟糕的是:他们在实际用户提示之前放置了一个额外的(非功能性)用户提示。神...
无论如何,我使用 plink.exe 解决了这个问题,它工作得很好,但是我想检查终端输出,这样我就可以确定是否正确执行了一些命令。我也想使用登录的输出,因为提示看起来有点慢,我不能依赖恒定的时间。
我找到了一些关于输出重定向的文章(例如 Process.start: how to get the output?),但它就是不起作用。我只能在 plink.exe 关闭时收集输出,但我仍需要使用 plink 实时读取输出 运行。
到目前为止,这是我的代码:
namespace WIFI
{
public class Program
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myForm());
}
public static String ProcessInput(string user)
{
GlobalVar.hasError = false;
GlobalVar.status = "";
if (user.Length < 5 || user.Length > 10)
{
GlobalVar.status = "Username ungültig";
GlobalVar.hasError = true;
}
else
{
WLCconnection WLC = new WLCconnection();
WLC.ConnectWLC();
if (!GlobalVar.hasError) WLC.UserExists(user);
if (!GlobalVar.hasError) WLC.CreateUser(user);
WLC.CloseWLC();
}
return GlobalVar.status;
}
}
public static class GlobalVar
{
public static Boolean hasError;
public static String status;
}
public class WLCconnection
{
Process terminal = new Process();
StringBuilder terminalOutput = new StringBuilder();
Boolean telnet = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["telnet"]);
String WLChostname = System.Configuration.ConfigurationManager.AppSettings["WLChostname"];
String WLCusername = System.Configuration.ConfigurationManager.AppSettings["WLCusername"];
String WLCpassword = System.Configuration.ConfigurationManager.AppSettings["WLCpassword"];
public void ConnectWLC()
{
terminal.StartInfo.FileName = @System.Configuration.ConfigurationManager.AppSettings["plinkpath"];
terminal.StartInfo.UseShellExecute = false;
terminal.StartInfo.RedirectStandardInput = true;
terminal.StartInfo.RedirectStandardOutput = true;
terminal.StartInfo.RedirectStandardError = true;
terminal.StartInfo.CreateNoWindow = true;
if (telnet) terminal.StartInfo.Arguments = "-telnet " + WLChostname;
else terminal.StartInfo.Arguments = "-ssh " + WLChostname;
terminal.OutputDataReceived += (s, e) => terminalOutput.Append(e.Data);
//terminal.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
terminal.ErrorDataReceived += (s, e) => terminalOutput.Append(e.Data);
//terminal.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
terminal.Start();
terminal.BeginOutputReadLine();
terminal.BeginErrorReadLine();
int runs = 0;
if (!telnet)
{
while (!terminalOutput.ToString().Contains("login as:") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write("workaroundSSHproblem\n");
}
runs = 0;
while (!terminalOutput.ToString().Contains("User:") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write(WLCusername + "\n");
runs = 0;
while (!terminalOutput.ToString().Contains("Password:") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write(WLCpassword + "\n");
}
public void CloseWLC()
{
terminal.StandardInput.WriteLine("logout");
terminal.WaitForExit();
terminal.CancelOutputRead();
terminal.Close();
terminal.Dispose();
}
public Boolean UserExists(String user)
{
int runs = 0;
while (!terminalOutput.ToString().Contains("(Cisco Controller) >") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write("show netuser detail " + user);
if (!terminalOutput.ToString().Contains("blabla"))
{
GlobalVar.status = "User " + user + " bereits aktiviert";
GlobalVar.hasError = true;
}
return GlobalVar.hasError;
}
public void CreateUser(String user)
{
int runs = 0;
while (!terminalOutput.ToString().Contains("(Cisco Controller) >") && !GlobalVar.hasError)
{
Thread.Sleep(500);
if (runs < 20) runs++;
else GlobalVar.hasError = true;
}
terminal.StandardInput.Write("config netuser add " + user + " " + System.Configuration.ConfigurationManager.AppSettings["guestpassword"] + " wlan 3 userType guest lifetime 86400");
if (!GlobalVar.hasError)
{
if (UserExists(user))
{
GlobalVar.status = "";
GlobalVar.hasError = false;
}
else
{
GlobalVar.status = "User " + user + " konnte nicht aktiviert werden";
GlobalVar.hasError = true;
}
}
}
protected void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
terminalOutput.Append(outLine.Data);
}
}
}
非常感谢您的帮助!
对谁有帮助:我再次使用 SSH.NET 解决了这个问题,但这次没有使用内置函数登录,而是结合 ShellStream 手动处理 in- 和输出。到目前为止效果很好。