Home arrow Site Administration arrow Page 3 - More Hacks for the User Environment in BSD

Hack 11: Use an Interactive Shell - Administration

In this article, the third and last of a series, you'll learn how to customize a BSD environment for other users; maintain your chosen environment on multiple machines; and more. It is excerpted from chapter one of the book BSD Hacks, written by Dru Lavigne (O'Reilly, 2005; ISBN: 0596006799), Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. More Hacks for the User Environment in BSD
  2. Hack 10: Maintain Your Environment on Multiple Systems
  3. Hack 11: Use an Interactive Shell
  4. Hack 12: Use Multiple Screens on One Terminal
By: O'Reilly Media
Rating: starstarstarstarstar / 1
December 21, 2006

print this article



Save and share an entire login session.

How many times have you either struggled with or tried to troubleshoot another user through a thorny problem? Didnít you wish you had another set of eyes behind you so you could simply type your command set, point at the troublesome output, and say, ďThatís the problem.Ē Well, if you canít bring another user to your output, you can still share that real-time output using an interactive shell.

Recording All Shell Input and Output

There are actually several ways to share what is happening on your screen. Letís start by recording all of your input and output to a file. Then weíll see how we can also allow another user to view that output from another terminal.

Your BSD system comes with thescriptcommand which, not surprisingly, allows you to script your session. This command is extremely simple to use. Simply typescript:

  % script
Script started, output file is typescript

By default, script will create an output file named typescript in your current directory. If you prefer, you can specify a more descriptive name for your script file:

  % script configure.firewall.nov.11.2003
  Script started, output file is configure.firewall.nov.11.2003

Regardless of how you invoke the command, a new shell will be created. This means that you will see the MOTD and possibly a fortune, and your .cshrc will be reread.

You can now carry on as usual and all input and output will be written to your script file. When you are finished, simply press Ctrl-d. You will see this message:

  Script done, output file is configure.firewall.nov.11.2003

If youíve ended a script and decide later to append some more work to a previous session, remember the -a(append) switch:

  % script -a configure.firewall.nov.11.2003

This will append your current scripting session to the named file.

I findscript extremely useful, especially when Iím learning how to configure something for the first time. I can easily create a written record of which commands I used, which commands were successful, and which commands caused which error messages. It also comes in handy when I need to send an error message to a mailing list or a programís maintainer. I can simply copy or attach my script file into an email.

Cleaning Up script Files

Thescript utility is a very quick and dirty way to record a session, and it does have its limitations. One of its biggest is that it records everything, including escape characters. For example, here is the first line from one of my script files:

  [1mdru@~ [m: cd /s [K/ysr/ [K [K [K [K [Kusr/ports/security/sn o rt

Itís a bit hard to tell, but this is whatscriptwas recording:

  cd /usr/ports/security/snort

This isnít reallyscriptís fault; itís ugly for several reasons. One, my customized prompt contains control characters. Those display as[1mand[maround my username. Second, I had problems typing that day. Instead of/usr, I typed/sand had to backspace a character. Then I typed/ysrand had to backspace three characters. Finally, I used tab completion. You can see that I tried to tab atsnbut received a beep; I then tried to tab atsnoand had my input completed tosnort.

Granted, if I had first used thefileutility on my script file, I would have received a warning about this behavior:

  % file configure.firewall.nov.11.2003  
  configure.firewall.nov.11.2003: ASCII English text, with CRLF, CR, LF line
  terminators, with escape sequences

All is not lost, though. This command will get rid of most of the garbage characters:

  % more configure.firewall.nov.11.2003 | \
    col -b > configure.firewall.nov.11.2003.clean

col is an interesting little utility. It silently filters out what it doesnít understand. Hereís an example where this actually works to our advantage. col doesnít understand control characters and escape sequences, which is exactly what we wish to get rid of. Including 
-b also asks col to remove backspaces.

The result is much more readable:

  1mdlavigne6@~m: cd /usr/ports/security/snort
  % file configure.firewall.nov.11.2003.clean 
  configure.firewall.nov.11.2003.clean: ASCII English text

Iíve found that using an editor during a script session also produces very messy output into my script file. The precedingcol -bcommand will clean up most of the mess, but I still wonít have a very good idea of exactly what I typed while I was in that editor. For this reason, I use theechocommand to send little comments to myself:

  % echo # once you open up /etc/rc.conf
% echo # change this line: linux_enable="NO"
  % echo # to this: linux_enable="YES"
  % echo # and add this line: sshd_enable="YES"

If you really want to get fancy, map one key to ďstart echoĒ and another to ďend echoĒ as in ďUse Terminal and X BindingsĒ [Hack #4].

Recording an Interactive Shell Session

Letís look at an alternate way of recording a session. This time Iíll use the-i(or interactive) switch of my shell:

  % csh -i | & tee test_session.nov.12.2003

tcsh is linked to csh in FreeBSD. It doesnít matter which one I type; Iíll still end up with the tcsh shell.

In that command, I used-ito start an interactivetcshshell. I then piped (|) both stdout and stderr (&) to theteecommand. If youíve ever looked at physical pipe plumbing, youíll recognize the job of a ďteeĒ in a pipe: whatever is flowing will start going in both directions when it hits the ďtee.Ē In my case, all stdout and stderr generated by my shell will flow to both my monitor and to the test_session.nov.12.2003 file. When Iím finished recording my session, I can type Ctrl-c, Ctrl-d, orexitto quit.

Like the previousscriptcommand, an interactivecshshell will present me with a new shell. However, this method does not record escape characters, meaning I wonít need to use thecol -bcommand to clean up the resulting file.

But if I try to usevi during my session, the shell will refuse to open the editor and will instead present me with this interesting error message:

  ex/vi: Vi's standard input and output must be a terminal.

If I try to useee, it will open, but none of the commands will work.pico works nicely but still throws garbage into the session file. So, if I need to use an editor during my session, Iíll stillecho some comments to myself so I can remember what I did while I was in there.

Appending works almost exactly like it does forscript, again with the-a(append) switch:

  % csh -i | & tee -a test_session.nov.12.2003

Letting Other People Watch Your Live Shell Sessions

Regardless of which method you choose to record a session, another user can watch your session as it occurs. In order for this to work, that user must:

  • Be logged into the same system
  • Know the name and location of your script file

For example, Iíve created atestaccount on my system and configuredsshd. Iíll now see if I cansshinto my system as the usertestand watch the results ofdruís test_session.nov.12.2003.

  % ssh -l test

Once I successfully log in, my customized prompt indicates Iím thetestuser. I can now use thetailcommand to watch what is happening indruís session:

  % tail -f ~dru/test_session.nov.12.2003

My prompt will appear to change to indicate I am the userdru. However, Iím not. Iím simply viewingdruís session. In fact, I can see everything that the userdruis seeing on her terminal. This includes all of her input, output, and any error messages she is receiving.

Whiletailis running, I wonít be able to use my prompt. If I try typing anything, nothing will happen. I also canít interact with the user or change what is happening on her terminal. However, I do have a birdís eye view of what that user is experiencing on her terminal. When Iím ready to return to my own prompt, which will also end my view of the session, I simply need to press Ctrl-c.

See Also

  1. man script
  2. man file
  3. man col
  4. man tee
  5. man tail

>>> More Site Administration Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates


Dev Shed Tutorial Topics: