Hello Everyone,
I am trying to shutdown application on one of our Servers, and we are trying to do it via C#
We downloaded the SSH.NET library file and wrote some simple commands to test out if the code we wrote is working or not.
We tried grep, date, pwd and all seem to work properly.
However, the "cd" command does not work at all.
Can someone help me out on this ?
In the code, i am trying to do a "cd" and then the do a "pwd" to check if the cd actually happened, but the pwd returns the same directory where i landed after i logged in.
To be sure on our side, the path we provided is the absolute path and not the relative one.
This is a code snippet (got the basic working code from one of the discussions here on CodePlex)
using (var ssh = new SshClient("host", "username", "password"))
{
ssh.Connect();
string Command = "cd /apps/dtsjrap1/jira/depot" ;
var cmd = ssh.CreateCommand(Command);
var asynch = cmd.BeginExecute(delegate(IAsyncResult ar)
{
Console.WriteLine("Finished.");
}, null);
var reader = new StreamReader(cmd.OutputStream);
while (!asynch.IsCompleted)
{
var result = reader.ReadToEnd();
if (string.IsNullOrEmpty(result))
continue;
Console.Write(result);
}
cmd.EndExecute(asynch);
var cmd1 = ssh.CreateCommand("pwd");
var asynch1 = cmd1.BeginExecute(delegate(IAsyncResult ar)
{
//Console.WriteLine("Finished.");
}, null);
var reader1 = new StreamReader(cmd1.OutputStream);
while (!asynch1.IsCompleted)
{
var result = reader1.ReadToEnd();
if (string.IsNullOrEmpty(result))
continue;
Console.Write(result);
}
cmd1.EndExecute(asynch1);
ssh.Disconnect();
}
}
I am trying to shutdown application on one of our Servers, and we are trying to do it via C#
We downloaded the SSH.NET library file and wrote some simple commands to test out if the code we wrote is working or not.
We tried grep, date, pwd and all seem to work properly.
However, the "cd" command does not work at all.
Can someone help me out on this ?
In the code, i am trying to do a "cd" and then the do a "pwd" to check if the cd actually happened, but the pwd returns the same directory where i landed after i logged in.
To be sure on our side, the path we provided is the absolute path and not the relative one.
This is a code snippet (got the basic working code from one of the discussions here on CodePlex)
using (var ssh = new SshClient("host", "username", "password"))
{
ssh.Connect();
string Command = "cd /apps/dtsjrap1/jira/depot" ;
var cmd = ssh.CreateCommand(Command);
var asynch = cmd.BeginExecute(delegate(IAsyncResult ar)
{
Console.WriteLine("Finished.");
}, null);
var reader = new StreamReader(cmd.OutputStream);
while (!asynch.IsCompleted)
{
var result = reader.ReadToEnd();
if (string.IsNullOrEmpty(result))
continue;
Console.Write(result);
}
cmd.EndExecute(asynch);
var cmd1 = ssh.CreateCommand("pwd");
var asynch1 = cmd1.BeginExecute(delegate(IAsyncResult ar)
{
//Console.WriteLine("Finished.");
}, null);
var reader1 = new StreamReader(cmd1.OutputStream);
while (!asynch1.IsCompleted)
{
var result = reader1.ReadToEnd();
if (string.IsNullOrEmpty(result))
continue;
Console.Write(result);
}
cmd1.EndExecute(asynch1);
ssh.Disconnect();
}
}