How to reload a Cisco Router in C#? (General questions)
by zubenelgenubi, Thursday, May 01, 2008, 23:46 (6049 days ago)
Hi,
Id like to specify a new configuration and reload my cisco router using wodSSH.Net. What is the recommended method for doing so?
Here is what I have so far, but it fails to get past the connect.
Ssh1.Login = user ;
Ssh1.Password = pass ;
Ssh1.Protocol = WeOnlyDo.Client.SSH.SupportedProtocols.SSHAuto;
Ssh1.Timeout = 10;
Ssh1.TerminalType = tty ;
Ssh1.Blocking = true;
Ssh1.Connect( 192.168.0.1 );
Ssh1.WaitFor( cisco871 );
Ssh1.Send( configure terminal
);
Ssh1.WaitFor( cisco871(config) );
Ssh1.Send( file prompt quiet
);
Ssh1.WaitFor( cisco871(config) );
Ssh1.Send( end
);
Ssh1.WaitFor( cisco871 );
Ssh1.Send( copy OldConfig startup-config
);
Ssh1.Send( reload
);
Ssh1.WaitFor( Proceed with reload? [confirm] );
Ssh1.Send(
);
Ssh1.Disconnect(); //Is this necessary?
Thanks
Re: How to reload a Cisco Router in C#?
by woddrazen, Friday, May 02, 2008, 00:02 (6049 days ago) @ zubenelgenubi
Tia,
Which line produce error and which error you receive? Can you please add \r\n after each Send Method call. If this failed please try just \r or \n.
I think you don't need to call Disconnected Method if you are successful in restarting your Cisco router. wodSSHNET will be auto disconnected.
If it failed on Connect Method maybe you need to change protocol to Telnet. Most old Cisco servers use only Telnet protocol. Did you maybe use some other client for that router?
Drazen
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Friday, May 02, 2008, 00:41 (6049 days ago) @ woddrazen
Not sure why but the characters were dropped from my last post.
I do have a '
' after the text in each send call.
The error is a TimeoutException 'Timeout occurred due to inactivity' which is returned by the connect method.
What is curious is that I can connect in non blocking mode without a problem, so I dont think it is telnet vs ssh? Or is it?
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Friday, May 02, 2008, 00:46 (6049 days ago) @ zubenelgenubi
Here is the detailed exception from the Connect method:
WeOnlyDo.Exceptions.SSH.TimeoutException was unhandled
Message= Timeout occurred due to inactivity.
Source= WeOnlyDo.Client.SSH
StackTrace:
at ..()
at ..(String )
at WeOnlyDo.Client.SSH.Connect(String Hostname)
at ComSelector.Form1.button3_Click(Object sender, EventArgs e) in D:Development ProjectsComSelectorComSelectorForm1.cs:line 76
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ComSelector.Program.Main() in D:Development ProjectsComSelectorComSelectorProgram.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Re: How to reload a Cisco Router in C#?
by woddrazen, Friday, May 02, 2008, 08:58 (6049 days ago) @ zubenelgenubi
Tia,
Actually you aren't' connected. When non blocking mod is used errors aren't received immediately you should check it in Connected Event.using ErrorCode and ErrorText variable.
Did you try with Telnet protocol? What happened? Did you maybe use some other client to connect there? Putty for example.
Drazen
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Friday, May 02, 2008, 12:20 (6049 days ago) @ woddrazen
Thanks for the quick reply,
I have set a breakpoint in the Connectd event to try to debug, but the code never gets past the .Connect() method, where it times out as described above.
I have tried telnet as well from the Code as well as from putty. The .Connect method still times out. Here is the current code:
Ssh1.Hostname = 192.168.0.1 ;
Ssh1.Login = user ;
Ssh1.Password = pass ;
Ssh1.Protocol = WeOnlyDo.Client.SSH.SupportedProtocols.SSHAuto;
Ssh1.Timeout = 10;
Ssh1.TerminalType = tty ;
Ssh1.Blocking = true;
Ssh1.Connect();
Ssh1.WaitFor( cisco871# );
Ssh1.Send( configure terminal
);
Ssh1.WaitFor( cisco871#(config) );
Ssh1.Send( file prompt quiet
);
Ssh1.WaitFor( cisco871#(config) );
Ssh1.Send( end
);
Ssh1.WaitFor( cisco871# );
Ssh1.Send( copy OldConfig startup-config
);
Ssh1.Send( reload
);
Ssh1.WaitFor( Proceed with reload? [confirm] );
Ssh1.Send(
);
TIA
Re: How to reload a Cisco Router in C#?
by woddrazen, Friday, May 02, 2008, 12:42 (6049 days ago) @ zubenelgenubi
Tia,
I'm not sure that I understand you correctly. Are you able to connect there with some other client?
Drazen
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Friday, May 02, 2008, 12:45 (6049 days ago) @ woddrazen
Tia,
I'm not sure that I understand you correctly. Are you able to connect there with some other client?
Drazen
Yes I can connect with putty using either telnet or ssh. It seems like the negotiation of the certificates might be failing in blocking mode? Is that possible?
Re: How to reload a Cisco Router in C#?
by woddrazen, Friday, May 02, 2008, 13:49 (6048 days ago) @ zubenelgenubi
Tia,
Can you please check if maybe firewall is turn on. Can you please disable it and try it again.
That could be issue here.
Drazen
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Monday, May 12, 2008, 20:03 (6038 days ago) @ woddrazen
Hi,
Still having this problem.
To simplify I have slightly modified the code in Wait For And Execute as follows:
{
WeOnlyDo.Client.SSH Ssh1 = new SSH();
Console.WriteLine( This sample will connect with SSH protocol to the router of your choice and wait until 'cisco871' is found within received data. Hopefully, this is the prompt when new commands can be entered. Then it will enter 'show ver' command, wait for response, and exit. );
Console.WriteLine(
);
Console.Write( Please enter hostname: );
Ssh1.Hostname = Console.ReadLine();
Console.Write( Please enter login: );
Ssh1.Login = Console.ReadLine();
Console.Write( Please enter passowrd: );
Ssh1.Password = Console.ReadLine();
Ssh1.Blocking = true;
Ssh1.Protocol = WeOnlyDo.Client.SSH.SupportedProtocols.SSHAuto;
Ssh1.Timeout = 10;
Ssh1.Connect();
String a;
a = Ssh1.WaitFor( cisco871# );
// Console.Write(a);
a = Ssh1.Execute( show ver
, cisco871# );
Console.Write(a);
a = Ssh1.Receive();
Console.Write(a);
Ssh1.Disconnect();
}
and I receive the following exception:
WeOnlyDo.Exceptions.SSH.TimeoutException was unhandled
Message= Timeout occurred due to inactivity.
Source= WeOnlyDo.Client.SSH
StackTrace:
at ..(String , Byte[]& , Int32 )
at ..(String , Int32 )
at WeOnlyDo.Client.SSH.WaitFor(String Pattern)
at Wexec.Class1.Main(String[] args) in C:UserscharlieAppDataRoamingWeOnlyDoSampleswodSSH.NETWexec.cs:line 38
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I can allow a remote connection to this router since its a test platform. Please advise.
Re: How to reload a Cisco Router in C#?
by wodDamir, Monday, May 12, 2008, 20:11 (6038 days ago) @ zubenelgenubi
Hi,
If you can allow us access to that router, that would be great! In that case, you can send any private information to techsupport@weonlydo.com
Also, I'm pretty sure that the issue occurs since WaitFor never gets the specified prompt. What line exactly times out?
In first WaitFor call I see that you have a tryiling <space> character. Is that there on purpose?
Regards,
Damba
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Monday, May 12, 2008, 20:46 (6038 days ago) @ wodDamir
hi,
Thanks for the response.
Well - good news and bad news.
-Your observation about trailing whitespace was helpful. I removed the whitespace and was tehn able to connect from the console app.
-So I tried the same code from the basic CS windows app and it just hangs.
Is there something additional that needs to be sent with a connect from a windows app that does not need to be sent from a console app?
TIA
Re: How to reload a Cisco Router in C#?
by wodDamir, Monday, May 12, 2008, 21:39 (6038 days ago) @ zubenelgenubi
Hi,
You mean our Basic sample?
Or a new project that you started?
When using component in blocking mode, you have to watch out that you don't invoke any methods from events.
Can you show me a code snippet of your windows app?
Regards,
Damba
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Monday, May 12, 2008, 21:57 (6038 days ago) @ wodDamir
YEs I mean your basic app. In CS.
I pasted the code from the console app into the windows app and the same hanging occured.
Ideas?
Re: How to reload a Cisco Router in C#?
by wodDamir, Monday, May 12, 2008, 22:01 (6038 days ago) @ zubenelgenubi
Hi,
Did you remove the existing code? Or just edited it?
Can you send your code to techsupport@weonlydo.com
Regards,
Damba
Re: How to reload a Cisco Router in C#?
by zubenelgenubi, Tuesday, May 13, 2008, 01:53 (6038 days ago) @ wodDamir
Here is a modified method from Basic CS Sample code.
private void Button2_Click(object sender, System.EventArgs e)
{
Ssh1.Login = TextBox3.Text;
Ssh1.Password = TextBox4.Text;
/*
Ssh1.Protocol = WeOnlyDo.Client.SSH.SupportedProtocols.SSHAuto;
Ssh1.Timeout = 10;
Ssh1.TerminalType = tty ;
Ssh1.Command = show ver
;
Ssh1.Connect(TextBox2.Text);
* */
Ssh1.Blocking = true;
Ssh1.Protocol = WeOnlyDo.Client.SSH.SupportedProtocols.SSHAuto;
Ssh1.Timeout = 10;
Ssh1.Connect(TextBox2.Text);
String a;
a = Ssh1.WaitFor( cisco871# );
// Console.Write(a);
a = Ssh1.Execute(
, cisco871# );
Console.Write(a);
a = Ssh1.Receive();
Console.Write(a);
Ssh1.Disconnect();
}
It just hangs after connect. No time out exception. In my own code, I receive a timeout exception as described above.
Re: How to reload a Cisco Router in C#?
by wodDamir, Tuesday, May 13, 2008, 09:13 (6038 days ago) @ zubenelgenubi
Hi,
I see that you have two Connect calls. You should remove the first one. Also, you are setting some properties twice, which isn't needed. From the sample you sent, you should remove the Command property, and add show ver\r\n to your execute call.
Also, i don't see other code, but I assume that you left all the events there. Unforunately, the sample uses events, and is designed to run in non-blocking mode. Since you use blocking mode, you shouldn't be using events, since that could cause exactly the problem you are experiencing.
I would strongly suggest that you start a new Windows Application Project, and then add that code (modified with suggestions that I provided).
Hope I helped.
Regards,
Damba