#1
|
|||
|
|||
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 Code:
CPU2:ibcx1105vm002ssc001:~# uname -n ibcx1105vm002ssc001 CPU2:ibcx1105vm002ssc001:~# Code:
ibcx1105vm002ssc001 □]□[0mCPU2:ibcx1105vm002ssc001 Please advise. Bjoern |
#2
|
|||
|
|||
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. |
#3
|
|||
|
|||
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 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 |
#4
|
|||
|
|||
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. |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|