【发布时间】:2016-01-15 19:06:40
【问题描述】:
尝试在127.0.0.1:9999 上打开 SSH 隧道时,我在 C# 中遇到问题。
这是我的Program.cs
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Begin");
PlinkTest plink = new PlinkTest();
String feedback = plink.CreateTunnel("1.2.3.4", "user", "user");
Console.WriteLine("THING:" + feedback);
Console.Read();
}
}
这是我的程序,PlinkTest.cs:
class PlinkTest
{
String PATH_TO_PLINK = @"C:\plink\plink.exe";
public PlinkTest()
{
}
public string RequestInfo(string remoteHost, string userName, string password, string[] lstCommands)
{
m_szFeedback = "Feedback from: " + remoteHost + "\r\n";
// ProcessStartInfo psi = new ProcessStartInfo("echo y | C:\\plink\\plink.exe")
ProcessStartInfo psi = new ProcessStartInfo("C:\\plink\\plink.exe")
{
Arguments = String.Format("-ssh -N -D 9999 user@1.2.3.4 -pw user -v"),
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
Process p = Process.Start(psi);
m_objLock = new Object();
m_blnDoRead = true;
AsyncReadFeedback(p.StandardOutput); // start the async read of stdout
AsyncReadFeedback(p.StandardError); // start the async read of stderr
StreamWriter strw = p.StandardInput;
foreach (string cmd in lstCommands)
{
strw.WriteLine(cmd); // send commands
}
strw.WriteLine("exit"); // send exit command at the end
p.WaitForExit(); // block thread until remote operations are done
return m_szFeedback;
}
public string CreateTunnel(string remoteHost, string userName, string password)
{
m_szFeedback = "Feedback from: " + remoteHost + "\r\n";
// ProcessStartInfo psi = new ProcessStartInfo("echo y | C:\\plink\\plink.exe")
ProcessStartInfo psi = new ProcessStartInfo(PATH_TO_PLINK)
{
Arguments = String.Format("-ssh -N -D 9999 user@1.2.3.4 -pw user -v"),
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
Process p = Process.Start(psi);
m_objLock = new Object();
m_blnDoRead = true;
AsyncReadFeedback(p.StandardOutput); // start the async read of stdout
AsyncReadFeedback(p.StandardError); // start the async read of stderr
StreamWriter strw = p.StandardInput;
// SLEEP HERE 10 SEC
strw.WriteLine("exit"); // send exit command at the end
p.WaitForExit(); // block thread until remote operations are done
return m_szFeedback;
}
private String m_szFeedback; // hold feedback data
private Object m_objLock; // lock object
private Boolean m_blnDoRead; // boolean value keeping up the read (may be used to interrupt the reading process)
public void AsyncReadFeedback(StreamReader strr)
{
Thread trdr = new Thread(new ParameterizedThreadStart(__ctReadFeedback));
trdr.Start(strr);
}
private void __ctReadFeedback(Object objStreamReader)
{
StreamReader strr = (StreamReader)objStreamReader;
string line;
while (!strr.EndOfStream && m_blnDoRead)
{
line = strr.ReadLine();
// lock the feedback buffer (since we don't want some messy stdout/err mix string in the end)
lock (m_objLock) { m_szFeedback += line + "\r\n"; }
}
}
}
第一个问题
我面临的第一个问题是自动接受 RSA 密钥,我可以运行
echo y |plink.exe -ssh -N -D 9999 user@1.2.3.4 -pw user -v
但事实证明,当我在Process p = Process.Start(psi); 行使用echo y | 时出现错误,其中psi 变量是可以的。我知道如何在 Arguments = String.Format("-ssh -N -D 9999 user@1.2.3.4 -pw user -v"), 之后放置参数,但我不知道如何将它们放在命令前面。
第二个问题
第二个问题是我不想等待内容,因为当我创建隧道时它什么也没说,只是在等待。我只想在127.0.0.1:9999的后台打开一个SSH隧道,仅此而已。
我需要改变什么?
谢谢。
【问题讨论】:
-
我会说这不是 C# 问题,这是使用 plink.exe 的问题。您可能想从文件中读取
y:plink blabla < yes.txt。由于您已经从为 plink 打开的流中读取,您可能会考虑将 y 写入它,因为echo y |有效地做同样的事情。 -
真的吗?它将与
plink blabla < yes.txthmm 一起使用,必须尝试,我在 linux 中使用了很多 :))
标签: c# ssh public-key ssh-tunnel