home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / autoppp.zip / AutoPPP.CMD next >
OS/2 REXX Batch file  |  1996-11-09  |  7KB  |  238 lines

  1. /* 
  2.  *  AutoPPP is a REXX script for periodic or continuous connection 
  3.  *  to a PPP line after which an optional Internet app (eg. mail 
  4.  *  reader or news reader to download messages) is launched.
  5.  *
  6.  *  Author:  David Forrai <dforrai@erinet.com>
  7.  *  Revision 1:  9 Nov 1996
  8.  *    Corrected logic to avoid creation multiple queues per connect attempt.
  9.  *    Added logic to prevent orphaned Wait4PPP jobs after PPP exits on error.
  10.  * 
  11.  *  First, load the REXX Util functions to access SysSleep() 
  12.  */ 
  13. CALL RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs' 
  14. CALL SysLoadFuncs 
  15. /* 
  16.  *  Default values for start time, interval, and queue name
  17.  */ 
  18. hmsTime = '' 
  19. hmsInterval = '' 
  20. start = 0        /* Start immediately  */ 
  21. interval = 86400 /* repeat in 24 hours */ 
  22. newQueue = ''
  23. /* 
  24.  * Parse the command line info 
  25.  */ 
  26. PARSE ARG args 
  27. allDone = 0 
  28. DO UNTIL allDone = 1 
  29. /* 
  30.  *  Parse the first word out of the args string, then convert it to 
  31.  *  uppercase. 
  32.  */ 
  33.    PARSE VAR args program args 
  34.    PARSE UPPER VAR program program 
  35.    allDone = 1 
  36. /* 
  37.  *  If the first word is preceeded by /H, it specifies the start time 
  38.  *  in hr:min:sec. Loop again to parse out the program names and 
  39.  *  arguments. 
  40.  */ 
  41.    IF POS('/H', program) = 1 THEN DO 
  42. /* 
  43.  *  If the first word is preceeded by /I, it specifies the interval 
  44.  *  time in hr:min:sec.  Loop again to parse out the program names 
  45.  *  and arguments. 
  46.  */ 
  47.       hmsTime = SUBSTR(program, 3) 
  48.       allDone = 0 
  49.       SAY 'Start time =' hmsTime 
  50.    END  /* IF */ 
  51.    IF POS('/I', program) = 1 THEN DO 
  52.       hmsInterval = SUBSTR(program, 3) 
  53.       allDone = 0 
  54.       SAY 'Interval =' hmsInterval 
  55.    END  /* IF */ 
  56. END /* DO */ 
  57. /* 
  58.  *  If a start time was given on the command line, calculate the 
  59.  *  interval between the current time and the time to launch the 
  60.  *  passed program. 
  61.  */ 
  62. IF hmsTime \= '' THEN DO 
  63.    PARSE VALUE hmsTime WITH hour ':' minute ':' second 
  64.    PARSE VALUE TIME() WITH curHr ':' curMin ':' curSec 
  65.    current = 3600 * curHr + 60 * curMin 
  66.    IF DATATYPE(hour, 'N') THEN DO 
  67.       IF (hour > 23) | (hour < 0) THEN DO 
  68.          SAY 'Invalid start hour ('||hour||'), aborting ...' 
  69.          'EXIT' 
  70.       END  /* If */ 
  71.       ELSE hour = 3600 * hour 
  72.    END /* IF */ 
  73.    ELSE DO 
  74.       SAY 'Invalid start hour ('||hour||'), aborting ...' 
  75.      'EXIT' 
  76.    END  /* Else */ 
  77.    IF DATATYPE(minute, 'N') THEN DO 
  78.       IF (minute > 59) | (minute < 0) THEN DO 
  79.          SAY 'Invalid start minute('||minute||'), aborting ...' 
  80.          'EXIT' 
  81.       END  /* If */ 
  82.       ELSE minute = 60 * minute 
  83.    END /* IF */ 
  84.    ELSE DO 
  85.       SAY 'Invalid start minute('||minute||'), aborting ...' 
  86.      'EXIT' 
  87.    END  /* Else */ 
  88.    start = hour + minute - current 
  89.    IF start < 0 THEN start = 86400 + start 
  90. END  /* IF */ 
  91. /* 
  92.  *  If an interval was given as an argument, calculate the interval 
  93.  *  between logins. 
  94.  */ 
  95. IF hmsInterval \= '' THEN DO 
  96.    PARSE VALUE hmsInterval WITH hour ':' minute ':' second 
  97.    IF DATATYPE(hour, 'N') THEN DO 
  98.       IF (hour > 23) | (hour < 0) THEN DO 
  99.          SAY 'Invalid interval hour ('||hour||'), aborting ...' 
  100.          'EXIT' 
  101.       END /* If */ 
  102.       ELSE interval = 3600 * hour 
  103.    END /* If */ 
  104.    ELSE DO 
  105.       SAY 'Invalid interval hour ('||hour||'), aborting ...' 
  106.       'EXIT' 
  107.    END /* If */ 
  108.    IF DATATYPE(minute, 'N') THEN DO 
  109.       IF (minute > 59) | (minute < 0) THEN DO 
  110.          SAY 'Invalid interval minute ('||minute||'), aborting ...' 
  111.          'EXIT' 
  112.       END /* If */ 
  113.       ELSE interval = interval + 60 * minute 
  114.    END /* If */ 
  115.    ELSE DO 
  116.       SAY 'Invalid interval minute ('||minute||'), aborting ...' 
  117.       'EXIT' 
  118.    END /* If */ 
  119. END  /* IF */ 
  120. /* 
  121.  *  Set the program running flag to 0 (= not running). 
  122.  */ 
  123. programUp = 0 
  124. connectionFailed = 0 
  125. hour = start / 3600 
  126. PARSE VALUE hour WITH hour '.' minute 
  127. minute = ((start / 3600) - hour) * 60 + 0.5 
  128. PARSE VALUE minute WITH minute '.' second 
  129. SAY 'PPP will start in' hour 'hours and' minute 'minutes.' 
  130. /* 
  131.  *  Now sleep until the start time 
  132.  */ 
  133. IF start > 0 THEN CALL SysSleep(start) 
  134. /* 
  135.  *  Start the main timing loop 
  136.  */
  137. DO FOREVER
  138. /* 
  139.  *  Reset the timer if the program is not currently running 
  140.  */ 
  141.    IF (programUp = 0) & (connectionFailed = 0) THEN CALL TIME 'R' 
  142. /* 
  143.  *  Turn off the IP gate. 
  144.  */ 
  145.    '@IPGATE off' 
  146. /* 
  147.  *  If a program was passed to automatically launch, start the 
  148.  *  "wait for PPP connect" script in the background, passing it 
  149.  *  the name of the queue which will receive output from PPP.EXE. 
  150.  *  By monitoring the queue's contents, we can tell when the PPP 
  151.  *  connection is made. 
  152.  */ 
  153.    IF (program \= '') & programUp = 0 THEN DO 
  154. /* 
  155.  *  Create a queue and set it as the default queue if no queue was created.
  156.  */
  157.       IF newQueue = '' THEN DO
  158.          newQueue = RXQUEUE('CREATE') 
  159.          oldQueue = RXQUEUE('SET', newQueue)
  160.       END /* If */
  161.       'START /B /MIN Wait4PPP.CMD' newQueue program args 
  162. /* 
  163.  *  Start the PPP connection and pipe the output into the write queue 
  164.  */ 
  165.       CALL LINEOUT 'QUEUE:', 'AutoPPP: PPP started' 
  166.       'ppp.exe | rxqueue' newQueue 
  167.       CALL LINEOUT 'QUEUE:', 'AutoPPP: PPP ended' 
  168. /* 
  169.  *  Sleep for 3 seconds to let Wait4PPP process the PPP ended message 
  170.  *  if it is still running. 
  171.  */ 
  172.       CALL SysSleep(3) 
  173.    END /* If */ 
  174. /*       CALL LINEOUT 'QUEUE:'
  175.  *  If there is no program to automatically start or the program 
  176.  *  is already running, just launch PPP. 
  177.  */ 
  178.    ELSE DO
  179.       'ppp.exe'
  180.       IF newQueue \= '' THEN DO
  181.          CALL LINEOUT 'QUEUE:', 'AutoPPP: PPP ended' 
  182.          CALL SysSleep(3) 
  183.       END /* If */
  184.    END /* Else */
  185. /* 
  186.  *  PPP has ended. 
  187.  */ 
  188.    IF program \= '' THEN DO 
  189.       line = '' 
  190.       pppEnded = 0 
  191.       DO UNTIL (programUp = 0) | (pppEnded = 1) | (connectionFailed = 1) 
  192.          IF QUEUED() \= 0 THEN DO 
  193.             DO WHILE QUEUED() \= 0 
  194.                line = LINEIN('QUEUE:') 
  195.                SAY line /* Echo line for diagnostic purposes */ 
  196.                IF POS('Program started', line) > 0 THEN DO 
  197.                   programUp = 1 
  198.                   connectionFailed = 0 
  199.                END /* If */ 
  200.                IF POS('Program ended', line) > 0 THEN programUp = 0 
  201.                IF POS('Timed out', line) > 0 THEN connectionFailed = 1 
  202.                IF POS('PPP ended', line) > 0 THEN pppEnded = 1 
  203.             END /* Do While */ 
  204.          END  /* If */ 
  205.          ELSE 
  206.             CALL SysSleep(1) 
  207.       END /* Do While */ 
  208.       IF programUp = 0 & connectionFailed = 0 THEN DO 
  209. /* 
  210.  * If the program isn't running, destroy the queue, 
  211.  */ 
  212.          CALL RXQUEUE 'SET', oldQueue 
  213.          CALL RXQUEUE 'DELETE', newQueue
  214.          newQueue = ''
  215. /* 
  216.  *  turn on the IP gate, 
  217.  */ 
  218.          '@IPGATE on' 
  219.          wait = interval - TIME('E') 
  220. /* 
  221.  *  and sleep until it's time to run again. 
  222.  */ 
  223.          SAY 'PPP ended at '||TIME() 
  224.          hour = wait / 3600 
  225.          PARSE VALUE hour WITH hour '.' minute 
  226.          minute = ((wait / 3600) - hour) * 60 - 0.5 
  227.          PARSE VALUE minute WITH minute '.' second 
  228.          SAY 'PPP will start in' hour 'hours and' minute 'minutes.' 
  229.          wait = interval - TIME('R') 
  230.          PARSE VALUE wait WITH wait '.' dummy 
  231.          CALL SysSleep(wait) 
  232.       END /* If */ 
  233. /* 
  234.  *  For all other cases, loop back and try to connect again. 
  235.  */ 
  236.    END /* If */ 
  237. END /* do */ 
  238.