I have a situation where i will login to a server using root user and password. but sometimes i will get a prompt "Do you want to login to primary console (yes or no)". we will give yes and will be prompted with password. So my question is if i use shellstream for this login . then can i use the SSHClient object to run commands with root user privilges. moreover is there any way to assign the ShellStream to SSHClient since the shell stream is created using SSHClient object.
string commandResult = string.Empty;
bool loginResult = false;
string strIPAdress = txtIP.Text.ToString().Trim();
string strUserName = txtUser.Text.ToString().Trim();// root user name
string strPassword = txtPassword.Text.ToString();// root password
string strCommand = txtCommand.ToString();
SshClient client = null;
try
{
client = new SshClient(strIPAdress, strUserName, strPassword);
client.Connect();
var shellStream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
string reply = String.Empty;
// Check if the Prompt contains Yes/No
MessageBox.Show(shellStream.ToString());
var rep = shellStream.Expect("(yes or [no])");
if (rep != null)
{
MessageBox.Show("Inside yes no check");
shellStream.WriteLine("yes");
rep = shellStream.Expect("Password:");
if (rep != null)
{
shellStream.WriteLine(strPassword );
// Check if the root user prompt
rep = shellStream.Expect(new Regex(@"[$#]"));
if (rep != null)
{
loginResult = true;
}
else
{
loginResult = false;
}
}
else
{
loginResult = false;
}
}
else
{
// Check if the root user prompt
rep = shellStream.Expect(new Regex(@"[$#]"));
if (rep != null)
{
loginResult = true;
}
else
{
loginResult = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
}
if (loginResult)
{
string commandName = txtCommand.Text.ToString();
SshCommand command = client.CreateCommand(commandName);
command.CommandTimeout = new TimeSpan(24, 0, 0);
commandResult = command.Execute();
}
}