Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > General

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 12-16-2014, 12:31 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
Thumbs down For Each or Do Until----Help Please

I am building a script that can detect if a current modem is in use vs. what is open. However, I don't know what conditional loop to use?

I.E
I have a modem farm, and have currently in use.

router#sh users
Line User Host(s) Idle Location
tty 1/4 idle 00:00:06 modem5
tty 1/6 idle 00:02:13 modem5
tty vty 1 User modem5 00:00:10 IPaddr
tty vty 2 User modem7 00:02:14 IPaddr
*324 con User idle 00:00:00 IPaddr
tty con USer idle 00:01:23 IPaddr

Interface User Mode Idle Peer Address

router#

This is what i have so far. I get into an infinite loop with the following code. Anyone got a good idea of how I can pick a modem that's is open and not in use?

Sub Main()

' turn on synchronous mode
crt.Screen.Synchronous = True

crt.Screen.Send "show users" & chr(13)

Dim n
n=""

Dim modem_inuse(10)
modem_inuse(0) = "idle"
modem_inuse(1) = "modem1"
modem_inuse(2) = "modem2"
modem_inuse(3) = "modem3"
modem_inuse(4) = "modem5"
modem_inuse(5) = "modem6"
modem_inuse(6) = "modem7"
modem_inuse(7) = "modem8"
modem_inuse(8) = "modem15"
modem_inuse(9) = "modem16"

For Each modem in modem_inuse

Do Until n = "modem_found"

Dim Host_Inuse

Host_Inuse = crt.Screen.WaitForStrings(modem_inuse(0), 10)
'Tested this out as well, and doesn't work.
'Host_Inuse = crt.Screen.WaitForStrings("idle", "modem1", "modem2", "modem3" , "modem5", "modem6", "modem7", "modem8", "modem15", "modem16", 10)
Select Case Host_Inuse
Case 0
MsgBox "Timed out waiting for strings"
Exit Do
Case 1
MsgBox "Found idle string!"
Case 2
MsgBox "Found modem1 string!"

Case 3
MsgBox "Found modem2 string!"

Case 4
MsgBox "Found modem3 string!"

Case 5
MsgBox "Found modem5 string!"

Case 6
MsgBox "Found modem6 string!"

Case 7
MsgBox "Found modem7 string!"

Case 8
MsgBox "Found modem8 string!"

Case 9
MsgBox "Found modem15 string!"

Case 10
MsgBox "Found modem16 string!"

Case 11
MsgBox "All Modems are in use!"

End Select

'Exit Do

Loop

Next

End Sub
  #2  
Old 12-16-2014, 12:58 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
Do I need to have this question moved?

I am new to the support forum, and was browsing around. I just found that there is a scripting section. Should i try to move this post there?

I apologize for not doing my due diligence first.

Thanks

jbd1300
  #3  
Old 12-16-2014, 02:03 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi jbd1300,

Thanks for the post. It seems like we are missing some of the equation.

I have no knowledge of your modem farm, but it seems like you don't just need to find the string "modem<x>". You need to find the string, and determine if the modem is idle or busy.

I think that I would read in the output of the "sh users" command, and then parse the data using a loop combination of some sort.

Is my understanding of your goal accurate?

What is the version of SecureCRT that you are using?

Also, you are welcome to keep this thread here. In the future you may consider posting scripting questions in the scripting forum.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
  #4  
Old 12-16-2014, 03:42 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
For Each or Do Until----Help Please

I never thought about testing on both the string and if it's idle or busy. If it shows up in show users, then it has to be in use, or the line is in use. That's the logic I was using at least.. . Yes, I need to read in the show users output, however, didn't want to read it into a log or file, but real time. So, as the command is run and displays the output, the script finds an open modem, and opens a line...such as "connect modem5" for example.

Yes, I would like to parse with a loop, but can't wrap my head around which loop to use, and what the syntax would look like.

I am using Version 6.7.4

Thank you!

jbd1300
  #5  
Old 12-16-2014, 03:43 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi jbd1300,

Thanks for the update.

If the modem is not in the output, then it is not in use?

Do you just need to know the first modem not in use, or all modems not in use?
__________________
--Todd

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

Last edited by rtb; 12-16-2014 at 03:45 PM.
  #6  
Old 12-16-2014, 04:38 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
For Each or Do Until----Help Please

Correct..if the modem is not in the list, then it's open and not in use.

Yes, based on all the modems in the array, if it can find an open one, that would be exactly what i need. It doesn't necessarily have to be the first one, but just an open one. I would assume that the script would need to know all modems in order to look. I have a total of 9 modems to choose from, and which ever one i can find not in the list, that would be it.

modem_inuse(1) = "modem1"
modem_inuse(2) = "modem2"
modem_inuse(3) = "modem3"
modem_inuse(4) = "modem5"
modem_inuse(5) = "modem6"
modem_inuse(6) = "modem7"
modem_inuse(7) = "modem8"
modem_inuse(8) = "modem15"
modem_inuse(9) = "modem16"

Thanks

jbd1300
  #7  
Old 12-16-2014, 05:50 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi jbd1300,

Thanks for the confirmation. One of the problems you will encounter is duplication. The string "modem1" will always match "modem1", "modem15" and "modem16". I encountered this when I was running tests.

In this type of scenario, a dictionary and regular expressions lend themselves nicely to the solution.

I am working on testing an example. I will post it when it is finished.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
  #8  
Old 12-17-2014, 10:17 AM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
For Each or Do Until----Help Please

Thank you!
  #9  
Old 12-29-2014, 11:32 AM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
For Each or Do Until----Help Please

Todd,

Just following up, and see if you had a chance to post the example script for the modem farm.

Thanks

jbd1300
  #10  
Old 12-29-2014, 12:02 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,109
Hello jbd1300,

Todd is not presently available.

I am not sure of the status of the example script he mentioned, but there is info on Using Regular Expressions to Parse Information in the scripting manual on our website here (see Section 4.3).

You can also find info on regular expressions (and regular expression testers) by searching the internet.

Using dictionaries is maybe not as common and was not covered in the scripting manual. Since VBScript is a Microsoft product, you might see their documentation on the topic here.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
  #11  
Old 12-29-2014, 04:18 PM
Casey Casey is offline
Registered User
 
Join Date: Oct 2011
Posts: 114
Small question... Does using the 'show line' command not tell you what you need to know?

I find your question intriguing, but I don't fully understand what you are after. I'd like to know more.
  #12  
Old 12-30-2014, 12:45 PM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
bgagnon,

That's fine, I will wait for Todd to get back, so I can take a look at his example script. I am assuming he might be out for the holidays.

Casey,

I went that route initially, but here is the issue. Show line will show you what line is in use by an asterisk, however, then you would need to translate whatever line is in use translate into a TCP Port number. For example:

Tty Line Typ Tx/Rx A Modem Roty AccO AccI Uses Noise Overruns Int
0 0 CTY - - - - - 0 0 0/0 -
1 1 AUX 9600/9600 - - - - - 0 0 0/0 -
1/0 66 TTY 115200/115200- inout - - - 59 0 0/0 -
1/1 67 TTY 115200/115200- inout - - - 26 0 0/0 -
1/2 68 TTY 115200/115200- inout - - - 70 0 0/0 -
1/3 69 TTY 115200/115200- inout - - - 0 0 0/0 -
* 1/4 70 TTY 115200/115200- inout - - - 71 0 0/0 -

The asterisk is showing line 70 is in use, but that actually translates to TCP Port 2070, and then if multiple lines are in use, you would have to track that. I didn't want to keep up with all the lines, asterisks, and TCP Ports, so that's why I am using the "show users" command. That will tell you which hosts are in use. Todd was working on an example script, which I believe and hope he will post soon since I am stuck.

Thank you

jbd1300
  #13  
Old 01-05-2015, 07:28 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 952
Todd's not yet available, so I'll jump in and provide you with some pointers.

The first thing you'll want to do is set up a dictionary (AKA un-itemed collection or hash-table) object and populate it with the universe of known modem names that you could ever expect to see in the output of the command you're running (including any modems that might not show up in the output, but still exist and therefore might be available). For example:
Note that none of the example code segments are really "PHP code" in spite of the forum-provided label; it's simply easier to do some basic syntax highlighting using that built-in mechanism.
PHP Code:
Set cModems CreateObject("Scripting.Dictionary")
' We do not care what is in the dictionary as items/values, we are         '
' just "keying" in on the entries themselves due to their name             '
cModems.Add "modem1"""
cModems.Add "modem2"""
cModems.Add "modem3"""
cModems.Add "modem4"""
cModems.Add "modem5"""
cModems.Add "modem6"""
cModems.Add "modem7"""
cModems.Add "modem8"""
cModems.Add "modem9"""
cModems.Add "modem10"""
cModems.Add "modem11"""
cModems.Add "modem12"""
cModems.Add "modem13"""
cModems.Add "modem14"""
cModems.Add "modem15"""
cModems.Add "modem16"""
cModems.Add "modem17"""
cModems.Add "modem18"""
cModems.Add "modem19"""
cModems.Add "modem20"""
'...' 
Once you have your collection/dictionary/hash-table of possible modems that you know exist on the system, you then issue the command that generates the output showing which modems are being used in any way. For example:
PHP Code:
crt.Screen.Synchronous True
crt
.Screen.IgnoreEscape True

crt
.Screen.Send "sh users" vbcr 
Then, you use the crt.Screen.ReadString() method to read all the output up to the shell prompt "router#", as in:
PHP Code:
strOutput crt.Screen.ReadString("router#"
Now that you have all the output of the command stored in the variable named strOutput, you then set yourself up a regular expression object and establish a pattern that searches through the output (contained in the variable strOutput) for any evidence of modems identified by the pattern: "\smodem\d+[\s\r\n]". In English this pattern would read as, "any whitespace character (\s) followed by the word 'modem' (modem) followed by one or more digits (\d+) followed by any whitespace or end of line chars ([\s\r\n])."

Here's how you set up the regular expression object in VBScript:
PHP Code:
Set re = New RegExp
re
.MultiLine True
re
.Global = True
re
.IgnoreCase True
re
.Pattern "\s(modem\d+)[\s\r\n]" 
Here's how to test whether or not there are any matches to the pattern in the output at all:
PHP Code:
If re.Test(strOutputThen
    
' Get the matches and do something with them...                     '
Else
    
' Report that all modems are available (since no matches were found '
    ' in the output)...                                                 '
End If 
And, here's how you would iterate through all the matches found, getting the sub-matches (the part between the parens) as the name of any modems that appear within the output, and removing them from the collection since you know they're in use and aren't available:
PHP Code:
    Set objMatches re.Execute(strOutput)
    For 
Each objMatch In objMatches
        
' Get the sub-match (text between the parens in the regexp "Pattern")    '
        
strModemFound objMatch.Submatches(0)
        If 
cModems.Exists(strModemFoundThen
            cModems
.Remove(strModemFound)
        
End If
    
Next 

Once you are done iterating over all the matches found in the output and removing any you did find from your collection/dictionary/hash-table, the only keys left in your dictionary/collection/hash-table will be names of modems that are not being used.

Then, you could get access to simply the first one in the collection...
PHP Code:
crt.Dialog.MessageBox cModems.Keys()(0
The above looks weird, but Keys() simply returns an array, and the (0) gets the first item in the array that's returned by Keys().
...or you could show a message box listing all available modems...
PHP Code:
crt.Dialog.MessageBox Join(cModems.Keys(), vbcrlf
...you could even get fancy, if you like and display information about how many modems were found to be in use vs. not used, etc.:
PHP Code:
nModemsUsed 0
strModemsInUse 
""
If re.Test(strOutputThen
    Set objMatches 
re.Execute(strOutput)
    For 
Each objMatch In objMatches
        
' Get the sub-match (text between the parens in the regexp "Pattern")    '
        
strModemFound objMatch.Submatches(0)
        If 
cModems.Exists(strModemFoundThen
            cModems
.Remove(strModemFound)
            
nModemsUsed nModemsUsed 1
            strModemsInUse 
strModemsInUse strModemFound vbcrlf
        End 
If
    
Next
End 
If

If 
nModemsUsed 0 Then
    strMsg 
"All modems are available... no modems were found to be in use!"
ElseIf cModems.Count 1 Then
    strMsg 
"There are no modems available. Sorry"
Else
    
strMsg "The following " nModemsUsed " modems are currently being used: " vbcrlf _
    strModemsInUse 
vbcrlf _
    
"The following " cModems.Count " modems are available:" vbcrlf Join(cModems.Keys(), vbcrlf)
End If

crt.Dialog.MessageBox strMsg 
Sky's the limit.

Enjoy!
--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
  #14  
Old 01-06-2015, 10:44 AM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
For Each or Do Until----Help Please

Nice job!!!

I will implement this in the code, thank you for the example!

This is exactly what I am looking to do! I have never used a dictionary nor regex's before, and looks like I can expand this code for other network devices that I use.

Thanks

jbd1300
  #15  
Old 01-07-2015, 11:01 AM
jbd1300 jbd1300 is offline
Registered User
 
Join Date: Dec 2014
Posts: 18
Smile

Jake, Todd, and Brenda,

Thank you for the help, I implemented these examples in my production script, and it works flawlessly!!!

I can't thank you enough for the guiding and examples.

Have a great New Year, and Happy 2015!

Thanks

jbd1300
Closed Thread

Tags
#auto-select , #conditionalloops


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 11:27 PM.