View Single Post
Old 10-24-2012, 11:25 AM
stuartX stuartX is offline
Registered User
Join Date: Feb 2012
Posts: 33
Logging Device from script and save to text file

I have a script with VBscript that reads the ip names/IP from a text file (IOS-Device.txt) and write to a text file in a directory (S:\Riggs\IOS Test directory).

The device text file (IOS Device.txt) contains the following:

This is what I want the script to do:
1. read device from file
2. log into device
3. perform commands
4. write file
5 go to next device and repeat steps.

What I see is that it accesses all the devices from the file and perform the commands, I can see this on the screen scrolling through. It also saves the different file names to the proper directory. But the problem is that even though the file names are different, each file contains the commands performed on the last device in the list. For example, the saved text files 1, 2 and 3 have the same command results from Device 3.

Below is my code.

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

Sub Main
    strUsername = "admin1" 'not real username
    strPassword = "getin" 'not real password
    strPrompt = ">"
    Set fso = CreateObject("Scripting.FileSystemObject")
    strDeviceList = "s:\riggs\IOS-Device.txt"
    Set fil = fso.OpenTextFile(strDeviceList)
    ' Start loop to iterate over all devices defined in file
    While Not fil.AtEndOfStream
        ' Process a line at a time
        vLine = Split(fil.ReadLine, ";")
        strHostname = vLine(0)
        strIP = vLine(1)
        On Error Resume Next
            crt.Session.Connect cnxnString
            strErr = Err.Description
            nError = Err.Number
        On Error Goto 0        
        If crt.Session.Connected = False Then 
            crt.Dialog.MessageBox "The following host cannot be reached: " &_
            vbcrlf & vbtab & strHostname & vbcrlf & vbcrlf &_
            "With error: " & vbcrlf & vbtab & strErr
            ' Enable Synchronous which is necessary to avoid missing data from
            ' remote
            crt.Screen.Synchronous = True
            crt.Screen.WaitForString "Username:"
            crt.Screen.Send strUsername & vbcr
            crt.Screen.WaitForString "Password:"
            crt.Screen.Send strPassword & vbcr

            ' Confirm that remote is ready
            crt.Screen.WaitForString strPrompt
            ' Start logging to new file
            strLogFile = "S:\Riggs\IOS Test\" & strHostname & "-%Y-%M-%D--%h-%m-%s.%t.txt"
            crt.Session.LogFileName = strLogFile
            crt.Session.Log True
            ' send deive command
            crt.Screen.Send "term length 0" & vbcr
            crt.Screen.Send "show ver" & vbcr	
            crt.Screen.WaitForString strPrompt
            ' Send device commands
            crt.Screen.Send "show proc cpu | inc CPU" & vbcr
            crt.Screen.WaitForString strPrompt
            crt.Screen.Send "show version" & vbcr 
            crt.Screen.WaitForString strPrompt
            crt.Screen.Send "show ip route summary" & vbcr 
            crt.Screen.WaitForString strPrompt
            ' Stop logging
            crt.Session.Log False
            ' Disconnect from device
            If crt.Session.Connected Then
            End If
        End If
    ' Close device file
End Sub

Last edited by rtb; 10-24-2012 at 11:32 AM.
Reply With Quote