VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Logging Device from script and save to text file (https://forums.vandyke.com/showthread.php?t=10724)

stuartX 10-24-2012 11:25 AM

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:
Device1;10.10.10.1;Telnet;Test1
Device2;10.10.10.2;Telnet;Test2
Device3;10.10.10.3;Telnet;Test3


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.

code:
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
        Else
       
            ' 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
                crt.Session.Disconnect
            End If
        End If
    Wend
   
    ' Close device file
    fil.Close
End Sub


rtb 10-24-2012 11:47 AM

Hi stuartX,

Thanks for posting the problem.

What version of SecureCRT are you using?

stuartX 10-24-2012 12:04 PM

I believe it 6.0 or later.

stuartX 10-24-2012 12:08 PM

Its version 6.7.4

rtb 10-24-2012 02:08 PM

Hi stuartX,

Thanks for the data.

I don't see in your posted code where you define cnxnString.

I am not sure what your script is doing, but it seems unlikely that the code you posted is actually connecting to multiple remote devices.

As a note, if you pass empty data to the Connect method, the Connect method will just reconnect the last session that was connected in the tab where you are running the script. If you were to run your script in a fresh instance that has no previously connected session in a tab, I suspect you would find your script failing when it gets to the point where it needs to connect.

Would you confirm that you posted the exact script you are using (with the exception of sensitive data)?

stuartX 10-25-2012 05:14 AM

Yes Sir, it the one I'm using.
how do I use the cnxnstring?

stuartX 10-25-2012 05:49 AM

Actually that term 'cnxnString' is in the posted code. See the last line:



' Process a line at a time
vLine = Split(fil.ReadLine, ";")
strHostname = vLine(0)
strIP = vLine(1)
On Error Resume Next
crt.Session.Connect cnxnString

rtb 10-25-2012 07:40 AM

Hi stuartX,

Thanks for the updates. The last line in your most recent post is the line I am referencing. You cannot use cnxnString as a variable if you don't define it. Defining it could look something like the following if you had a session named the same as the value of strHostname:
Code:

cnxnString = "/S " & strHostname
I would suggest taking a look at chapter 3 of the scripting manual. It illustrates how you can use the Connect method in detail. Chapter 3.6 specifically illustrates how you can use a variable with the Connect method rather than using a string as is illustrated prior to chapter 3.6.

Does this information help you fix your script?

If you don't have a copy of the scripting manual already, you can download it from the following location:
http://www.vandyke.com/support/tips/...ing/index.html

ganeshfarande 11-21-2012 11:31 PM

Logging Device from script and save to text file and update in exel steste done / not
 
i want script for below mention command and update in exel file status done / not done i want scrip pls help me i have 3000 router :mad:mad:

ssh 192.168.5.1
password :- abc
en
password :- abc
copy running-config tftp:
Address or name of remote host []? 192.168.1.1
Destination filename
exit




i am using below scrip manual work but its so difficult :confused::confused::eek::eek::confused::confused::(:(:mad:

crt.Screen.Send "ssh 192.168.5.1" & chr(13)
crt.Screen.WaitForString "Password: "
crt.Screen.Send "cisco" & chr(13)
crt.Screen.Send "en" & chr(13)
crt.Screen.Send "cisco" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "copy running-config tftp:" & chr(13)
crt.Screen.WaitForString "Address or name of remote host []? "
crt.Screen.Send "192.168.1.1" & chr(13)
crt.Screen.WaitForString "Destination filename "
crt.Screen.Send chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "q" & chr(13)
crt.Screen.WaitForString "#"

bgagnon 11-23-2012 10:47 AM

Hello ganeshfarande,

I am not sure this is the appropriate thread for your post, but since the information I plan to share can already be found in other threads, I will leave it here.

As a note, we do not write custom scripts, but can assist if you need help with a cohesive task.

While you say the script example posted works, you are taking a risk in that you have three "sends" in a row, without any intervening "waitforstrings".

Section 4.2 of the Scripting Essentials manual available on our website states that best practice is that there should always be a
WaitForString() following each Send() command.

The above is probably a moot point, as it sounds like you want to replace that script anyway. :)

Is the connection information for your 3000 routers already in an Excel spreadsheet?

If so, see the example script in another thread, which I think will help you to accomplish your objective.

kmusawi 12-15-2012 06:30 PM

script to search for strings and output result in text
 
Hi,

I would like some direction on how to accomplish the following, i do not have prior experience in VB however I have done Java and C++ some years ago so I can learn VB with some direction.

I would like to be able to have a script prompt the user asking what interface to search for current status ( i.e. - interface ge-1/4/2/1 ) and to look up the shw interface command to check if the link state is up or down. If the state is down to then check the log (i.e.- shw log ) to check when the interface last went down and to output the results in a text file or on the current screen.

In essence i would like to have a generic script that can do the following :

1) check the current state of the interface (Link up or Link down)
2) check how long the interface has been up for
3) if link to a specific interface is currently down to grab the exact date/time it went down
4) If link to specific interface went down and then came back up within a period of less than 10min to grab these dates and times

Output the results of the above commands to the screen or text file.

I deal with both Juniper and Cisco routers so I am aware both use slighly different commands to get the specified result but really need help on how to overall accomplish the above as it requires to "check for specific strings ( is link down or up) and ( look for the date and times it went down and back up)

bgagnon 12-15-2012 08:21 PM

Hi kmusawi,

To familiarize yourself with common scripting concepts as it relates to SecureCRT, I recommend the Scripting Essentials manual available on our website.

Quote:

I would like to be able to have a script prompt the user asking what interface to search for current status ( i.e. - interface ge-1/4/2/1 ) and to look up the shw interface command to check if the link state is up or down. If the state is down to then check the log (i.e.- shw log ) to check when the interface last went down and to output the results in a text file or on the current screen.
In regards to what you are trying to accomplish, what mechanism would the user use to "look up the link state"?

If you are not adverse to using Excel, this post by Jake includes a script example that seems like it might be a good starting point.

kmusawi 12-16-2012 03:59 PM

Thanks Brenda, will have a look at that.


All times are GMT -6. The time now is 01:48 AM.