#!/usr/bin/perl #---------------------------------------------------------------------------------------------- # Description: This is an interface to start the pppd connection. # It display a dialog box with a Cancel button on it. # If the ppp connection is successfull, it kills the dialog, # on the other hand, # If the Cancel button is pressed, it kill the connection script. # Author: Laurentiu Cucos # Date : 01/25/02 #---------------------------------------------------------------------------------------------- #--- global constants ------------------------------------- $dlgBoxPath = "/home/lcucos/utils/ppp-scripts"; #--- Path to the ppp utilities $dlgBox = "connDlg"; #--- Dialog Box application $secReset = 10 ; #--- Number of seconds before issuing a reset $pingHost = "pi.cs.wmich.edu"; #--- the host where to do a ping to check the connection $disconnect = "$dlgBoxPath/directDown"; #--- The disconnect script $lockFile = "/var/lock/LCK..ttyS0"; #--- Look file to check if the connection is on #--- check the command line parameters --- use Getopt::Long; GetOptions("-d" => \$useDialog, #--- display a dialog box with a cancel button "-h" => \$help ); if($help){ die("This is a small utility that help to make a connection to WMU apparments Options: -d : use a dialog Box that allow the user to cancel the connection process. By default do not use the dialog. The dialog mode requires connectDlg.c to be compiled under the name connDlg -h : this help"); } #--- A function that tests if a the connection is ON sub isConnected { #--- first check if the lock file exist if (open(MYFILE, $lockFile)) { #--- do a ping to see if the connection is rely on open(DLGID, "ping -c 1 $pingHost|"); $outt = ; close($outt); $outt =~ /$pingHost/; }else{ $outt = 0; } #--- return $outt; } #---------------------------------------------------------- #----- Start the program here ----------------------------- if ( &isConnected ) { print("Already connected\n"); exit ; } #--- call the ppp Up script --- print("Start the pppd !\n"); system("/usr/sbin/pppd /dev/ttyS0 57600 file $dlgBoxPath/final"); #--- only if requested display a dialog --- if($useDialog ne ""){ #--- display a dialog box with connect in a different process --- $procID = fork(); if($procID == 0) { #--- this is the child process open(CAT, "$dlgBoxPath/$dlgBox|"); $input = ; close(CAT); print("$input"); exit; } } $count=0; $resetDone = 0; #--- this is the parent process ----------------- #--- check the status of the network using ping, # when the network is up, kill the dialog box do{ sleep 1; #--- do a reset, if fail to connect after $secReset seconds if($count > $secReset && $resetDone == 0){ print("Do a reset\n"); system("cat $dlgBoxPath/resetData.txt > /dev/ttyS0"); $resetDone = 1; }else{ $count++; } if($useDialog ne ""){ #--- get the dialog box process ID, assume there is no other process running with the same name!! $out =`ps -e| grep $dlgBox`; print "$out\n"; $out =~ s/ +//; #$out =~ s/^[ \t]+//; #--- remove extra blanks (from the beggining/end/more) @arrOut = split(/ +/,$out); $childID = $arrOut[0]; print("ChildID = $childID\n"); close(DLGID); } # print("check if it is connected --$arrOut--\n"); if ( &isConnected ){ $res = TRUE; }else{ #--- check if the user had cancel the dialog box if($arrOut ne "" and @arrOut == 0){ $res = TRUE; #--- call disconnect script system($disconnect); die("Program aborted!\n"); } } }while(!$res && $count<100); #--- kill the child dialog box if there was one if($childID ne ""){ print("Connected; kill dialog child: $childID\n"); kill (2, $childID); } #--- send the IP address to my account on my web site print("Send IP address to my web page\n"); system("$dlgBoxPath/getIP.pl > xxx; scp -i /home/lcucos/.ssh/id_dsa xxx "); #--- start an infinite loop of ping at every 20 minute --- print("Start an inifinite ping loop\n"); system("/bin/ping -i 1200 $pingHost&");