Home » Linux Magazine » Cooking with Linux—The French Connection

Cooking with Linux—The French Connection

Marcel Gagné

Issue #65, September 1999

Mr. Gagné provides us with several recipes from his famed French restaurant.

Allo, and welcome to Chez Marcel, home of fine French Linux cooking.

Please take a seat. If you have not already done so, I would like you to read this article with a somewhat exaggerated French accent since that is the way I wrote it. Even the README files are heavily accented. For those of you who, like myself, are French, you may translate as you read, thus adding another level of authenticity to the excellent menu that awaits you.

It is an honour and a privilege to welcome you to my kitchen. Tonight, I have a special treat for you. The recipes I have prepared are made from common Linux distribution ingredients so that you can recreate these delicacies when you return to your own kitchen CPU. Tonight’s menu:

  • Mail notification for Windows clients
  • What’s my IP?
  • diald Control
  • Waiter! More disk.
  • Waiter! There’s a fly in my system.
  • Is it That Time Already?

A Little Wine with Dinner?

I must tell you right now that I am thrilled you have decided to join me in my kitchen. Much of what I have in store for you is designed to make Windows 9x more productive with a little help from Linux. If you have already cast away your Windows PC but you miss some forgotten piece of software (provided that software is not too demanding), you could check out http://www.winehq.com/ for the latest version of the Windows non-emulator for Linux.

For many users out there, the Windows workstation is still on the desktop, while the Linux machine performs such noble tasks for the Windows user as connecting to the Internet, providing firewall services, delivering mail, sharing disk space and printers, among other services.

What we have in store for you here today are Linux scripts that can make your Windows experience more enjoyable.

Talking to Windows

In the next few examples, we will have our Linux system talk back to Windows. While that is a good idea, we must first set up Windows to listen; otherwise, all our talk will disappear into the proverbial ether. When our Linux system needs to contact a Windows user, it will do so using Samba’s smbclient messaging functionality. Obviously, that requires a system running Samba. On the Windows side, it requires Winpopup.

To set up Winpopup to start each time you boot Windows, create a shortcut in the Windows Startup folder. Click on the Start button, then Settings, then Taskbar. Choose the tab that says “Start Menu Programs”, then click the Advanced button. This will open a Windows Explorer window. Open the Startup folder under Programs and add a shortcut to winpopup.exe. Accept the default name, and you will wind up with a cute little Jack-in-the-box icon. Right-click on it to access the properties tab. Now, set the Run: option to Minimized. Winpopup starts up out of the way, and pops up only when it gets a message. Double-click the icon to start it right away.

One more thing: you should now see Winpopup sitting minimized on your taskbar. Click on it to bring it up. Now click on Messages, and choose Options from the drop-down menu. Click the box for “Pop up dialog on message receipt”. Click OK and re-minimize Winpopup. Windows is now ready to receive messages from your Linux system, which brings us to the next item on the menu.

Why Didn’t Somebody Tell Me I Have Mail?

This is a frequent question we get from users. You are the administrator of a small- to medium-size office; you’ve set up an Internet gateway that picks up the mail on a regular basis using fetchmail, which then stores it on your Linux server. The problem is that your users are still running Windows with some sort of stand-alone POP3 e-mail package like David Harris’ Pegasus Mail. While many packages can be set up to retrieve mail automatically, keeping them open and running taxes their already taxed Windows system. This means users tend to point out how slowly their systems are running. You suggest they close a few applications. “How about e-mail?” you suggest. To which they reply, “What if I miss an important message?” The solution is the checkusermail script shown in Listing 1.

Listing 1. Check User Mail Script

# Check user mail and send messages if mail
# present
# Marcel Gagné - Salmar Consulting Inc., 1999
for user_name in 'cat /usr/local/etc/mail_notify'
do
   no_messages='frm $user_name |
      grep -v "Mail System Internal Data" |
      wc -l'
   if [ "$no_messages" -gt "0" ]
   then
     echo "You have $no_messages e-mail message \
waiting."> /tmp/$user_name.msg
     echo " " >> /tmp/$user_name.msg
     echo "Please start your email client to \
collect mail." >> /tmp/$user_name.msg
     /usr/bin/smbclient -M $user_name < \
       /tmp/$user_name.msg
   fi
done

In /usr/local/etc, create a small text file called mail_notify with the names of users who receive mail on your system. If the Windows clients are named differently (in the network configurator of the Windows control panel) than the user ID for mail, create an /etc/lmhosts file with IP addresses matching your mail user IDs, and the results should be the same. The script can be run from cron at whatever time interval suits your environment. Since it runs as root, it can spy into everyone’s mailbox with the frm command. It will tell each user they have mail and how many messages. If there is no mail, there is no message and no need to waste time and energy checking every few minutes.

Another happy soul you’ll discover after you deploy this script is the boss who had been complaining about either the amount of time users spent checking mail, or the user who did not check it often enough and missed an important message.

Hey There, What’s Your IP?

In the last example, I talked about the beleaguered administrator (is there any other kind?) and suggested this might be a useful tool for them as well. What if you are administering that system from far away? Worse yet, this Internet gateway runs on a budget—it’s a simple dial-up connection to an ISP running diald and IP masquerading. When trouble strikes, they call you. Trouble means you use TELNET to log in, check things out and fix the problems remotely. Trouble is, the IP is dynamic and changes with each connection to the ISP. The solution this time is the showppp.pl script shown in Listing 2. The script is simple and lives in cgi-bin. Depending on your web server setup, you might want to rename this one showppp.pl.

Listing 2. Show IP Perl Script

#!/usr/bin/perl
# 1999: Salmar Consulting Inc.  www.salmar.com
# showppp.pl - show me the ppp connection address
&show_header;
&show_body;
&http_end;
exit();
# Subroutines #
sub get_pppaddr {
   $pppaddr = `/sbin/ifconfig -l ppp0`;
     print "<HR NOSHADE>\n";
     print "<BR><PRE>$pppaddr</PRE><P>\n";
     print "<HR NOSHADE>\n";
}
#
sub show_header {
   print "Content-type: text/html\n\n";
   print "<HTML><HEAD>"
   print "<META HTTP-EQUIV='Refresh'>"
   print "<TITLE>La Connection Address</TITLE>\n";
   print "</HEAD>\n";
}
#
sub show_body {
   print "<BODY BGCOLOR='White'>\n";
   print "<HR noshade>\n";
   print "<H1><font color='Blue'>"
   print "PPP Connection Information</font></H1>\n";
   print "<p><h3>The information you need is the"
   print " number that follows <BR>"
   print "'inet addr:' (4 numbers "
   print "separated by periods).</h3>\n";
   &get_pppaddr;
}
#
sub http_end {
   print "</body>\n";
   print "</html>\n";
}

Setting up a simple Intranet for your users is a breeze. A nice corporate home page with some popular links to the Internet makes it easier on your resources than having each user start up their browser with http://www.cnn.com/ or http://www.excite.com/ as their start page. If your dial-up connection gives you only a fixed number of hours per month, that time can get chewed up very quickly.

Why not use that same page and give your users a link to a script that will display the IP address of your dial-up IP connection without requiring them to log in to your Linux server? Figure 1 shows just such a page.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 1. Show IP Page

Speaking of diald…

Every once in a while, the diald process may get hung up. Sometimes the modem has gotten hung up and diald can’t seem to drop it. In either case, the easiest way to get life back to normal is to stop diald and restart the process. Since you are operating remotely and want to make this as painless as possible, you can either do the work yourself or talk one of your remote users through the process. It would be so much easier if they could just type one command, rather than doing a ps ax | grep diald while hoping they kill the right PID and not bring your system down. Listing 3 is a little script to do just that. The user simply types:

diald.control restart

to get things moving again. You can also use the script to stop diald, start it, or shut it down and start a single instance of mgetty in order to pick up a fax (but that’s for another time).

Listing 3. Script for diald Management

#!/bin/sh
# description: Starts and stops the diald
# diald-on-demand process
# Marcel Gagné - Salmar Consulting Inc.
# See how we were called.
case "$1" in
  start)
     echo -n "Starting diald process: "
     /usr/sbin/diald
     echo "Done!"
     ;;
  stop)
     echo -n "Shutting down diald : "
     kill 'pidof -s diald'
     echo "Waiting 2 seconds for process to \
shut down ..."
     sleep 2
     kill 'pidof -s diald'
     ps ax | grep diald | grep -v grep |
       grep -v control
     echo "Done!"
     ;;
  restart)
     echo "Restarting /usr/sbin/diald process: "
     kill 'pidof -s diald'
     echo "Waiting 2 seconds for process to \
shut down ..."
     sleep 2
     /usr/sbin/diald
     echo "Done!"
     ;;
  getfax)
     echo -n "Preparing to receive a single fax:"
     kill 'pidof -s diald'
     echo "Waiting 2 seconds for process to \
shut down ..."
     sleep 2
        /sbin/mgetty /dev/ttyS0 < /dev/null &
        echo "starting mgetty on port ttyS0..."
     echo "Done!"
     ;;
  *)
     echo "Usage: diald.control \
{start|stop|restart|getfax}"
     exit 1
esac

You can use this example as a template to create any number of start, stop or restart scripts. When necessary, you can have trusted users log in to your server and run simple administration scripts like this one without risking damage to your system or the equally ominous possibility of killing an important process like init. After all, kill –1 looks very similar to kill 1, doesn’t it?

Waiter! More disk!

Remember those great little messages for letting your users know they have mail? How about using that same technique to let you, the administrator, know that some important system event needs your attention? For example, suppose you are constantly battling disk space. Wouldn’t it be nice if the system let you know that resources are low? With the next little Perl script (see Listing 4), you can have your Linux system send a pop-up message to your Windows workstation alerting you that system resources are low. This script scans disk partitions and reports to client speedy that disk space usage is over 90%. (See Figure 2.)

Listing 4. Perl Script to Check Disk Usage

#!/usr/bin/perl
# 1999: Salmar Consulting Inc. www.salmar.com
# checkdfusage.pl : checks disk usage in
# percentage and alerts sys admin if greater
# than 90%
&check_dfusage;
exit();
# Subroutines #
sub check_dfusage {
  @message='df';
  $message_text = '';
  foreach (@message) {
    ($dirname, $arg1, $arg2, $arg3, $arg4, $arg5,
        $arg6) = split (' ', $_, 7);
    ($arg4num,$therest) = split('%',$arg4,2);
    if ($arg4num > "90") {
      $message_text .= "Directory $dirname is now " .
      " at $arg4num\n";
      }
#print $arg4 . "\n";
  }
  if ($message_text ne '') {
    $smbclient = "/usr/bin/smbclient -U root";
    $smbhost = 'speedy';
    &sendmessage;
  }
}
sub sendmessage {
  open(SF,"|$smbclient -M $smbhost");
  print SF "$message_text";
  close(SF);
}

 

 

 

 

 

 

Figure 2. Disk Space Usage Screen

The Windows winpopup utility is a great tool for the user who still runs a Windows client but needs to get information sent to him from their Linux servers. This script and the earlier mail notification script highlight this flexibility.

By the way, the next item on the menu, the fly—that’s just to get you thinking about other possibilities.

Where Does the Time Go and What Time Is It Anyway?

In my business, I’ve set up a number of Linux internet gateways. To make these installations as inexpensive as possible, we set the machines to dial every half hour or so to pick up mail. That half-hour mark is also our cue to log in using TELNET if we need to do some administration work on the machines. Unfortunately, the time on each machine can vary. How about a way to check the time against some reliable source and adjust the Linux server?

The script in Listing 5 is a bit of Perl magic. It opens a socket on a secondary time server, picks up the time, and closes the socket. This is a nice alternative to NTP in that it does not tax the NTP time server’s resources. For that very reason, the NTP rules ask that you do not use primary servers if you don’t need to. A link to the “Public NTP Time Servers” page is provided in Resources.

Listing 5. Getting the Time Perl Script

#!/usr/bin/perl
# Get the time from a time server
# Marcel Gagné Salmar Consulting Inc., 1999
$remote_host="chime.utoronto.ca";
$remote_port="13";
$hostname=`hostname`;
chomp $hostname;
use IO::Socket;
$socket = IO::Socket::INET->new(
    PeerAddr => $remote_host,
    PeerPort => $remote_port,
    Proto    => "tcp",
    Type     => SOCK_STREAM)
  or die "Could not connect to " .
  "$remote_host:$remote_port: $@\n";
# Get the time
$the_time = <$socket>;
print "The time is $the_time\n";
$local_time = `date`;
print "Your system, $hostname, thinks the" .
 "correct \n date and time is $local_time.\n";
print "Resetting date and time ...\n";
$new_date = `date -s "$the_time"`;
print "New date is $new_date";
# Close the socket connection if open
close($socket);

Notice the line in Listing 5 that says remote_host=”chime.utoronto.ca”;. The host specified here is chime.utoronto.ca, but it could be one of any number of machines which offer primary or secondary time services. Consider the rules, though, and visit the NTP time server page for a system in your area and time zone.

Sorry Folks, it’s Closing Time

Well, that last script on time was my way of getting all of you out of the restaurant. I hope you’ll pay another visit to Chez Marcel. We hope we’ve succeeded in whetting your appetite for some of the wonderful and fun things you can do with a Linux system. Bon Appétit!

marcel gabne

 

 

 

 

 

 

 

Marcel Gagné (mggagne@salmar.com) lives in the mythical city of Mississauga, Ontario. Besides being a space alien, adventurer, pilot, magician and international man of mystery, he is president of Salmar Consulting Inc., a systems integration and network consulting firm. He also writes science fiction and fantasy, and edits TransVersions, a science fiction, fantasy and horror magazine.

x

Check Also

pppconnection

Linux system talk back to Windows

Marcel Gagné Issue #65, September 1999 Mr. Gagné provides us with several recipes from his ...