Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
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:
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

Last edited by rtb; 10-24-2012 at 11:32 AM.
Reply With Quote
  #2  
Old 10-24-2012, 11:47 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for posting the problem.

What version of SecureCRT are you using?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 10-24-2012, 12:04 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
I believe it 6.0 or later.
Reply With Quote
  #4  
Old 10-24-2012, 12:08 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
Its version 6.7.4
Reply With Quote
  #5  
Old 10-24-2012, 02:08 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
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)?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by jdev; 10-24-2012 at 05:28 PM.
Reply With Quote
  #6  
Old 10-25-2012, 05:14 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
Yes Sir, it the one I'm using.
how do I use the cnxnstring?
Reply With Quote
  #7  
Old 11-21-2012, 11:31 PM
ganeshfarande ganeshfarande is offline
Registered User
 
Join Date: Nov 2012
Posts: 1
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:

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

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 "#"
Reply With Quote
  #8  
Old 11-23-2012, 10:47 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #9  
Old 12-15-2012, 06:30 PM
kmusawi kmusawi is offline
Registered User
 
Join Date: Dec 2012
Posts: 2
Lightbulb 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)
Reply With Quote
  #10  
Old 12-15-2012, 08:21 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #11  
Old 12-16-2012, 03:59 PM
kmusawi kmusawi is offline
Registered User
 
Join Date: Dec 2012
Posts: 2
Thanks Brenda, will have a look at that.
Reply With Quote
Reply

Tags
log , router , textfile

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 08:19 PM.