Ho scritto un SSH script wrapper che fa la linea locale di editing. Viene richiamata allo stesso modo per SSH. Per esempio: python3 sshwrapper.py [email protected] -CX
. Il problema è che quando ho la connessione a un computer remoto utilizzando questo script e utilizzare vim
o tmux
lì, alcuni di immondizia viene stampato. Questo problema non è specifico per SSH, poiché i problemi che appaiono anche quando uso questo script per avvolgere bash
invece di ssh
.
Esempi:
Dopo l'avvio
tmux
alcuni di immondizia viene stampata dopo il prompt di bash:abc@me:~$ ^[[?65;1;9c
Quando l'apertura di un nuovo file con Vim utilizzando
vim mynewfile.txt
questo viene visualizzato nella prima riga:^[[2;2R^[[>65;6003;1c^[]10;rgb:0000/0000/0000^G^[]11;rgb:ffff/ffff/dddd^G
Come posso risolvere il problema?
Questo è lo script in questione:
import os
import pty
import select
import signal
import subprocess
import sys
master_fd, slave_fd = pty.openpty()
process = subprocess.Popen(['ssh'] + sys.argv[1:],
stdin=slave_fd,
stdout=slave_fd,
stderr=subprocess.STDOUT,
# Important for Ctrl-c in the remote terminal.
preexec_fn=os.setsid)
def sigint_handler(_signum, _frame):
os.write(master_fd, b'\03') # Send Ctrl-c.
signal.signal(signal.SIGINT, sigint_handler)
def sigtstp_handler(_signum, _frame):
os.write(master_fd, b'\x1A') # Send Ctrl-z.
signal.signal(signal.SIGTSTP, sigtstp_handler)
def sigchld_handler(_signum, _frame):
process.wait()
sys.exit(process.returncode)
signal.signal(signal.SIGCHLD, sigchld_handler)
while process.poll() is None:
# Block until there is something to read or write.
r, w, e = select.select([sys.stdin, master_fd], [], [])
if sys.stdin in r:
# Write to SSH.
user_input = os.read(sys.stdin.fileno(), 4096)
if not user_input:
os.write(master_fd, b'\04') # Send Ctrl-d.
else:
os.write(master_fd, user_input)
if master_fd in r:
# Read from SSH.
data = os.read(master_fd, 4096)
sys.stdout.write(data.decode())
sys.stdout.flush()
Sto usando Python 3.8.10 su Ubuntu 20.04 su entrambi i miei computer locale e il computer remoto. Questo è un self-progetto educativo, in modo da sto scrivendo il programma utilizzando librerie standard di Python solo.
export LS_COLORS=none
non ha alcun effetto sulla situazione.