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 07-30-2021, 09:58 AM
BjoernH BjoernH is offline
Registered User
 
Join Date: Mar 2018
Posts: 55
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
Reply With Quote
  #2  
Old 07-30-2021, 01:05 PM
gregg gregg is offline
Registered User
 
Join Date: Oct 2010
Posts: 75
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.
Reply With Quote
  #3  
Old 07-30-2021, 02:53 PM
BjoernH BjoernH is offline
Registered User
 
Join Date: Mar 2018
Posts: 55
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
Reply With Quote
  #4  
Old 07-31-2021, 01:22 AM
gregg gregg is offline
Registered User
 
Join Date: Oct 2010
Posts: 75
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()

Last edited by gregg; 07-31-2021 at 01:29 AM.
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 01:39 PM.