ssh gateway in python - colors available?
I am using python bindings for libssh2 to connect to SSH2 server programmatically. The received output should be forwarded to a remote server and displayed there.
The code below works correctly, but it displays the result in monochrome. How can I display the colors or at least get the VT100 terminal control escape sequences, so I can replace them with HTML tags?
import socket
import libssh2
import os
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 22))
session = libssh2.Session()
session.startup(sock)
session.userauth_password("test", "test")
channel = session.channel()
channel.execute('ls -al /')
stdout = []
stderr = []
while not channel.eof:
data = channel.read(1024)
if data:
stdout.append(data)
data = channel.read(1024, libssh2.STDERR)
if data:
stderr.append(data)
print (''.join(stdout))
print (''.join(stderr))
I can use another ssh libra开发者_JS百科ry if needed, I just liked the simplicity and documentation of libssh2 bindings... I am open for other suggestions.
You can look into the paramiko python library. A good blog post going over details, with links to various python info is here. I'm not sure how paramiko would handle the control codes, but in theory, it will at least allow you to see them in its return data.
If you check the section of the docs dealing with invoking a shell, it allows you to set the terminal emulation.
精彩评论