#$language = "Python" #$interface = "1.0" # RevealNonPrintingCharacters.py # # Last Modified: # 23 Dec, 2022 # - Reviewed/Tested for Python 2/3 compatibility # # Description: # Shows one way to reveal what non-printing characters are in a string (for # example a string that was read in from the remote that might contain # CR, LF, and other non-printing characters and escape sequences) # # This example illustrates running a command and revealing non-printing # characters in the command output received by SecureCRT. # #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def RevealNonPrintingCharacters(strText): # Get a handle to a Dictionary object. colCharNames = dict() colCharNames[0] = '[NUL]' colCharNames[1] = '[SOH]' colCharNames[2] = '[STX]' colCharNames[3] = '[ETX]' colCharNames[4] = '[EOT]' colCharNames[5] = '[ENQ]' colCharNames[6] = '[ACK]' colCharNames[7] = '[BEL]' colCharNames[8] = '[BS]' colCharNames[9] = '[HT]' colCharNames[10] = '[LF]' colCharNames[11] = '[VT]' colCharNames[12] = '[FF]' colCharNames[13] = '[CR]' colCharNames[14] = '[SO]' colCharNames[15] = '[SI]' colCharNames[16] = '[DLE]' colCharNames[17] = '[DC1]' colCharNames[18] = '[DC2]' colCharNames[19] = '[DC3]' colCharNames[20] = '[DC4]' colCharNames[21] = '[NAK]' colCharNames[22] = '[SYN]' colCharNames[23] = '[ETB]' colCharNames[24] = '[CAN]' colCharNames[25] = '[EM]' colCharNames[26] = '[SUB]' colCharNames[27] = '[ESC]' colCharNames[28] = '[FS]' colCharNames[29] = '[GS]' colCharNames[30] = '[RS]' colCharNames[31] = '[US]' # The Range() function takes 3 arguments with the 2nd and 3rd arguments as # optional. If you use two arguments, the range will begin with the first # argument and is not inclusive of the second argument. In the statement # below we want 32 through 126, so we provide a second argument of 127. for nIndex in range(32, 127): colCharNames[nIndex] = chr(nIndex) # Delete is a special case: colCharNames[127] = '[DEL]' # Now, let's get to the "revealing"... strRevealingText = '' for nPos in range(0, len(strText)): # Get the current character (we're working left->right from the first # character of the string to the last character of the string): strCurrentChar = strText[nPos] # Map the current character to a printable sequence using either the # actual character (if printable), or a substitute as defined in the # colCharNames collection above: if ord(strCurrentChar) in colCharNames: strRevealingText += colCharNames[ord(strCurrentChar)] else: strRevealingText += strCurrentChar # Tidy things up a bit so that they'll appear more "normalized" in the # messagebox strRevealingText = strRevealingText.replace('[CR][LF]', '[CRLF]\r\n') strRevealingText = strRevealingText.replace('[CR]', '[CR]\r\n') strRevealingText = strRevealingText.replace('[LF]', '[LF]\r\n') strRevealingText = strRevealingText.replace('[HT]', '[HT]\r\n') # Return the data to the caller. return strRevealingText #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def Main(): strPrompt = '$' crt.Screen.Synchronous = True crt.Screen.Send('ls -l --color\r') strReceivedData = crt.Screen.ReadString(strPrompt) crt.Dialog.MessageBox(RevealNonPrintingCharacters(strReceivedData)) Main()