I have a problem in C# when trying to open a SSH tunnel on 127.0.0.1:9999
.
This is my 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();
}
}
This is my program, 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"; }
}
}
}
FIRST PROBLEM
The first problem I am facing is accepting RSA key automatically, whitch I can do running
echo y |plink.exe -ssh -N -D 9999 user@1.2.3.4 -pw user -v
but it turns out that I get an error when I use echo y |
at Process p = Process.Start(psi);
line where psi
variable is ok. I know how to put arguments after with Arguments = String.Format("-ssh -N -D 9999 user@1.2.3.4 -pw user -v"),
but i don't know how to put them in front of the command.
SECOND PROBLEM
The second problem is that I don't want to wait for the content because when I am creating the tunnel it says nothing, just waiting. I just want to open a SSH tunnel in background on 127.0.0.1:9999
, that's all.
What do I have to change?
Thanks.