I am struggling for the past couple of days to figure out why is paramiko sending \x07 instead of tab to remote session. I am using paramikos invoke_shell because i need to run several interconnected commands. Everything works except for this part:
check_export = f'echo -e \"this\tis\tjust a test\" > /tmp/export'
This is simplified version, the only important part is that i need to have words delimited by a tab (in certain places only). print returns correct form:
echo -e "this is just a test" > /tmp/export
but when i check the recv() from paramiko session i see this:
b'echo -e "this\x07is\x07just a test" > /tmp/export\r\n[root@server~]# '
and as a result the file /tmp/export does not contain tabs and those first 3 words are jammed together.
I have tried providing literal tab, \t, U00000009, nothing works.
I also tried doing .encode() on the string, but i get error that bytes are expected, not string.
What am i missing here? I would appreciate some input. Thanks
CodePudding user response:
That's all probably a side effect of you abusing an interactive shell for command automation. What happens if you type echo -e "this
in the shell and then you press Tab key? The shell will try to expand this
to a filename and beep (ASCII 7), if no such file is found. That's what your code does. It's not Paramiko seconding ASCII 7 (bell) to the server. It's the server responding with bell to you sending the Tab key.
Don't use an interactive shell for command automation. See
Execute multiple commands in Paramiko so that commands are affected by their predecessors
Or if you want to stick to your (imo wrong) approach, send literal \
t
sequence to the remote shell (what I assume you actually meant to do, based on the -e
) by escaping the backslash: \\t
check_export = f'echo -e \"this\\tis\\tjust a test\" > /tmp/export'
Btw, no need to f'...'
here. Or maybe you have meant to use r'...'
?
check_export = r'echo -e \"this\tis\tjust a test\" > /tmp/export'
See https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals