Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 10-20-2020, 06:34 PM
joshua.peterson joshua.peterson is offline
Registered User
 
Join Date: Oct 2020
Posts: 3
For Loop not running all commands

Hi,

This is my first post. I am evaluating the software to see if it will be useful for us.

I have written a script that gathers the output from show commands. The script uses a for loop to run these commands against an array of IP addresses.

For testing, I am running the script against 3 IP addresses. The issue I am having is that the script only runs the first few show commands against the first two IP addresses in the array. But It always runs all the commands against the last IP.

I originally used crt.Screen.WaitForString "#" to detect when a command had finished. I had issues with this because our run file contains # in it. This leads the script to think that the output what finished prematurely. I created a system to capture the exact hostname of the device and use that to detect when a command finishes.

I have tried changing the order of the IP addresses. But the script always does the same thing. Only the last IP address in the array works as intended.

It is like the script is in a hurry and just skips the last few show commands. Then when it gets to the last IP it has plenty of time and runs all the commands.

Here is my script. Any insite would be greatly appreciated!

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

' Turn on synchronous mode while performing Send/Wait sequences
' so no input is missed.

crt.Screen.Synchronous = True

' Make a list of hosts you want to run the commands against.

Dim vHosts(3)
vHosts(0) = "10.0.0.1"
vHosts(1) = "10.0.0.2"
vHosts(2) = "10.0.0.3"

' This automatically generated script will login to each device listed above and
' run the commands listed.

For Each strHost In vHosts
If strHost = "" Then Exit For

' Make sure we are disconnected before attempting a connection
If crt.Session.Connected Then crt.Session.Disconnect

' Connect to the next host
' (be sure to change the username and password to use a valide service account)
crt.Session.Connect "/SSH2 /L onewhohasaccess /PASSWORD donttell " & strHost & " /AcceptHostKeys"

' Send vbCr command repeatedly to ensure the screen is not showing the login banner
'
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send vbCr
crt.Screen.WaitForString "#"

'write the devices hostname to a variable
'
nHostname = crt.Screen.Get(3,1,3,15)

' Choose the directory to save the logged file in and then
' Set the name of the log file name to "Hostname-YYYY-MM-DD-HH-MM.txt"
' In this script the directory specified is "C:\Network-Assessment"
'
Dim logfile
logfile = "C:\Network-Assessment\%H-%Y-%M-%D-%h-%m.txt"
crt.Session.LogFileName = logfile

' Enable logging and run show commands
'
crt.Session.Log True
crt.Screen.Send "terminal len 0" & chr(13)
crt.Screen.WaitForString nHostname
crt.Screen.Send "show run" & chr(13)
crt.Screen.WaitForString nHostname
crt.Screen.Send "show cdp neighbor detail" & chr(13)
crt.Screen.WaitForString nHostname
crt.Screen.Send "show version" & chr(13)
crt.Screen.WaitForString nHostname
crt.Screen.Send "show lldp neighbor detail" & chr(13)
crt.Screen.WaitForString nHostname

Next
Reply With Quote
  #2  
Old 10-21-2020, 08:08 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,474
Hi joshua.peterson,

Welcome!

Your objective sounds much like what is accomplished with this example script. It reads the hosts from a hosts file and the commands from a commands file.

Do you think that might work for you? (This way we don't have to "reinvent the wheel".)
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 10-22-2020, 04:09 PM
joshua.peterson joshua.peterson is offline
Registered User
 
Join Date: Oct 2020
Posts: 3
Thanks!

We switched to using the python version and it works great!
Reply With Quote
  #4  
Old 10-28-2020, 06:26 PM
joshua.peterson joshua.peterson is offline
Registered User
 
Join Date: Oct 2020
Posts: 3
Thanks We used the python script from your link and it worked great!
Reply With Quote
  #5  
Old 10-29-2020, 08:08 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,474
Hi Joshua.peterson,

Great! Thanks for posting the update.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 09:15 PM.