开发者

ssh gateway in python - colors available?

开发者 https://www.devze.com 2023-04-10 22:02 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号