home *** CD-ROM | disk | FTP | other *** search
/ ftp.rainsoft.? / ftp.rainsoft.zip / rainsoft / os2 / rainslip.cmd < prev    next >
OS/2 REXX Batch file  |  2012-12-05  |  13KB  |  405 lines

  1. /* REXX ---- RAINSLIP.CMD ------------------------------------------*/
  2. /* OS/2 IAK SLIP RAIN ACCESS REXX file */
  3. /* Heavily modified from slipup.cmd for SLIP access provider   */
  4. /* This rexx wil redial rain every 5 seconds if busy */
  5.  
  6. /* user setup variables */
  7.  
  8. /* Modified INIT string for  Default Modem */
  9. ModemInit='ATZ'   /*** EDIT ModemInit TO SUPPORT YOUR MODEM */
  10.  
  11. /* set the comport you use */
  12. ComPort='com1'   /*** EDIT ComPort FOR COM1-4 YOUR MODEM IS ON */
  13.  
  14. /* unique string for command prompt */
  15. CommandKey=''
  16.  
  17. /* options for the "mode" system call */
  18. ModeOptions='57600,n,8,1,buffer=on,rts=hs,dtr=on'
  19.  
  20. /* does your host ask you what terminal you use? */
  21. /* 1 if it does, 0 if not                        */
  22. AskTerm=0
  23.  
  24. /* delay after BUSY */
  25. BusyDelay=0
  26. MaxBusy=60  /* ATTEMPTS REDAIL 60 TIMES BEFORE GIVING UP */
  27. ElapseSecs = 0
  28. DialtimeOut = 45 /* wait for connect secs */
  29.  
  30. /* delay after NO CARRIER */
  31. NoCDelay=2
  32. Wdialcmd='xxxxxxxxxxxxxxxxxxxx'
  33.  
  34. /* Login COMMAND */
  35. LoginCMD='slip:'
  36.  
  37.  
  38. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysloadFuncs'
  39. call SysLoadFuncs
  40.  
  41. parse arg interface , dialcmd username password
  42.  
  43. /*----------------------------------------------------------------*/
  44. /*                   Initialization and Main Script Code                    */
  45. /*----------------------------------------------------------------*/
  46.  
  47. /* Set some definitions for easier COM strings */
  48. cr='0d'x
  49. crlf='0d0a'x
  50.  
  51. say ''
  52. say 'REXX SLIP Script (interface' interface')'
  53.  
  54. Wdialcmd = dialcmd
  55.  
  56. 'mode 'ComPort||':' ModeOptions   /* set comport speed */
  57.  
  58. call flush_receive  /* Flush any stuff left over from previous COM activity */
  59.  
  60.  
  61. /* dial the number and login */
  62.  
  63. connected=0
  64. nBusy = 0
  65.  
  66. DO FOREVER UNTIL connected 
  67.  
  68.    CALL SetModem
  69.    
  70.    rc = Dial()
  71.  
  72.    call lineout , 'rc=' rc ' Dial Elapse ' ElapseSecs 'secs' 
  73.  
  74.    IF rc \=0 THEN DO
  75.       if rc = 1 then do
  76.         nBusy = nBusy + 1
  77.         if (nBusy > MaxBusy) then do
  78.           call lineout , 'Line BUSY ' MaxBusy 'Times -- Aborting...'
  79.           EXIT 1
  80.         end
  81.         call lineout , 'Busy #' nBusy ', Retry in ' BusyDelay ' secs.'
  82.         CALL SysSleep BusyDelay
  83.         ITERATE
  84.       end
  85.       if rc = 2 then do
  86.         SAY 'Something wrong - NO CARRIER - Retry in ' BusyDelay ' secs.'
  87.         CALL SysSleep BusyDelay
  88.         ITERATE
  89.       end
  90.       if rc = 7 then do
  91.         SAY 'No Connection Time Out -- Retry in ' BusyDelay ' secs.'
  92.         CALL SysSleep BusyDelay
  93.         ITERATE
  94.       end
  95.       SAY 'Something wrong, either VOICE or ERROR returned after - Aborting...'
  96.       EXIT 3
  97.    END
  98.  
  99.   SAY 'Doing Login'
  100.    
  101.    IF Login()=1 THEN DO
  102.       /* hang up */
  103.       'mode '||ComPort||':dtr=off > \dev\nul'
  104.       'mode '||ComPort||':dtr=on > \dev\nul'
  105.       SAY 'Retrying connection after ' BusyDelay ' sec delay.'
  106.       CALL SysSleep BusyDelay
  107.    END 
  108.    ELSE DO
  109.       connected = 1
  110.    END
  111. END 
  112.  
  113. /* Parse the results of the SLIP LOGIN to determine our address.   */
  114. /* We use the "waitfor_buffer" variable from the waitfor routine   */
  115. /* to parse the stuff we get from the Annex after waiting for an   */
  116. /* appropriate point in the data stream.                           */
  117.  
  118. call waitfor 'beginning'
  119. parse var waitfor_buffer . 'from (' a '.' b '.' c '.' d ')' .
  120. annex_address = a||'.'||b||'.'||c||'.'||d 
  121.  
  122.  
  123. parse var waitfor_buffer . 'to ' a '.' b '.' c '.' d ' ' .
  124. os2_address = a||'.'||b||'.'||c||'.'||d 
  125.  
  126. call flush_receive 'echo'
  127.  
  128. /* Now configure this host for the appropriate address, */
  129. /* and for a default route through the Annex.           */
  130.  
  131. say cr || 'SLIP Connection Established'
  132. say 'Configuring local address =' os2_address
  133. say 'R.A.I.N address =' annex_address
  134.  
  135. 'ifconfig sl0' os2_address annex_address 'netmask 255.255.255.0'
  136.  
  137. 'route -f add default' annex_address '1'
  138.  
  139. /* All done */
  140. exit 0
  141.  
  142. /*-------------------------------------------------------------*/
  143. /*                 SetModem()                                 */
  144. /*.........................................................................*/
  145. /* Sets the modem and hopefully everybody is happy when done...            */
  146. /* We have to be very paranoid with "waitfor" or we hang the dialer.       */
  147. /*-------------------------------------------------------------*/
  148. SetModem:
  149.    successful=0
  150.    DO UNTIL successful=1
  151.       call send ModemInit || cr
  152.       if waitfor('OK', 5) = 1 then DO
  153.      call flush_receive 'echo'
  154.      call lineout , 'Modem not resetting... Trying again'
  155.      call send '+++'
  156.      IF waitfor('OK' ,5) = 1 THEN DO
  157.         CALL LineOut , 'could not reset modem.  Check your modem settings!'
  158.         EXIT
  159.      END
  160.       END
  161.       ELSE DO
  162.      CALL flush_receive 'echo'
  163.      successful=1
  164.       END
  165.    END
  166. RETURN 1
  167.  
  168. /*-----------------------------------------------------------*/
  169. /*                 dial()                                     */
  170. /*.........................................................................*/
  171. /* Dials the number with some extra error handling above and beyond the    */
  172. /* normal stuff-                                                       */
  173. /*------------------------------------------------------------*/
  174.                                                                              
  175. dial:
  176.    
  177.    /* Dial the remote server */
  178.       call charout , 'Now Dialing...'
  179.       call send Wdialcmd || cr
  180.    /* catch first cr from modem after dialing */
  181.       rc=waitfor(cr , 10); 
  182.       CALL flush_receive 'echo' 
  183.    /* now parse the return from the modem, give time to return code */
  184. /*      rc=waitfor(cr, 60); */
  185.  
  186.    rc = 0
  187.    waitfor_buffer = '' ; curpos = 1
  188.    ElapseSecs = TIME('R')
  189.  
  190.   do forever
  191.  
  192.       ElapseSecs = TIME('E')
  193.       if (ElapseSecs > DialtimeOut) then do
  194.         CALL flush_receive 'echo'
  195.         rc = 7
  196.         RETURN rc
  197.       end
  198.  
  199.       line = slip_com_input(interface,,10)
  200.       waitfor_buffer = waitfor_buffer || line
  201.       call charout , substr(waitfor_buffer,curpos)
  202.       curpos = length(waitfor_buffer)+1
  203.  
  204.     rc = what()
  205.  
  206.     IF rc = 0 THEN DO
  207.        CALL flush_receive 'echo'
  208.        RETURN rc
  209.     END
  210.  
  211.     IF rc < 8 THEN DO
  212. /*      call lineout , waitfor_buffer */
  213.       CALL flush_receive 'echo'
  214.       RETURN rc
  215.     END
  216.  
  217.   END  /* end forever */
  218.  
  219.   RETURN 8 /* SHOULD NEVER GET HERE */
  220.  
  221. what:
  222.      SELECT;
  223.        WHEN Pos('CONNECT', waitfor_buffer) \= 0 THEN DO
  224.       RETURN 0
  225.        END
  226.        WHEN Pos('BUSY', waitfor_buffer) \= 0 THEN DO
  227.       RETURN 1
  228.        END
  229.        WHEN Pos('NO CARRIER', waitfor_buffer) \= 0 THEN DO
  230.       RETURN 2
  231.        END
  232.        WHEN Pos('ERROR', waitfor_buffer) \= 0 THEN DO
  233.       RETURN 3
  234.        END
  235.        WHEN Pos('NO DIALTONE', waitfor_buffer) \= 0 THEN DO
  236.       RETURN 4
  237.        END
  238.        WHEN Pos('VOICE', waitfor_buffer) \= 0 THEN DO
  239.       RETURN 5
  240.        END
  241.        OTHERWISE DO
  242.       RETURN 8
  243.        END
  244.  
  245. /*---------------------------------------------------------------*/
  246. /*                             login()                                     */
  247. /*.........................................................................*/
  248. /* Handle the login process with the proper error checking and all that.   */
  249. /*---------------------------------------------------------------*/
  250.  
  251. login:
  252. /* Handle login.  We wait for standard strings, and then flush anything */
  253. /* else to take care of trailing spaces, etc..                          */
  254.  
  255. /*   call send cr */
  256.  
  257.    IF waitfor('ogin:',60)=1 THEN DO
  258.       call flush_receive 'echo'
  259.       SAY crlf||'No login RQ.'
  260.       RETURN 1
  261.    END
  262.    ELSE DO 
  263.       call flush_receive 'echo'
  264.       call send LoginCMD || username || cr
  265.    
  266.      IF waitfor('Password:',15)=1 THEN DO
  267.         SAY crlf||'No Password RQ.'
  268.         RETURN 1
  269.      END
  270.      ELSE DO
  271.         call flush_receive 'echo'
  272.         call send password || cr
  273.      END
  274.    END
  275.  
  276. RETURN 0
  277.  
  278. /*----------------------------------------------------------*/
  279. /*                            send ( sendstring)                            */
  280. /*..........................................................................*/
  281. /*                                                                          */
  282. /* Routine to send a character string off to the modem. */
  283. /*                                                                          */
  284. /*-----------------------------------------------------*/
  285.  
  286. send:
  287.  
  288.    parse arg sendstring
  289.    call slip_com_output interface , sendstring
  290.  
  291.    return
  292.  
  293.  
  294. /*------------------------------------------------------*/
  295. /*                    waitfor ( waitstring , [timeout] )                    */
  296. /*..........................................................................*/
  297. /*                                                                          */
  298. /* Waits for the supplied string to show up in the COM input.  All input    */
  299. /* from the time this function is called until the string shows up in the   */
  300. /* input is accumulated in the "waitfor_buffer" variable.                   */
  301. /*                                                                          */
  302. /* If timeout is specified, it says how long to wait if data stops showing  */
  303. /* up on the COM port (in seconds).                                                         
  304.  
  305. */
  306. /*                                                                          */
  307. /*-------------------------------------------------------*/
  308.  
  309. waitfor:
  310.  
  311.    parse arg waitstring , timeout
  312.  
  313.    if timeout = '' then
  314.      timeout = 5000    /* L O N G   delay if not specified */
  315.      waitfor_buffer = '' ; done = -1; curpos = 1
  316.      ORI_TIME=TIME('E')
  317.  
  318.    if (remain_buffer = 'REMAIN_BUFFER') then do
  319.       remain_buffer = ''
  320.    end
  321.  
  322.    do while (done = -1)
  323.       if (remain_buffer \= '') then do
  324.          line = remain_buffer
  325.          remain_buffer = ''
  326.        end
  327.        else do
  328.          line = slip_com_input(interface,,10)
  329.       end
  330.       waitfor_buffer = waitfor_buffer || line
  331.       index = pos(waitstring,waitfor_buffer)
  332.       if (index > 0) then do
  333.          remain_buffer = substr(waitfor_buffer,index+length(waitstring))
  334.          waitfor_buffer = delstr(waitfor_buffer,index+length(waitstring))
  335.          done = 0
  336.       end
  337.       call charout , substr(waitfor_buffer,curpos)
  338.       curpos = length(waitfor_buffer)+1
  339.       if ((done \= 0) & (TIME('E')>timeout)) then do
  340.         call lineout , ' WAITFOR: timed out '
  341.         done = 1
  342.        end
  343.    end
  344.  
  345.    timeout=0
  346.    RC=done
  347.  return RC
  348.  
  349.  
  350.  
  351. /*-----------------------------------------------------*/
  352. /*                               readpass ()                                */
  353. /*..........................................................................*/
  354. /*                                                                          */
  355. /* Routine used to read a password from the user without echoing the        */
  356. /* password to the screen.                                        */
  357. /*                                                                          */
  358. /*---------------------------------------------------------*/
  359.  
  360. readpass:
  361.  
  362.   answer = ''
  363.   do until key = cr
  364.     key = slip_getch()
  365.     if key \= cr then do
  366.       answer = answer || key
  367.     end
  368.   end
  369.   say ''
  370.   return answer
  371.  
  372.  
  373. /*-----------------------------------------------------*/
  374. /*                             flush_receive ()                             */
  375. /*..........................................................................*/
  376. /*                                                                          */
  377. /* Routine to flush any pending characters to be read from the COM port.    */
  378. /* Reads everything it can until nothing new shows up for 100ms, at which   */
  379. /* point it returns.                                                        */
  380. /*                                                                          */
  381. /* The optional echo argument, if 1, says to echo flushed information.      */
  382. /*                                                                          */
  383. /*----------------------------------------------------------------*/
  384.  
  385. flush_receive:
  386.  
  387.    parse arg echo
  388.  
  389.    /* If echoing the flush - take care of waitfor remaining buffer */
  390.    if (echo \= '') & (length(remain_buffer) > 0) then do
  391.       call charout , remain_buffer
  392.       remain_buffer = ''
  393.    end
  394.  
  395.    /* Eat anything left in the modem or COM buffers */
  396.    /* Stop when nothing new appears for 100ms.      */
  397.  
  398.    do until line = ''
  399.      line = slip_com_input(interface,,100)
  400.      if echo \= '' then
  401.         call charout , line
  402.    end
  403. return
  404. /* --------------End of rainslip.cmd file---------------------------*/
  405.