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-20-2012, 02:37 AM
jorelv jorelv is offline
Registered User
 
Join Date: Oct 2012
Posts: 3
Using a list of hostname aliases to make connections instead of the actual IPs

Using the SecureCRT Scripting Essentials Guide and the examples provided, I was able to create a basic script that, based on the information gathered by the user, logs into Cisco ONS 15454 and 15600 nodes and then constructs and runs TL1 commands to quickly retrieve performance monitor (PM) information on specific or all STS1, T3, OC3, OC12, OC48, or OC192 circuits. The script has been working great, but I think it could be improved upon before I share it with the rest of my department.

We have several hundred nodes, so instead of prompting the user for a host IP address (which has to be looked up manually), I would like for the user to simply enter the desired alias for the Cisco node, such as ABCDEFG-454-01 into the target host prompt, and then the script finds ABCDEFG-454-01 in a CSV file, and sees that it correlated to the IP 192.168.1.1 so it knows to connect to that IP. Saving the user the trouble of typing in the IP would really come in handy, and the node list, which would be located on a network drive, could be updated regularly when new nodes are added.

To reiterate, the CSV would look something like this:

ABCDEFG-454-01,192.168.1.1
ABCDEFG-454-02,192.168.1.2
ABCDEFG-454-03,192.168.1.3
HIJKLMNO-454-01,192.168.2.2
PQRSTUVW-454-01,192.168.3.3

And when the script asks for the strHost (IP address) I want the user to be able to simply input the node's alias ABCDEFG-454-01 instead. I want to this all without creating saved sessions for each node. I've tried searching these forums and working with the example scripts, but I'm new to VBScript and so far I haven't been able to make this new functionality work. Is there any way someone can assist?

Most of my department is using SecureCRT 7.0 in a Windows 7 environment. We're on a 31 Computer Right To Use License. Below is a copy of the current working script. Thank you!

Code:
#$language = "VBScript"
#$interface = "1.0"
Sub Main()

    crt.Screen.Synchronous = True

    strHost = crt.Dialog.Prompt(_ 
        "Please specify the host to connect to (Cisco 15454 or 15600):", _
        "Target Host?", _ 
"192.168.1.1")
    If strHost = "" Then Exit Sub

    strCircuit = crt.Dialog.Prompt(_ 
        "What type of circuit? (OC192, OC48, OC3, T3, or STS1) :", _
        "Circuit type?", _ 
"OC192")
    If strCircuit = "" Then Exit Sub
     
    strFacility = crt.Dialog.Prompt(_ 
        "What card and port (and STS if applicable)? You can also specify ALL", _ 
        "Facility?", _ 
"1-1-1")
    If strFacility = "" Then Exit Sub
     
    strDate = crt.Dialog.Prompt(_ 
        "Please enter the date that you want PMs pulled from. Today's date or yesterday in MM-DD format:", _ 
        "Date?", _ 
Month(Date) & "-" & Day(Date))

    crt.Session.Connect "/TELNET " & strHost & " 3083"
    
    crt.Screen.Send "act-user::USERNAME:CTAG::PASSWORD;" & vbCr

    crt.Screen.WaitForString "logged in from"

    ' Let's check to see if this is for ALL circuits of a type
    If uCase(strFacility) = "ALL" Then
         crt.Screen.Send "RTRV-COND-" & strCircuit & "::ALL:CTAG::;" & vbCr
         crt.Screen.Send "RTRV-ALM-" & strCircuit & "::ALL:CTAG::;" & vbCr
         crt.Screen.Send "RTRV-PM-" & strCircuit & "::ALL:CTAG::,,,,1-DAY," & strDate & ";" & vbCr
         crt.Screen.Send "RTRV-PM-" & strCircuit & "::ALL:CTAG::,,FEND,,1-DAY," & strDate & ";" & vbCr 
         crt.Screen.Synchronous = False
         Exit Sub 
    End If

    ' Let's check to see if this is a STS1. If it is we will want to check STS1 alarms, conditions, and PMs
    If uCase(strCircuit) = "STS1" Then
         crt.Screen.Send "RTRV-COND-STS1::STS-" & strFacility & ":CTAG::,,;" & vbCr
         crt.Screen.Send "RTRV-ALM-STS1::STS-" & strFacility & ":CTAG::,,;" & vbCr
         crt.Screen.Send "RTRV-PM-STS1::STS-" & strFacility & ":CTAG::,,,,1-DAY," & strDate & ";" & vbCr
         crt.Screen.Send "RTRV-PM-STS1::STS-" & strFacility & ":CTAG::,,FEND,,1-DAY," & strDate & ";" & vbCr  
         crt.Screen.Send "RTRV-CRS-STS1::STS-" & strFacility & ":CTAG:;" & vbCr  
         crt.Screen.Synchronous = False
         Exit Sub 
    End If

   ' Let's check alarms and conditions 
    crt.Screen.Send "RTRV-COND-" & strCircuit & "::FAC-" & strFacility & ":CTAG::;" & vbCr
    crt.Screen.Send "RTRV-ALM-" & strCircuit & "::FAC-" & strFacility & ":CTAG::;" & vbCr 

    ' Let's check to see if this is a T3. If it is we will want to check T3 and STS1 PMs
    If strCircuit = "T3" Then
         crt.Screen.Send "RTRV-PM-T3::FAC-" & strFacility & ":CTAG::,,,,1-DAY," & strDate & ";" & vbCr
         crt.Screen.Send "RTRV-PM-STS1::STS-" & strFacility & "-1:CTAG::,,,,1-DAY," & strDate & ";" & vbCr
         crt.Screen.Send "RTRV-PM-STS1::STS-" & strFacility & "-1:CTAG::,,FEND,,1-DAY," & strDate & ";" & vbCr  
         crt.Screen.Send "RTRV-CRS-STS1::STS-" & strFacility & "-1:CTAG:;" & vbCr  
         crt.Screen.Synchronous = False
         Exit Sub 
    End If

   ' Now we're left with just OC3's, OC12's, OC48's, and OC192's: let's check near-end and far-end errors for the date specified 
    crt.Screen.Send "RTRV-PM-" & strCircuit & "::FAC-" & strFacility & ":CTAG::,,,,1-DAY," & strDate & ";" & vbCr
    crt.Screen.Send "RTRV-PM-" & strCircuit & "::FAC-" & strFacility & ":CTAG::,,FEND,,1-DAY," & strDate & ";" & vbCr                 

    crt.Screen.Synchronous = False

End Sub
Reply With Quote
  #2  
Old 10-22-2012, 03:07 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,039
Hello jorelv,

Did you see the example script in SecureCRT Scripting Guide Solution: Read Data from Separate Files: Hosts, Commands?

I think you could modify the ReadDataFromFile() example function to get the host data from a file.

Does the host's alias not resolve through DNS (does the script need to find the corresponding IP address in the CSV file if there's a host alias match)?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 10-25-2012, 10:13 PM
jorelv jorelv is offline
Registered User
 
Join Date: Oct 2012
Posts: 3
Michael,

Thank you for your reply. I've used the example script Read Data from Separate Files: Hosts, Commands in the past to successfully connect to a list of hosts and run different commands. However, I'm still struggling to understand how to modify the examples to loop through and only connect to one of the hosts in a CSV file, based on what alias the user supplies.

I want the user to be prompted for what alias to connect to, and when they type "HIJKLMNO-454-01" the script will loop through the hosts CSV, find the alias HIJKLMNO-454-01, and see that it's supposed to go to 192.168.2.2, so the session will connect to 192.168.2.2

The does script need to find the corresponding IP address in the CSV file if there's a host alias match. There are no DNS resolutions on the host aliases. All of these hosts are only accessible from our internal network.
Reply With Quote
  #4  
Old 10-26-2012, 05:12 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,099
Quote:
Originally Posted by jorelv View Post
I'm still struggling to understand how to modify the examples to loop through and only connect to one of the hosts in a CSV file, based on what alias the user supplies.
Thanks for clarifying. You probably don't need all of the overhead associated with the example script from the guide. Instead, you'll want to learn about the "Dictionary" object that VBScript provides as a way of performing "lookup" operations based on key:value pairs.

Read in the data from the CSV file
One of the first things your script would do is load the alias & address data from the CSV file you want to use:
PHP Code:
''# Load data from CSV file
''#    alias1,192.168.1.1
''#    alias2,192.168.1.2
''#    alias3,192.168.1.3
''#    example alias 4,192.168.2.2
''#    example alias 5,192.168.3.3
Set objFSO CreateObject("Scripting.FileSystemObject")
Set objFile objFSO.OpenTextFile("C:\Temp\AliasDefinitions.csv"1False)
strCSVFileData objFile.ReadAll
objFile
.Close 
Populate a Dictionary object with the data you read in
After you have read in the data from the CSV file, you would iterate over each line of data, parse out the alias and address components, and plug the aliases into a dictionary object (dictionary is key:value pair) where the key is the alias and the value is the hostname or address that can be used to really make the connection. Some example code (untested... just here to present the idea; take it and run with it) to help you achieve this might look like this:
PHP Code:
''# Populate a dictionary object with the data read in:
Set cAliases CreateObject("Scripting.Dictionary")
For 
Each strLine In Split(strCSVFileDatavbcrlf)
    
vTokens Split(strLine",")
    
strAlias Trim(vTokens(0)) ' Parse out the alias
    strAddress = Trim(vTokens(1)) ' 
Parse out the IP address or hostname
    
    
''# Add alias to the collection if it's not already there.
    
''# Note: syntax is --->   dictionary.Add key, value
    
If Not cAliases.Exists(strAliasThen
        cAliases
.Add strAliasstrAddress
    End 
If

    
''# You probably want IP addresses to double as allowable "aliases", so
    
''# they should be taken into consideration here as well. Instead of
    
''# prompting for IP addresses alone, this allows you to prompt for
    
''# aliases and IP addresses alike (allowing the user to choose their
    
''# preference)...
    
If Not cAliases.Exists(strAddressThen
        cAliases
.Add strAddressstrAddress
    End 
If
Next 

Prompt for data from the user
Once your dictionary is populated with data, you would then display your prompt asking for alias/host specification.

Perform a "Lookup" on the alias
You check the dictionary (this is your "lookup") to see if the supplied alias/host exists as a key in the collection (another name for "dictionary") by doing something like this:
If nameOfMyDictionaryObject.Exists("Alias/Host I entered") Then ...

Map the alias to its value as stored in the dictionary object
You retrieve the value (in your case, the address/host) of the key very easily. For example:
strValue = nameOfMyDictionaryObject("Alias/Host I entered")
You'd likely want to do your prompt+lookup within in a loop -- repeating the prompt until either it's cancelled or until a valid alias is found to exist in the dictionary. Here's some more example (untested, unwarranteed) code that should give you the idea:

PHP Code:
    Do
        
strHost crt.Dialog.Prompt(
            
"Please specify the host/alias to connect to (Cisco 15454 or 15600):"_
            
"Target Host?"
            
"192.168.1.1")
        If 
strHost "" Then Exit Sub
        
        
If Not cAliases.Exists(strHostThen
            crt
.Dialog.MessageBox _
                
"Alias/Address '" strHost _
                
"' was not found in the collection of hosts/aliases defined in " _
                
"the CSV file."
        
Else
            
''# Convert the alias to its host value as found in the dictionary.
            
strAlias strHost
            strHost 
cAliases(strAlias)
            
strHostInfo strAlias " = " strHost
            
            
''# Exit the loop now that we have a match for the alias
            
Exit Do
        
End If
    
Loop

    strCircuit 
crt.Dialog.Prompt(_
        
"What type of circuit? (OC192, OC48, OC3, T3, or STS1) :"_
        
"Circuit type?  (" strHostInfo ")"
        
"OC192")
    If 
strCircuit "" Then Exit Sub 

Does this help increase your understanding to the point where you'd be able to move forward with a solution using a Dictionary object to implement your idea of performing "lookups"?


--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #5  
Old 10-27-2012, 03:27 AM
jorelv jorelv is offline
Registered User
 
Join Date: Oct 2012
Posts: 3
Thank you! This looks perfect. I will give it a try and let you know how it goes.
Reply With Quote
Reply

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 07:05 AM.