Telnet SendToServer and VB SendKeys()

+1 vote
asked Apr 2, 2013 by tomkennedy (190 points)
edited Dec 3, 2013


I'm doing some automation and need to send the telnet session some keystrokes to log in and take me to a certain area. So, I can use the SendToServer method to send basic text and 'certain' function keys.

However, I need to send other keys, specifically the carriage return. I can do this by using SendKeys() in VB but I don't think that's the correct way to do it (I also think it's the root cause of some problems I'm having where the SendKeys() calls are being executed immediately when in fact the server may sometimes take 0.5 seconds to process them, thus any following code could be out of sync)

So, after all that, can I use the SendToServer method to send special keys such as carriage return and tab?

Thanks in advance Tom

1 Answer

+1 vote
answered Apr 2, 2013 by Lukas Matyska (56,670 points)
edited Dec 3, 2013
Best answer

Hello Tom,

yes, you can use the SendToServer method (actually it is the only reliable method), but you have to handle client-server communication process. It means you cannot just call the method five times in sequence, otherwise you encounter sync problem (the server is not capable of receiving so many keystrokes in such rapid way). You have to send one carriage return or tab, then wait for the server response (cursor move or prompt display).

TerminalControl class is not designed for scripting primarily (VirtualTerminal class is better for this), but it is still possible using the Process or Expect methods, but you have to switch TerminalControl from automatic processing mode to manual by calling the SetProcessingMode method.

Log in sequence can look like this:

terminal.Bind(New Telnet("host"))
terminal.Expect("gin: ", 9000) ' wait for login
terminal.SendToServer("user" & vbCr)
terminal.Expect("sword: ", 9000) ' wait for password
terminal.SendToServer("password" & vbCr)
commented Apr 3, 2013 by tomkennedy (190 points)
edited Apr 3, 2013

Thanks very much, that works great!

Cheers Tom