See the troubleshooting / FAQs chapter when you have problems with your programs.

Example 1: Windows XP Telnet server

Connect to the MS Windows XP Telnet server, login, and receive directory information. See console output below.

...
Terminal tn = new Terminal("giga", 23, 10, 80, 40); // hostname, port, timeout [s], width, height


tn.Connect(); // physical connection do { f = tn.WaitForString("Login"); if (f==null) break; // this little clumsy line is better to watch in the debugger Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd()); tn.SendResponse("telnet", true); // send username f = tn.WaitForString("Password"); if (f==null) break; Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd()); tn.SendResponse("telnet", true); // send password f = tn.WaitForString(">"); if (f==null) break; tn.SendResponse("df", true); // send Shell command if (tn.WaitForChangedScreen()) Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd()); } while (false); tn.Close(); // physically close on TcpClient ...

image

  • newTerminal: Creates a new Terminal client object. No physical connection will be established, only the object will be created in the memory. The parameters read: server's hostname, port (usually 23), timeout in seconds, width, height. It is important the screen size is big enough to capture the data, 80x40 is a good size to start with.
  • object.Connect() opens a physical connection while, object.Close()closes this connection.
  • The WaitFor..("string") methods are the key methods of the whole application. These methods come in different flavours, but primarily do all the same: They wait a certain time [timeout, see new Telnet(..)] for a particular string or regular expression. If the search operation is successful the search result will be returned, otherwise null. There are different overloaded methods where the search behaviour or the timeout can be explicitly specified [see also other examples].
  • This returns the content of the virtual screen buffer as string - since the application behaves very much as a virtual screen I've use the method name Hardcopy().
  • Another key method: We send a response back to the server, the true means we terminate the line with "\r\n".
  • This is very similar to the WaitForString() method, only this waits for the completion of the server output (or a timeout) instead of a specified string.

Example 2: Router Netgear RT311

Connect to a Netgear RT311 router, login, walk through the menus, and determine the external IP-address. See console output below. An (crude?) alternative to the WaitForString(..) methods would be the usage of Wait(int). See comment in source code.

...
Terminal tn = new Terminal("router", 23, 10, 80, 40); // hostname, port, timeout [s], width, height
tn.Connect(); // physcial connection
do 
{
	f= tn.WaitForString("Password");
	if (f==null) 
		throw new TerminalException("No password prompt found");
	Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
	tn.SendResponse("1234", true);	// send password
	f = tn.WaitForString("Enter");
	if (f==null) 
		throw new TerminalException("No 1st menu screen found");


/** A L T E R N A T I V E **/ /** tn.Wait(2); // simply wait 2 seconds would also work **/ Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd()); tn.SendResponse("24", true); // send "24" to get to next menu f = tn.WaitForString("Enter"); if (f==null) throw new TerminalException("No 2nd menu screen found"); Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd()); tn.SendResponse("1", true); // send "1" to get to next menu Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd()); f = Telnet.FindIPAddress(tn.WaitForRegEx(@"WAN.+?(\d?\d?\d\.\d?\d?\d\.\d?\d?\d\.\d?\d?\d)")); // search for 1st IP-like address next to "WAN" if (f==null) throw new TerminalException("No IP address screen found"); Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd()); Console.WriteLine("\n\nEXTERNAL IP " + f); } while (false); tn.Close(); // physically close on TcpClient ...

  • TerminalException: Custom exceptions may be used to distinguish between empty (null) strings and connectivity problems.

Example 3: Linux RH 7.3 Telnet server

Connect to a RedHat 7.3 box, login, and display the disk usage. This is similar to example 1. See console screenshot below for output.

...
Terminal tn = new Terminal("linux", 23, 10, 80, 40); // hostname, port, timeout [s], width, height
tn.Connect(); // physcial connection
do 
{
	f = tn.WaitForString("Login");
	if (f==null) 
	break; // this little clumsy line is better to watch in the debugger
	Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
	tn.SendResponse("kba", true);	// send username
	f = tn.WaitForString("Password");
	if (f==null) 
		break;
	Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
	tn.SendResponse("vmware", true);	// send password 
	f = tn.WaitForString("$");	// bash
	if (f==null) 
		break;
	tn.SendResponse("df", true);	// send Telnet
	if (tn.WaitForChangedScreen())
		Console.WriteLine(tn.VirtualScreen.Hardcopy().TrimEnd());
} while (false);
tn.Close(); // physically close on TcpClient
...

Special keys

image

Last edited Jul 22, 2012 at 9:58 PM by KWB, version 16

Comments

No comments yet.