View Single Post
  #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