I'd like to use socat to redirect the STDIN/STDOUT of the process executing docker attach <container-id> to a listening TCP socket on my Linux system.
root@eve-ng:/opt/unetlab# tty
/dev/pts/2
root@eve-ng:/opt/unetlab#
root@eve-ng:/opt/unetlab# socat EXEC:'/usr/bin/docker attach afe29573-0030-4a69-8b3e-c04ededa8db7-0-4',pty TCP4-LISTEN:32000 &
[1] 1275378
root@eve-ng:/opt/unetlab#
The options for socat are the following:
EXEC:<cmd>,ptyto saysocatto start a process executing<cmd>allocating a pseudo-terminalptyattached to itsSTDIN/STDOUT(socatactually opens the master side multiplexer/dev/ptmxand then thefdfor the pseudo-terminal pair's masterptm)TCP4-LISTEN:32000to saysocatto open atcpsocket listening on port32000
Indeed, in another terminal you can check:
root@eve-ng:~# tty
/dev/pts/5
root@eve-ng:~#
root@eve-ng:~# ps -ef | grep attach
root 1275378 1210230 0 08:43 pts/2 00:00:00 socat EXEC:/usr/bin/docker attach afe29573-0030-4a69-8b3e-c04ededa8db7-0-4,pty TCP4-LISTEN:32000
root 1275379 1275378 0 08:43 pts/2 00:00:00 /usr/bin/docker attach afe29573-0030-4a69-8b3e-c04ededa8db7-0-4
root@eve-ng:~#
docker attach actually attaches to a process running inside the container executing the exec executable, by the way you can see pts/1 is allocated to it (the container indeed was created using docker create -it)
root@eve-ng:~# ps -ef | grep exec
root 1284441 731051 2 08:52 pts/1 00:00:00 exec
root 1284583 1222614 0 08:53 pts/5 00:00:00 grep --color=auto exec
root@eve-ng:~#
The problem I'm facing to is that connecting to the tcp socket 127.0.0.1:32000 from a telnet client, the login process inside the container (exec) doesn't work as expected. It seems the username typed in (carlo) is repeated again not allowing to successfully login into the container.
Username: carlo
carlo
Password:
RP/0/RP0/CPU0:Jun 20 09:00:59.739 UTC: exec[68424]: %SECURITY-LOGIN-4-AUTHEN_FAILED : Failed authentication attempt by user '<unknown>' from 'console' on 'con0_RP0_CPU0'
User Access Verification
Username:
Can you help me in understanding why ? Thanks.
P.s. digging into it using strace I noticed that socat actually reads from the relevant fd (of type tcp socket) twice instead of just once.
rawer(soEXEC:<command>,pty,rawer). Maybe its the echo which screws you. Another option you might try instead ofrawerisecho=0becauseraweralso seem to affect the handling of `\r\n' line ends send by telnet.echo=0option (EXEC:<cmd>,pty,echo=0) without luck.