Bash capture keystrokes from another terminal

Using bash 4.3 and root privileges, I want to be able to detect one keystroke (any key) from another terminal.

In terminal 1, a background process that writes to a named pipe when one character was read from another tty, say pts / 18

read -rsn1 < /dev/pts/18 && echo > ./myfifo & 

In terminal 2, an attempt to read a character from the same fifo

read -rsn1 < ./myfifo

It works moderately well, but only after several (3 or 4) keystrokes in the / 18 glasses, and not in the first.

The mechanism seems redundant, but it allows you to start several background processes with different tty and redirect to the same named pipe.

I hope you help me.

+4
source share
1 answer

. script, , strace:

#!/bin/bash

# Couple of special symbols
BACKSPACE='\10\33[K'
BACKSPACE_EMPTY='\7'
BACKSLASH='\\'
QUOTE="\'"
DOUBLE_QUOUTE='\"'
LARROW='\10'
RARROW='\33[C'
#APPOSTRO="'\`'"
BACKSPACE_='\\33\[1P'
#LOGDATE=`date '+%Y/%m/%d %H:%M:%S'`
BADBIN='\\33\]'

while read -r line
do
    # Avoiding binary symbols
    NOBINline=$(echo "${line}" | strings)
    # Current `pwd` value for session
    CURRENT_PWD_OF_PID=$(readlink -f /proc/${1}/cwd)
    # Getting username
    USER_OF_PID=$(cat /proc/${1}/environ | strings | tr '.' ' ' | grep USER | awk -F= '{print $2}')
    # Not the best but working way to create prompt line
    HOSTNAME_OF_PID=`hostname -a`
    STR_TO_REMOVE=$(printf "${USER_OF_PID}""@""${HOSTNAME_OF_PID}"":""${CURRENT_PWD_OF_PID}")
    # Cut useless symbols from strace output.
    parsed_line=$(echo "${NOBINline}" | perl -nale 'print $1 if ~/\"(.*)\"/gi')
    if [ "${parsed_line}" == "\n" ]
    then
        parsed_line="{{ENTER}}"
    fi
    output_line=''
    inchar_line=''
    postinchar_line=''
        inchar_line=$(printf "${parsed_line}")
        if [ "${inchar_line}" == "{{ENTER}}" ]
        then
            echo ""
        else
        output_line=$(printf "${output_line}""${inchar_line}")
        fi
    if [ "${output_line}" != "${CURRENT_PWD_OF_PID}" -a "${output_line}" != "${STR_TO_REMOVE}" -a `echo "${NOBINline}" | grep -c "${BADBIN}"` -eq 0 ]
    then
        printf "${output_line}"
    fi
done < <(sudo strace -e trace=write -s1000 -p $1 2>/dev/stdout)

:

./script.sh <PID_of_mointored_console>

script ( , ):

#!/bin/bash

while [ 1 ]
do
        # lets and log_start -- names of two my scripts.
        for PIDs in `ps awwux | grep [p]ts | grep bash | grep -v lets | grep -v log_start | grep -v grep | awk '{print $2}'`
    do
        PTS=$(ps awwux | grep "${PIDs}" | grep -v grep | awk '{print $7}')
        if [ -f /tmp/bash_log.${PIDs} ]
        then
            sleep 1
        else
            touch /tmp/bash_log.${PIDs}
            # lets.sh -- first script.
            /bin/bash /root/lets.sh $PIDs >> /tmp/bash_log.${PIDs} &
        fi

#       tr -cd '\11\12\15\40-\176' < /tmp/x_bash_log.${PIDs} | sed 's/\]0;//g' | sed 's/\[C//g' > /tmp/bash_log.${PIDs}
    done
    for IFEMPTY in `find /tmp/ -type f -name 'bash_log*' -mmin +600`
    do
        if [ `cat "${IFEMPTY}" | grep -v "\'" | wc -c` -lt 2 ]
        then
            rm -rf "${IFEMPTY}"
        else
            sleep 1
        fi
    done

done

, script. firstscript.sh <PID> /tmp/bash_log.<PID>.

:

./monitor.sh &
# Start new terminal

[sahaquiel@sahaquiel-PC ~]$ echo $$
916
[sahaquiel@sahaquiel-PC ~]$ HELO, IM A NEW STRING
bash: HELO,: command not found
[sahaquiel@sahaquiel-PC ~]$ tail -n3 /tmp/bash_log.916
916
''[sahaquiel@sahaquiel-PC ~]$ HELO, IM A NEW STRING
'''[sahaquiel@sahaquiel-PC ~]$ tail -n3 /tmp/bash_log.916

NB. , .

/tmp/bash_log . , Ctrl+R, Ctrl+C , :

'''[sahaquiel@sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING 123[BACKSPACE]    [Backspace found. Rewritten string]: └────>
'''[sahaquiel@sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING 12[BACKSPACE]    [Backspace found. Rewritten string]: └────>
'''[sahaquiel@sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING 1[BACKSPACE]    [Backspace found. Rewritten string]: └────>
'''[sahaquiel@sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING 
'''[sahaquiel@sahaquiel-PC ~]$ NOW I I[BACKSPACE]    [Backspace found. Rewritten string]: └────>
'''[sahaquiel@sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TIMES[LARROW]    [Left arrow found.]: └────>
'''[sahaquiel@sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TIME[LARROW]S[LARROW]    [Left arrow found.]: └────>
'''[sahaquiel@sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TIM[LARROW]ES[LARROW]    [Left arrow found.]: └────>
'''[sahaquiel@sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TI[LARROW]MES

. , , .

P.S. - , Github ( )

0

Source: https://habr.com/ru/post/1692700/


All Articles