vbscript – auto login via telnet connection to Cisco device

Due to the fact that Cisco IOS 12.X is not capable of an SSH key authentication well an other method is needed. Well, telnet does not support any kind of authentication except for Username / Password.

Now the following script emulates all commands that are necessary to connect to a Cisco / Telnet device:

Option Explicit
On Error Resume Next
' Define variables
Dim WshShell
Dim MyDateTime
Dim strDate 
Dim strDay 
Dim strMonth 
Dim strYear
Dim strTime
Dim strSecond
Dim strHost
Dim strLogFile

' define target host and log file location
strHost = "10.0.0.2" 
strLogFile = "c:\tmp\"

' Generate the timestap for the logfile
Function GetFormattedDate
strDate = CDate(date)
strDay = DatePart("d", strDate)
strMonth = DatePart("m", strDate)
strYear = DatePart("yyyy", strDate)
If strDay < 10 Then
strDay = "0" & strDay
End If
If strMonth < 10 Then
strMonth = "0" & strMonth
End If
strTime = FormatDateTime(time(), 4)
strTime = replace(strTime,":","-")
strSecond = Second(Time)
If strSecond < 10 Then
strSecond = "0" & strSecond
End If
strTime = strTime & "-" & strSecond
GetFormattedDate = strYear & "-" & strMonth & "-" & strDay & "-" & strTime
End Function

' Open a command shell
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 define log file'
WshShell.SendKeys "telnet " & strHost & " -f " & strLogFile & "telnet-" & strHost & "-" & GetFormattedDate & ".log"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
'Step 2 - Issue Commands with pauses'
' Send username
WshShell.SendKeys "admin"
WScript.Sleep 1000
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
' Send password
WshShell.SendKeys "cisco"
WScript.Sleep 1000
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
' send commands to shell and 
WshShell.SendKeys "sh crypto session"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
'Step 3 - Exit Command Window
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Quit

This is quite a simple script that does not have and fault tolerance. Further a user needs to be logged into a system to run this script.

The best way is to upgrade to IOS Version 15 (if possible) and use SSH pre shared authentication. SSH is capable to be scripted and run “invisible”.

1 comment for “vbscript – auto login via telnet connection to Cisco device

Leave a Reply

Your email address will not be published. Required fields are marked *