셸 을 연주 하 는 것 부터 터미널 창 을 연주 하 는 것 까지 tty

Simple: shell over socket
## netcat

# client
nc  -nvlp 13337

# server
nc -e /bin/sh localhost 13337

# tips
# RHEL          -p     nc -lv 13337
# FreeBSD   -e  ,    
rm -f x; mkfifo x; /bin/sh 2>&1 < x | nc localhost 1234 > x
## socat

# client
socat - TCP:localhost:1337

# server
socat TCP-LISTEN:1337,reuseaddr,fork EXEC:bash

    결함:
        일부 프로그램 이 정상적으로 종료 되 지 않 습 니 다.CTRL+C 를 누 르 면 바로 터미널 을 종료 합 니 다.예 를 들 어 top,vim,emacs 등 입 니 다.
Better: tty over socket
# server
socat TCP-LISTEN:1337,reuseaddr,fork EXEC:bash,pty,stderr,setsid,sigint,sane

# client
socat FILE:`tty`,raw,echo=0 TCP:localhost:1337

   지금 우 리 는 tty 세 션 창 이 있 습 니 다.우 리 는 Ctrl-C(^C)를 누 를 수도 있 고,top,vim,emacs,ssh,su,sudo 등 tty 명령 을 실행 할 수도 있 습 니 다.
    질문:
        일부 tty 프로그램 을 실행 할 때 표시 창 이 작은 문제 가 발생 할 수 있 습 니 다.다음 과 같이 해결 합 니 다.
#       
$ stty -a
speed 38400 baud; rows 40; columns 130; line = 0;
#       
$ stty rows 40 cols 130

    결함:
        창 세 션 을 실행 할 때마다 다시 설정 해 야 합 니 다.
Implementing it
Since there is no option in socat to magically do that, first we need to re-implement both server and client sides of what socat was doing, then we will improve it.
  • server: we need to get a pseudo-terminal, which is OS-specific. On Linux we'll open /dev/ptmx , this gives us the master which we'll connect to the socket. With an ioctl we get the name of the slave and open its corresponding /dev/pts/N (like your terminal!), unlock it and give it as stdin/stdout/stderr to the shell.
  • client: we need to take over the terminal, the shell's stdin being a terminal we'll make it raw, then connect it to the socket. Making the terminal raw has the effect that signals such as Ctrl-C will now go on the socket.

  • Terminal window resizing
    So we've reached the same point as we had with socat. Now, what's up with window changes? Well, turns out when you resize your terminal window a SIGWINCH signal is delivered!
    Also, we can get and set the window size with ioctl TIOCGWINSZ and TIOCSWINSZ .
    Here's what we can do: catch this signal on the client, get the new window size, send it over the socket to the server, which will set the window size on the pseudo-terminal, and send the same signal to the shell so it knows it can resize.Unfortunately there's one problem: we have only one socket, and it already relays the terminal data. So we need another, or rather, we can multiplex the socket to give us 2 channels: one to exchange data, one to push window size information from client to server.We do that and finally... we have it ! A remote shell terminal, which we can resize and it gets updated. Fancy! I did an implementation in Go if you want to look or try.Now if we just add some TLS, we're not too far from SSH. And using this multiplex of streams on the socket we could even add port forwarding, file transfer, etc. all in the same connection.
    final:
        저 자 는 GO 로 이 루어 진 서버 와 클 라 이언 트 를 제공 합 니 다.
        https://github.com/StalkR/misc/tree/master/pty
        PS:위 가 너무 길 어서 번역 을 안 해 요-
        Via: http://blog.stalkr.net/2015/12/from-remote-shell-to-remote-terminal.html

    좋은 웹페이지 즐겨찾기