SecureCRT – Sending commands to a Cisco device with VBScript

Requires “commands.txt” and “hosts.txt”

#$language = "VBScript"
#$interface = "1.0"

Set shell = CreateObject("WScript.Shell")
Set env = shell.Environment("process")

Dim hostsFile
hostsFile = env("LAUNCHPADPATH") & "\scripts\SecureCRT-Send-Commands\hosts.txt"
Dim commandFile
commandFile = env("LAUNCHPADPATH") & "\scripts\SecureCRT-Send-Commands\commands.txt"
Dim logPath

logPath = env("USERPROFILE") & "\Desktop\"

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim LoginUserName 
Dim LoginUserPassword
Dim EnablePassword

Sub Main
  If ( Not fso.FileExists(hostsFile) ) Then
	MsgBox "Hosts file missing - " & hostsFile
	Exit Sub
  End If
    If ( Not fso.FileExists(commandFile) ) Then
	MsgBox "Dump file missing - " & commandFile
	Exit Sub
  End If
  If ( Not fso.FolderExists(logPath) ) Then
	MsgBox "Log file missing - " & logPath
	Exit Sub
  End If
  
  Set fileIOSHosts = fso.OpenTextFile(hostsFile, 1)
  Do While fileIOSHosts.AtEndOfStream <> True
	host = fileIOSHosts.ReadLine
	If ( len(host) > 6 ) Then
		On Error Resume Next
		crt.session.connect(" /SSH2 /ACCEPTHOSTKEYS " & host)
		On Error Goto 0
		if not ( crt.session.connected ) then
			MsgBox host & " Connection failed... Click OK to continue next switch. (Empty log created)"
			crt.Session.LogFileName = logPath & host & "_Connection_failed_%D-%M-%Y_%hh%mm%ss.txt"
			crt.Session.Log True
			crt.Session.Log False
		else
			crt.Session.LogFileName = logPath & host & "_%D-%M-%Y_%hh%mm%ss.txt"
			crt.Session.Log True
				Set fileDumpConf = fso.OpenTextFile(commandFile, 1)
				crt.Screen.Send "terminal length 0" & VbCr
				Do While fileDumpConf.AtEndOfStream <> True
					dump = fileDumpConf.ReadLine
					crt.Screen.Send (dump) & VbCr
				loop
			result = crt.Screen.WaitForStrings("config)# ", "config-if)# ", "config-if-range)# ", "config-mst)# ", "# ", 5)
			crt.Screen.Send "end" & VbCr
			crt.Screen.WaitForString "#"
			crt.Session.Log False
			crt.Screen.Synchronous = False
			crt.session.Disconnect
		End If
	End If
  Loop
  fileIOSHosts.Close
  MsgBox "Script execution finished, check log here: " & logPath
End Sub

Sauce

Leave a Reply

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