VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   crt.Screen.ReadString does not behave as expected (https://forums.vandyke.com/showthread.php?t=14593)

BjoernH 07-30-2021 09:58 AM

crt.Screen.ReadString does not behave as expected
 
Good day.


I'm trying to initialize a variable by capturing the hostname from a server:

Code:

Sub Main
    crt.Screen.Send "uname -n" & vbCr
    crt.Screen.WaitForString vbCr

    strResult = crt.Screen.ReadString(":~#")

    MsgBox strResult

End Sub

The SecureCRT session reads:
Code:

CPU2:ibcx1105vm002ssc001:~# uname -n
ibcx1105vm002ssc001
CPU2:ibcx1105vm002ssc001:~#

The strResult in the MsgBox shows this:
Code:

ibcx1105vm002ssc001
□]□[0mCPU2:ibcx1105vm002ssc001

instead of only the hostname (ibcx1105vm002ssc001).




Please advise.


Bjoern

gregg 07-30-2021 01:05 PM

Hi,

Try using:

Screen.IgnoreEscape = True

From the docs:

By default, ReadString will capture all data received from the remote, including escape sequences. To enable or disable the inclusion of escape sequences in the data captured by ReadString, set the Screen.IgnoreEscape property to false/true, respectively.

If the remote side is sending escape sequences and Screen.IgnoreEscape is set to true, ReadString will return the string "plug" when "p" was drawn in the upper left corner, "l" in the upper right corner, "u" in the bottom left corner, and "g" in the bottom right corner.

BjoernH 07-30-2021 02:53 PM

Thanks Greg.


I added the crt.Screen.IgnoreEscape = True, it got rid of the escape sequence characters, however, I still have two lines:

Code:

ibcx1105vm002ssc001
CPU2:ibcx1105vm002ssc001

when I only need the first line, not the prompt.


I managed to get it work with


strResult = crt.Screen.Get (1,6,1,24)

which did work, but if you have a better method, I'd welcome it.

Bjoern

gregg 07-31-2021 01:22 AM

Hi Bjoern-

So, it also looks like readstring reads until the characters defined in the call, in your case

crt.Screen.ReadString(":~#")

which is part of your command line prompt and so on the next line after the response.

I think if you change that to:

strResult = crt.Screen.ReadString(vbCr)

You'll have better results as it will read up until it gets to the new-line character, which should be just the text you are looking for. Though, it might also capture a trailing end-of-line character that will need to be stripped.

Code:

# $Language="VBScript"
# $Interface="1.0"

' https://forums.vandyke.com/showthread.php?p=55233

Function StripString(contents)
    contents = Replace(contents, vbCr, "")
    contents = Replace(contents, vbLf, "")
    contents = Trim(contents)

    StripString = contents
End Function

Function GetCommandResult(command)
    crt.Screen.Send command & vbCr
    crt.Screen.WaitForString vbCr

    GetCommandResult = StripString(crt.Screen.ReadString(vbCr))
End Function

Sub Main
    crt.Screen.Synchronous = True
        crt.Screen.IgnoreEscape = True

    strResult = GetCommandResult("uname -n")
    MsgBox "'" & strResult & "' is " & Len(strResult) & " characters"

    strResult = GetCommandResult("uname -a")
    MsgBox "'" & strResult & "' is " & Len(strResult) & " characters"

End Sub


And if you're feeling adventurous, here's a Python version of same.

Code:

# $language = "Python"
# $interface = "1.0"

def GetCommandResult(command):
    crt.Screen.Send(command + "\n")
    crt.Screen.WaitForString("\n")

    return crt.Screen.ReadString("\n").strip()

def main():
    crt.Screen.Synchronous = True
    crt.Screen.IgnoreEscape = True

    result = GetCommandResult("uname -n")
    crt.Dialog.MessageBox(
        "'%s' is %d characters" % (result, len(result))
    )

    result = GetCommandResult("uname -a")
    crt.Dialog.MessageBox(
        "'%s' is %d characters" % (result, len(result))
    )

main()



All times are GMT -6. The time now is 12:54 PM.