简体   繁体   中英

Replace login prompt with interactive bash script on serial port linux

I am working on a CentOS box.

What I expect: To run my own CLI/setup on startup instead of login prompt on serial console (telnet).

What I did so far:- I changed call to "agetty" command in serial.conf and serial-ttyUSB0.conf files under /etc/init/, as follows:-

exec /sbin/agetty -n -l <path-to-my-custom-script> ........

My custom.sh script is:-

#!/bin/bash

LOOP_FLAG=0
while [ $LOOP_FLAG -eq 0 ]; do
        for TTY in /dev/ttyS0 /dev/tty0; do
            echo "Please choose to enter in 'setup' or 'cli'. (s/c)?  " > $TTY
        done
        read sc
        case $sc in
            [Ss]* ) LOOP_FLAG=1; <some-executable-cli-file-path>; break;;
            [Cc]* ) LOOP_FLAG=1; <some-executable-setup-file-path>; break;;
            * ) for TTY in /dev/ttyS0 /dev/tty0; do
                    echo "Please press 's' or 'c'." >$TTY
                done;;
        esac
done

But when system boots, on a telnet session, I could only see the "Please choose to enter.." question on screen and after that I couldn't able to type anything on console.

One more update: If I run the above agetty command on shell prompt as it is (say from ssh session), then it works fine on serial console (telnet). But, from the above startup scripts, it doesn't work.

Can anybody help me out with this?

Thanks in advance.

-Neo

Sorry I'm a few years late. Hopefully this will be of help for people searching for solution to this problem in the future.

The issue lies here:

-n, --skip-login Do not prompt the user for a login name. This can be used in connection with -l option to invoke a non-standard login process such as a BBS system. Note that with the -n option, agetty gets no input from user who logs in and therefore won't be able to figure out parity, character size, and newline processing of the connection. It defaults to space parity, 7 bit characters, and ASCII CR (13) end-of-line character. Beware that the program that agetty starts (usually /bin/login) is run as root.

So you need to initialize the terminal yourself in the script you are replacing the login prompt with. I found the settings below work well:

/bin/stty -F /dev/ttyO0 115200 cs8 sane

Remember to replace the baud rate and terminal name to your own.

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