|
![]() |
|
Thread Tools | Rate Thread | Display Modes |
#1
|
|||
|
|||
Capture IP addresses from command output and launch multiple tabbed sessions
I have a script which shows the user a custom form. The form has a drop down list to select a pre-configured session for login.
After login the script runs a command which returns a list of IP addresses. I need to capture this list of IP addresses and launch tabbed sessions to each of the IP's. My problem happens at the point of capturing the list of IP's in the script. The script shows variable behavior and shows different result every time it is run. Below is the actual script. #$language = "VBScript" #$interface = "1.0" crt.Screen.Synchronous = False ' This automatically generated script may need to be ' edited in order to work correctly. Sub Main ' Get a reference to IE's Application object Set g_objIE = CreateObject("InternetExplorer.Application") g_objIE.Offline = True g_objIE.navigate "about:blank" ' This loop is required to allow the IE object to finish loading... Do crt.Sleep 100 Loop While g_objIE.Busy g_objIE.Document.body.Style.FontFamily = "Sans-Serif" strHTMLBody = _ "<font color=red><b>Select the Circle (CSM): </b></font>" & _ "<hr>" & _ "<b>CSM: </b>" & _ "<select name=CSM size=1 >" & _ "<option>Agra</option>" & _ "<option>Ahemdabad</option>" & _ "<option>Ambala</option>" & _ "<option>Banglore</option>" & _ "<option>Bhopal</option>" & _ "<option>Bihar</option>" & _ "<option>Chennai</option>" & _ "<option>Delhi</option>" & _ "<option>Guwahati</option>" & _ "<option>Hyderabad</option>" & _ "<option>Kolkata</option>" & _ "<option>Mumbai</option>" & _ "<option>Nagpur</option>" & _ "<option>Rajasthan</option>" & _ "</select>" & _ "<br><br>" & _ "<input name=All type=""radio"" name=""nodes"" value=""All"" checked " & _ "Onclick=document.all(""ButtonHandler"").value=""Allclick"" > All" & _ " " & _ "<input name=MME type=""radio"" name=""nodes"" value=""MME"" " & _ "Onclick=document.all(""ButtonHandler"").value=""MMEclick"" > MME" & _ " " & _ "<input name=SAE type=""radio"" name=""nodes"" value=""SAE"" " & _ "Onclick=document.all(""ButtonHandler"").value=""SAEclick"" > SAE-GW" & _ "<hr>" & _ "<button name=OK AccessKey=O " & _ "Onclick=document.all(""ButtonHandler"").value=""OK"";" & _ "><u>O</u>K </button>" & _ " " & _ "<button name=Cancel AccessKey=C " & _ "Onclick=document.all(""ButtonHandler"").value=""Cancel"";" & _ "><u>C</u>ancel</button>" & _ "<input name=ButtonHandler value=""Nothing Clicked Yet"" " & _ "type=hidden >" ' Inject the HTML code above into the IE object g_objIE.Document.Body.innerHTML = strHTMLBody ' Prevent the MenuBar, StatusBar, AddressBar, and Toolbar from ' being displayed as part of the IE window g_objIE.MenuBar = False g_objIE.StatusBar = False g_objIE.AddressBar = False g_objIE.Toolbar = False ' Set the initial size of the IE window g_objIE.height = 200 g_objIE.width = 425 ' Set the title of the IE window g_objIE.document.Title = "Circle wise login" 'if not crt.Session.Connected Then g_objIE.Visible = True ' This loop is required to allow the IE window to fully display ' before moving on Do crt.Sleep 100 Loop While g_objIE.Busy ' This code brings the IE window to the foreground (otherwise, the window ' would appear, but it would likely be behind the SecureCRT window, not ' easily visible to the user). Set objShell = CreateObject("WScript.Shell") objShell.AppActivate g_objIE.document.Title ' Once the dialog is displayed and has been brought to the ' foreground, set focus on the control of our choice... g_objIE.Document.All("CSM").Focus 'end if Do ' If the user closes the IE window by Alt+F4 or clicking on the 'X' ' button, we'll detect that here, and exit the script if necessary. On Error Resume Next Err.Clear strNothing = g_objIE.Document.All("ButtonHandler").Value if Err.Number <> 0 then exit do On Error Goto 0 ' Check to see which buttons have been clicked, and address each one ' as it's clicked. Select Case g_objIE.Document.All("ButtonHandler").Value Case "Cancel" ' The user clicked Cancel. Exit the loop g_objIE.quit Exit Do Case "CLIclick" with g_objIE.Document.All("INH-MSG") .Disabled = False if NOT g_objIE.Document.All("CLI").Checked then .Checked = False .Disabled = True end if end with Case "OK" ' The user clicked OK. Act on the information in the ' Username and Password fields. ' Capture data from each field in the dialog... strCSM = g_objIE.Document.All("CSM").Value boolAll = g_objIE.Document.All("All").Checked boolMME = g_objIE.Document.All("MME").Checked boolSAE = g_objIE.Document.All("SAE").Checked g_objIE.quit ' Now that we have closed the IE dialog, we can act on our data. if not crt.Session.Connected Then crt.Session.Connect "/S " & strCSM crt.Screen.Synchronous = False end if crt.Screen.Send "cd /home/cnems/expscript" & chr(13) crt.Screen.WaitForCursor crt.Screen.Send "tr -cd '\11\12\15\40-\132\134-\176' < cmdoutput.txt | sed 's/^33m //g' | awk '{ print $4 }' | grep :" & chr(13) crt.Screen.WaitForString chr(13) 'This is where I face inconsistent result strResult = crt.Screen.ReadString("[") MsgBox strResult 'Sometimes I get the list of IP addresses, but other times I get blank or different result or sometimes, the script keeps waiting for response if boolCLI = True Then crt.Screen.WaitForString "] " crt.Screen.Send "cdbin" & chr(13) crt.Screen.WaitForString "] " crt.Screen.Send "CLI" & chr(13) crt.Screen.WaitForString "USERNAME : " crt.Screen.Send "EMSR" & chr(13) crt.Screen.WaitForString "PASSWORD : " crt.Screen.Send "EMSR" & chr(13) if boolINH = True Then crt.Screen.WaitForString "] " crt.Screen.Send "INH-MSG:TYPE=ALL;" & chr(13) crt.Screen.WaitForString "PASSWORD : " crt.Screen.Send "EMSR" & chr(13) end if end if Exit Do End Select ' Wait for user interaction with the dialog... crt.Sleep 200 Loop End Sub |
#2
|
|||
|
|||
Hi max!mus.
I see that in your script, as soon as you connect, you set crt.screen.synchronous to false. When you are using crt.Screen.Send and if Screen.WaitFor*() and screen.ReadString() calls are present, synchronous must be set to Tru as soon after connecting as possible. You'll want to always do a WaitForString or ReadString after each Send call. You can read more about this on page 39 of the VBScript scripting manual found on the following web page: https://www.vandyke.com/support/index.html Does setting synchronous to True after the connection and doing a Waitfor or ReadString after each send call improve the functionality? Thanks JJH |
#3
|
|||
|
|||
Hi jjh,
Thanks for the reply. I had been really busy so just got a chance to review your reply today. Anyway, I added a few MsgBox to understand what's going on and here's what I found. Let's look at the code, which is changed: 1. At the start of script, commented the synchronous statement. #$language = "VBScript" #$interface = "1.0" 'crt.Screen.Synchronous = True '<< note i tried uncommenting this line and got same result. 2. Following is only part changed in the main body: if not crt.Session.Connected Then crt.Session.Connect "/S " & strCSM 'crt.Screen.Synchronous = True '<< same here, tried uncommenting with same result. end if crt.Screen.Synchronous = True '<< finally sticking to putting it here! crt.Screen.Send "cd /home/cnems/expscript" & chr(13) if crt.Screen.WaitForString ("] ", 5) Then MsgBox "Found ""] """ '<< This Msgbox is received as expected else MsgBox "Timed out waiting for ""] """ end if crt.Screen.Send "tr -cd '\11\12\15\40-\132\134-\176' < cmdoutput.txt | sed 's/^33m //g' | awk '{ print $4 }' | grep :" & chr(13) if crt.Screen.WaitForString (chr(13), 5) then MsgBox "Found ""chr(13)""" '<< This Msgbox is received before i could see the corresponding command fired on CLI, only when connect is called i.e. securecrt is not already connected strResult = crt.Screen.ReadString("[", 5) MsgBox strResult '<< strResult returns empty in this case. else MsgBox "Times out waiting for ""chr(13)""" end if When connect method gets called, i.e. securecrt is not already connected, then the strResult returns an empty string. It seems synchronous = true is not really doing what it says in this case. Another strange thing. I added couple of more MsgBox as follows, to monitor the change in Synchromous = true: if not crt.Session.Connected Then crt.Session.Connect "/S " & strCSM 'crt.Screen.Synchronous = True '<< same here, tried uncommenting with same result. end if strSync = crt.Screen.Synchronous MsgBox strSync 'here strSync showed 0, on execution crt.Screen.Synchronous = True strSync = crt.Screen.Synchronous Msgbox = strSync 'here strSync showed -1, on execution And This script with additional msgbox worked exactly as I want it too. I.e. even when connect is called I do get correct list of IP addresses in strResult. Although the other msgbox show up just the same as before. Finally, I removed the MsgBox before setting synchronous = true, so this code looks like. if not crt.Session.Connected Then crt.Session.Connect "/S " & strCSM 'crt.Screen.Synchronous = True '<< same here, tried uncommenting with same result. end if 'strSync = crt.Screen.Synchronous 'MsgBox strSync crt.Screen.Synchronous = True strSync = crt.Screen.Synchronous Msgbox = strSync 'here strSync showed -1, on execution And again I cannot get the list of IP addresses in strResult when connect gets called. It's strange. Do you have a solution? Thanks max!mus |
#4
|
|||
|
|||
Hi max!mus.
I would recommend removing the timeouts from your script in case you're not allowing enough time. If your script hangs, cancelling your script will show you where the script is hanging, which will help with troubleshooting. Also, when you issue a command, you might want to get into the habit of waiting for the string that you sent to appear on the screen as it is echoed back before you actually send it with a carriage return. So you would do the following: strCmd="anything" crt.Screen.Send(strCmd) crt.Screen.WaitForString(strCmd) crt.Screen.Send(vbcr) crt.Screen.WaitForString(prompt) Does that method work better for you? JJH |
#5
|
|||
|
|||
![]() Quote:
This did solve my problem!! Thanks so much. Still under testing, but so far so good. removed the extra timers (they were not in my original code as is, only added them for TS) and used your above mentioned suggestion for ReadString. Max! |
#6
|
|||
|
|||
Hi max!mus.
Thanks for the update. I'm glad that helped. JJH |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|