0

i'm working on a little automation with python about collecting some data related to Splunk indexes. So i though using paramiko to login in one each of those indexer instances and get the info. The thing is when i send a command using

myShell = ssh.invoke_shell()
myShell.send('sudo su\n')
time.sleep(1)
print(myShell.recv(2048))

i get a result like:

b'Last login: Fri Jun  7 15:56:07 2019 from MiIpAddress\r\r\n\x1b[0;39m[\x1b[0;31m15:56:55\x1b[0;39m][\x1b[1;33muser\x1b[1;34m@\x1b[1;33mhost\x1b[0;39m][\x1b[1;36m/home/user\x1b[0;39m]\x1b[0;39m $

Why this is happening and how can i solve it?

BTW: using pycharm IDE from Mac

Thanks!

2 Answers2

1

If the only issue is with the b'' around your result, then you have to convert it to a string.

When you receive the information from 'myShell' you are receiving it in bytes. If you want to convert it into a string you would do something like

info = myShell.recv(2048)
decoded_info = info.decode('utf-8')
print(decoded_info)

Note, however, that this assumes that your message was encoded using the utf-8 protocol, which is the default.

G. Curbelo
  • 29
  • 3
-1

The result doesn't get converted into a string as it contains ASCII escape code, which the decode() will be unable to convert into a string.

Laurens Koppenol
  • 2,946
  • 2
  • 20
  • 33
  • 1
    This was published as an answer, but does not attempt to answer the question. Possibly it should be a comment, otherwise please edit your answer and add an explanation that may be useful. – borchvm Feb 11 '20 at 08:49