简体   繁体   中英

Can ZSH's ZLE input into a child process?

I am trying to create a keyboard shortcut to automate upgrading my shell to a fully interactive TTY for reverse shells.

Currently, I have a shortcut configured in Konsole to add this to my stdin: python3 -c "import pty;pty.spawn('/bin/bash');" . I then need to press ctrl-z twice, once to suspend the running process and once more to execute the following shortcut (adapted from Jonathan Hodgson's blogpost ):

## Upgrade shells with keyboard shortcut (also configured in Konsole settings)
function fg-bg() {
    if [[ $#BUFFER -eq 0 ]]; then
        local backgroundProgram="$(jobs | tail -n 1 | awk '{print $4}')"
        case "$backgroundProgram" in
            "nc"|"ncat"|"netcat")
                # Make sure that /dev/tty is given to the stty command by doing </dev/tty
                local columns=$(stty -a < /dev/tty | grep -oE 'columns [0-9]+' | cut -d' ' -f2)
                local rows=$(stty -a < /dev/tty | grep -oE 'rows [0-9]+' | cut -d' ' -f2)
                notify-send "Terminal dimensions" "Rows: $rows\nColumns: $columns\nstty command on clipboard"
                stty raw -echo < /dev/tty; fg; zle -U "stty rows $rows cols $columns
export TERM=\"xterm-256color\""
                ;;
            *)
                fg
                ;;
        esac
    fi
}

zle -N fg-bg
bindkey '^Z' fg-bg

This works OK, but I'd like to make it better by removing the need to have three shortcuts pressed in quick succession. I thought it might be possible to change Konsole's shortcut to make the process suspend, for example by adding \r\n^Z\r\nzle fg-bg\r\n to the python3 shortcut, but that just adds the text literally (except for carriage returns).

While running a foreground job (in this case, after you submit your command line), the ZLE is no longer active and thus cannot handle your inputs. It's active only while editing the command line.

When you press ^Z during a foreground job, this causes the terminal driver (not the ZLE) to send the TSTP signal to the it. This causes it to be suspended.

If you want your ^Z to be handled differently for foreground jobs, your best bet might be to configure your terminal to send ^Z^Z when you press ^Z . However, then it will also send this while the ZLE is active. You're probably better off just pressing ^Z twice yourself.

As for the shortcut you configured in Konsole: Trying to run zle fg-bg from the command line is futile, because, again, when you execute a command line, the ZLE is no longer active.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM