Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[支持] - vim returns error code when using interactive ssh #2334

Open
JunBys opened this issue Dec 14, 2023 · 1 comment
Open

[支持] - vim returns error code when using interactive ssh #2334

JunBys opened this issue Dec 14, 2023 · 1 comment
Labels

Comments

@JunBys
Copy link

JunBys commented Dec 14, 2023

Are you using paramiko as a client or server?

Client

What feature(s) aren't working right?

SSH

What version(s) of paramiko are you using?

3.3.1

What version(s) of Python are you using?

3.11.6

What operating system and version are you using?

MacOS

If you're connecting as a client, which SSH server are you connecting to?

OpenSSH

If you're using paramiko as part of another tool, which tool/version?

No response

What are you trying to do with paramiko?

I hope to use paramiko to make an ssh interactive client, Works like the ssh root@xx.xx.xx.xx command in linux

How are you trying to do it, and what's happening instead?

So I wrote a python code to achieve this and it works fine except when I use vim!
When I use vim to edit any file, it will leak a bunch of garbled characters in the file, looks like terminal control code, like this:

^[]10;rgb:3333/3333/3333^[\^[]11;rgb:f5f5/f5f5/f5f5^[\

This problem occurs when I open any file using vim

Obviously I used xterm - 256color as the term type. This was not specified manually. I read it from the macos terminal environment variable $TERM.

I will show my code below so that teachers can troubleshoot this problem. Thank you very much!
I dont know what I did wrong, hope I can get help, thanks again

import threading, os, time, paramiko
import sys
import tty
import termios

class sshSocket():
    def __init__(self, cons):
        # 连接ssh
        print(cons)
        host = cons["host"]
        port = int(cons["port"])
        user = cons["user"]
        authType = cons["authType"]
        passwd = cons["passwd"]
        privkey = cons["rsa_path"]

        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(host, port, user, passwd)

        termT = os.environ.get('TERM')
        self.chan = client.invoke_shell(termT)


    def close(self):
        self.chan.close()
        self.client.close()

    def resize(self, width, height):
        self.chan.resize_pty(width=width, height=height)

    def read(self) -> str:
        data = self.chan.recv(1024)
        return data

    def send(self, data):
        # 向sshsocket发送数据
        self.chan.send(data.encode())
        

# Create ssh connection
cons = {
        "host": "xxxxxxx",
        "port": 22,
        "user": "root",
        "authType": 'password',
        "passwd": "xxxxxx",
        "rsa_path": ""
    }
ssh_socket = sshSocket(cons)


# Dynamically resize pty window
def sizes():
    oh, ow = '', ''
    while True:
        time.sleep(1)
        w, h = os.get_terminal_size()
        if oh != h or ow != w:
            print('大小改变')
            ssh_socket.resize(w, h)
            ow, oh = w, h
threading.Thread(target=sizes).start()


# Put the terminal into raw mode
def get_char():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setcbreak(fd)

        while True:
            dd = sys.stdin.read(1)
            if dd.encode() == b'\x1b':
                dd += sys.stdin.read(2)

            return dd
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)


# Ssh standard output and standard error are sent to the terminal
def echos():
    # sys.stdout.write(ssh_socket.read())
    # sys.stdout.flush()
    stdfile = open('/dev/stdout', 'wb')
    while True:
        stdfile.write(ssh_socket.read())
        stdfile.flush()
threading.Thread(target=echos).start()


# Terminal stdin sent to ssh
while True:
    try:
        char = get_char()
        # if char == '\n':
        #     char = '\r'
    except:
        char = '\x03'
    ssh_socket.send(char)

Anything else?

No response

@JunBys JunBys added the Support label Dec 14, 2023
@dsal3389
Copy link

dsal3389 commented Jan 4, 2024

I ran the code locally on my linux machine and vim on ssh works fine for me, can you try to use different terminal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants