VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   For Loop not running all commands (https://forums.vandyke.com/showthread.php?t=14321)

joshua.peterson 10-20-2020 06:34 PM

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

bgagnon 10-21-2020 08:08 AM

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".)

joshua.peterson 10-22-2020 04:09 PM

Thanks!

We switched to using the python version and it works great!

joshua.peterson 10-28-2020 06:26 PM

Thanks We used the python script from your link and it worked great!

bgagnon 10-29-2020 08:08 AM

Hi Joshua.peterson,

Great! Thanks for posting the update. :)


All times are GMT -6. The time now is 11:55 PM.