Oct 19, 2011

Automated Telnet Commands with VB script

Here's a VB script that can automate simple Telnet commands. Below is what happens during the telnet session in manual way:

  • Connect to an IP address on specific TCP port.
  • Press [ Enter ] to login.
  • Input the numeric key "5" and follow by an [ Enter ] key.
Below is the VB script that can automate the steps above:

<job>
<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000
'Send commands to the window as needed - IP and commands need to be customized
'Step 1 - Telnet to remote IP and port 99'
WshShell.SendKeys "telnet 192.168.1.1 99"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
 
'Step 2 - Issue Commands with pauses'
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
WshShell.SendKeys "5"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
 
'Step 3 - Exit Command Window
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Quit
</script>
</job>

>>>> Original article at MakeUseOf