Creating an Application-Based Terminal Session

One of my first exposures to computers in a work environment was using a Wyse terminal to access a console-based application for data entry. It wasn't until a while later that I learned about terminals in UNIX and how they work. Once I understood it, I wanted to create my own self-contained application that was tied to a telnet or SSH session.

The method I've found to accomplish this involves the use of the trap command and some (seemingly) intimate loops. The example code will be inserted into the .bashrc file of the user to whom you wish to give application access. The example code was used for spam message entry into the sa-learn application for Spam Assassin:


instanceid="$RANDOM"
clean_session() {
    [[ -f /tmp/spam.message-$instanceid ]] && rm /tmp/spam.message-$instanceid
    exit
}


trap clean_session SIGINT

while true; do
    clear
    printf "******** Spam Message Entry Menu ********\n"
    printf "Enter the body of spam message, then press Ctrl-D\n"
    printf "To exit at any time, press Ctrl-C\n\n"
    cat > /tmp/spam.message-$instanceid
    clear
    printf "Processing....\n\n"
    /opt/bin/sa-learn --spam /tmp/spam.message-$instanceid
    printf "\n\n"
    rm /tmp/spam.message-$instanceid
    read -s -n 1 -p "Press any key to continue...."
done
exit

Let's examine the logic and functionality. The first line establishes a unique session ID for application access. If you have multiple users logging on with the same user name this will help keep data straight between sessions. The clean_session function needs to be declared at the beginning of the script for use with the following trap statement. The trap statement will run the clean_session function whenever it detects that the user entered Ctrl-C (interrupt signal or SIGINT).

For the purpose of establishing a the data-entry application, you'll use an infinite while loop. Once data is entered the first time and the end of the loop is reached, the loop just starts over and is ready for more data entry. This is where the clean_session function comes into play. If users want to exit, as per the instructional printouts, they type Ctrl-C. Normally, this would drop them into a shell, but since you want to isolate their usage to this application, you'll invoke the clean_session function, which will clean up their temporary file (if it exists) and log them off. The body of the while loop just accepts input (the cat statement), feeds the data into sa-learn and cleans up the temporary file.

For this example, you will need to modify the sudoers file to allow your user root access to sa-learn; the actual location of sa-learn might vary with your installation.

Andy Carlson has worked in IT for the past 15 years doing networking and server administration along with occasional coding. He is thankful to have chosen a career that he loves, grows in and learns from. He currently resides in Cincinnati, Ohio, with his wife, three daughters and his son. His family is currently in the process of adopting two children internationally. He enjoys playing the guitar, coding, and spending time with family and friends.

Load Disqus comments