09-10-2019 02:57 AM
Hi all, I'm trying to write the C sharp application to configure Cisco Switch.
I'm using Renci.SshNet library. The problem whe I'm trying to enter to CONFIG mode. The next command is being not recognized. Any idea?
Regard
Paweł
09-10-2019 03:02 AM
09-10-2019 03:48 AM
Here's asynchronous version:
using (var sshclient = new SshClient(sutIP, sutUsername, sutPassword))
{
sshclient.Connect();
var cmd = sshclient.CreateCommand("configure terminal");
var asynch = cmd.BeginExecute();
while (!asynch.IsCompleted)
{
// Waiting for command to complete...
System.Threading.Thread.Sleep(2000);
}
var result = cmd.EndExecute(asynch);
sshclient.CreateCommand("interface Ethernet 1/1");
asynch = cmd.BeginExecute();
while (!asynch.IsCompleted)
{
// Waiting for command to complete...
System.Threading.Thread.Sleep(2000);
}
result = cmd.EndExecute(asynch);
sshclient.CreateCommand("speed 10000");
asynch = cmd.BeginExecute();
while (!asynch.IsCompleted)
{
// Waiting for command to complete...
System.Threading.Thread.Sleep(2000);
}
result = cmd.EndExecute(asynch);
sshclient.CreateCommand("end");
asynch = cmd.BeginExecute();
while (!asynch.IsCompleted)
{
// Waiting for command to complete...
System.Threading.Thread.Sleep(2000);
}
result = cmd.EndExecute(asynch);
sshclient.CreateCommand("show run");
asynch = cmd.BeginExecute();
while (!asynch.IsCompleted)
{
// Waiting for command to complete...
System.Threading.Thread.Sleep(2000);
}
result = cmd.EndExecute(asynch);
sshclient.Disconnect();
}
And here not:
List<string> commandSet = new List<string>();
commandSet.Add("configure terminal");
commandSet.Add("interface Ethernet 1/1");
commandSet.Add("speed 10000");
commandSet.Add("end");
commandSet.Add("copy running-config startup-config");
commandSet.Add("show running-config");
RunListOfSSHCommands(sutIP, sutUsername, sutPassword, commandSet);
public static void RunListOfSSHCommands(string sutIP, string sutUsername, string sutPassword, List<string> commandSet)
{
using (SshClient ssh = new SshClient(sutIP, sutUsername, sutPassword))
{
ssh.Connect();
foreach (string command in commandSet)
{
var result = ssh.RunCommand(command);
Console.WriteLine(result.Result.ToString());
}
ssh.Disconnect();
}
}
The error is attached.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide