SSH connection from ASPNET to a unix server

J

jg.lalanne

Hi,

I have developped a web service in C# .NET that has to launch a unix
script on a AIX machine. I have installed on my windows 2003 server the
cygwin environment with the ssh.exe utility.
I have created a RSA public key without passphrase and appended it in
the authorized_keys file on the unix server.
When I launch my C:\cygwin\bin\ssh.exe unixlogin@unixserverhost 'uname
-a' from a doc command prompt, it works correctly without asking me a
password.

I do the same with the following code from a C# console project, and it
works correctly as well :

_____________________________________________________
string commandText = "C:\\cygwin\\bin\\ssh.exe";
string argsText = "login@server 'uname -a'";

Process objProcess = new Process();
objProcess.StartInfo.FileName = commandText;
objProcess.StartInfo.Arguments = argsText;
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
objProcess.StartInfo.CreateNoWindow = false;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.RedirectStandardError = true;
objProcess.StartInfo.UseShellExecute = false;
try
{
objProcess.Start();
objProcess.WaitForExit();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
string error = objProcess.StandardError.ReadToEnd();
string output = objProcess.StandardOutput.ReadToEnd();
Console.Out.WriteLine("output = " + output );
---------------------------------------------------------

When I put the same code in my Web Service, it does not work anymore.
This is, I supposed, because of the ASPNET profile that is different
from the window user profile used for the RSA key generation.

That was I tried to use the impersonation to force the good user
profile in my web service code. But it does not work either....

Does anyone of you manage to do such a SSH communication from ASPNET to
a unix server ?

Thanks in advance for your help.

Best Regards

Jean-Guillaume LALANNE
 
J

jeangui

Finally, I found a solution by myself.
The ssh.exe has to be in a directory which the aspnet profile has
access to.
Moreover, the first time, you connect to the ssh server a prompt is
launched to ask for an answer like "....(y/n) ? ...". That was why the
process was hanging.
To avoid this, we have to use the :

process.StartInfo.RedirectStandardInput = true;

myStreamWriter.WriteLine("y");
myStreamWriter.Close();
objProcess.WaitForExit();

Hope it can help,

regards
Jean-Guillaume LALANNE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,821
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top