-1

Good afternoon. I get the example below from SSH:

b"rxmop:moty=rxotg;\x1b[61C\r\nRADIO X-CEIVER ADMINISTRATION\x1b[50C\r\nMANAGED OBJECT DATA\x1b[60C\r\n\x1b[79C\r\nMO\x1b[9;19HRSITE\x1b[9;55HCOMB FHOP MODEL\x1b[8C\r\nRXOTG-58\x1b[10;19H54045_1800\x1b[10;55HHYB"

I process ssh.recv (99999) .decode ('ASCII') but some characters are not decoded for example:

\x1b[61C
\x1b[50C
\x1b[9;55H
\x1b[9;19H

The article below explains that these are ANSI escape codes that appear since I use invoke_shell. Previously everything worked until it moved to another server. Is there a simple way to get rid of junk values that come when you SSH using Python's Paramiko library and fetch output from CLI of a remote machine?

When I write to the file, I also get:

rxmop:moty=rxotg;[61C
RADIO X-CEIVER ADMINISTRATION[50C
MANAGED OBJECT DATA[60C
[79C
MO[9;19HRSITE[9;55HCOMB FHOP MODEL[8C
RXOTG-58[10;19H54045_1800[10;55HHYB

If you use PuTTY everything is clear and beautiful. I can't get away from invoke_shell because the connection is being thrown from one server to another. Sample code below:

# coding:ascii
import paramiko
port = 22
data = ""
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, username=user, password=secret, port=port, timeout=10)
ssh = client.invoke_shell()
ssh.send("rxmop:moty=rxotg;\n")
while data.find("<") == -1:
   time.sleep(0.1)
   data += ssh.recv(99999).decode('ascii')
ssh.close()
client.close()
f = open('text.txt', 'w')
f.write(data)
f.close()

The normal output is below:

MO RSITE COMB FHOP MODEL
RXOTG-58 54045_1800 HYB BB G12

SWVERREPL SWVERDLD SWVERACT TMODE
B1314R081D TDM

CONFMD CONFACT TRACO ABISALLOC CLUSTERID SCGR
NODEL 4 POOL FLEXIBLE

DAMRCR CLTGINST CCCHCMD SWVERCHG
NORMAL UNLOCKED

PTA JBSDL PAL JBPTA

TGFID SIGDEL BSSWANTED PACKALG
H'0001-19B3 NORMAL

What can you recommend in order to return normal output, so that all characters are processed? Regular expressions do not help, since the structure of the record is shifted, then characters from certain positions are selected in the code. PS try to use ssh.invoke_shell (term='xterm') don't work.

Bear-ivan
  • 1
  • 1
  • 1
    Please fix the indentation of your Python code example and an answer will likely come. Thanks. – xendi Jun 11 '20 at 16:10
  • @dippas Please stop duplicating your revision comments with comments below the questions. – user229044 Jun 12 '20 at 03:07
  • @meagar sorry it is automatic from the magicEditor script, I've seen that only add comments when I make edits from the review queue. Since I noticed that I've been trying to deleted them, I might miss one or 2. sorry again – dippas Jun 12 '20 at 03:10
  • 1
    @meagar I might open an issue with SOCVR team, which are behind the script – dippas Jun 12 '20 at 03:13

1 Answers1

0

There is an answer here: How can I remove the ANSI escape sequences from a string in python

There are other ways... https://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output

Essentially, you are 'screen-scraping' input, and you need to strip the ANSI codes. So, grab the input, and then strip the codes.

import re

... (your ssh connection here)
data = ""
while data.find("<") == -1:
    time.sleep(0.1)
    chunk = ssh.recv(99999)
    data += chunk
... (your ssh connection cleanup here)
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
data = ansi_escape.sub('', data)
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42