1

I am using paramiko to connect to a win10 server (client is Linux) and for most parts communication is OK. But when i read the response to a command (via recv()) , i see some strange data. Example: this is the response to "hostname" and it should be just "halfdome-win10" , But what i read back is

 '\x1b[5;1Hhalfdome-win10

  \x1b[5;2Halfdome-win10                                                                  
  \x1b[5;3Hlfdome-win10                                                                  
  \x1b[5;4Hfdome-win10                                                                  
  \x1b[5;5Hdome-win10                                                                  
  \x1b[5;6Home-win10                                                                  
  \x1b[5;7Hme-win10                                                                  
  \x1b[5;8He-win10                                                                  
  \x1b[5;9H-win10                                                                  
  \x1b[5;10Hwin10                                                                  
  \x1b[5;11Hin10                                                                  
  \x1b[5;12Hn10                                                                  
  \x1b[5;13H10                                                                  
  \x1b[5;14H0                                                                  
  '

if i put this in a variable(say aa) and print it , seems like linux shell can decode and just print the right value.

>>> print cc
>>>
halfdome-win10

is there a right way to get the data from win10 ssh server and decode in a usable form?

I know it has the ansi characters but taking them out does not help and i still see the short echos of the response left.

  • i am using updated paramiko
  • i have a requirement to use interactive shell
  • if i use exec_command instead the stdout reads the expected value (no issue)
psb
  • 67
  • 1
  • 6
  • @MartinPrikryl: the question you mentioned is referring to ANSI codes in the reponse which does not help if i take out. What i see here is a strange repeating pattern of the expected response. This is completely independent of the ANSI characters. I have broken down the question to a lower layer since i dis not get any response to original question. – psb Aug 26 '19 at 17:18
  • *"which does not help if i take out"* - Do you mean *"if you remove them"*? Then you break the output. The `H` is an instruction for moving a cursor. If you remove that instruction, you obviously get repeating pattern. But if you process the `H` code correctly, you will overwrite the previous output and get correct results. Though you probably do not want to process the codes. You that to get output without the codes. You get the codes by using the "shell" channel and terminal emulation. This is covered in the link above, so please read it, before claiming that they do not help. They do! – Martin Prikryl Aug 26 '19 at 17:34
  • What requirement for *"interactive shell"* do you have? – Martin Prikryl Aug 26 '19 at 17:37
  • @MartinPrikryl - How to process the H code is what i am trying to figure out. could not phrase the question correctly. Thanks. I did some brute force processing to remove all but first call to H for each line re.sub(r'(\x1B.*?)(\x1B.*)' ,"\g<1>",line,re.I). This seems to resolve the issue but since server is still sending large number of bits. commands like "tasklist /NH" take a long time to get the data across. Is there are way i can setup the shell to not send the response with H code? same as i would get from a linux server? or is there any better way to process this response? – psb Aug 26 '19 at 23:41
  • Can you first explain, why you use `invoke_shell` in the first place? – Martin Prikryl Aug 27 '19 at 06:09
  • I Have an application which historically is using pexpect and i am trying to replace the low level code to paramiko and make APIs same as pexpect so calling programs do not have to change.Most derivatives cd and then issue commands specific to the dir. Also there are multiple threads syncing to operations in the same dir. Some commands i need query OP during execution(this i guess should be fine as can query stdout). – psb Aug 28 '19 at 00:32
  • And why do you think that it needs `invoke_shell`? – Martin Prikryl Aug 28 '19 at 05:24
  • Because that is how i can get a interactive shell. Is there any other way too? only other way i know is the exec_command which on each command is basically a new session. – psb Aug 28 '19 at 16:12
  • That's actually the question you should have asked in the first place! So please accept that this question is duplicate and ask a new question like *"How do I start a shell without terminal emulation in Python Paramiko?"* – Martin Prikryl Aug 28 '19 at 16:17
  • I can accept that(being a duplicate). But is there a way to start a shell without emulation? or you want me to ask the same as a different question post. BTW thanks for all the assistance you are providing, helps a lot – psb Aug 28 '19 at 18:09
  • Yes, I believe that this question is answered. Your real problem is *"How do I start a shell without terminal emulation in Python Paramiko?"* So please post a new question for that. – Martin Prikryl Aug 28 '19 at 18:52
  • and the answer is that if using invoke_shell for win10 openssh server, the returned will have H code, which cannot be processed in any easy to be in plain text? – psb Aug 28 '19 at 21:56
  • No. + You keep assuming it's Windows-only problem. It's not. Linux shells also use ANSI escape codes. See the duplicate question. It's about Linux, not Windows. You may have been just lucky not to encounter them with your Linux servers. But if you use the same code for Linux servers, you will sooner or later face the same problem. – Martin Prikryl Aug 29 '19 at 05:38
  • Actually i tried the same code to talk to Linux server, i do get ANSI color codes but not the H code which is the main issue here. But let me rephrase "if using invoke_shell for any server, that returns with ANSI H code . The returned cannot be processed in any easy to be in plain text" . Is this statement correct? – psb Aug 29 '19 at 17:16
  • It's not really correct. Post the question! – Martin Prikryl Aug 29 '19 at 17:19
  • Can you elaborate on how this statement is not correct? posted the question: https://stackoverflow.com/questions/57715388/how-do-i-start-a-shell-without-terminal-emulation-in-python-paramiko. – psb Aug 29 '19 at 17:43
  • I've answered your other question. Please *accept that this question is duplicate*! – Martin Prikryl Aug 29 '19 at 19:08

0 Answers0