home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / kcg.zip / KCG.CMD < prev    next >
OS/2 REXX Batch file  |  1999-06-20  |  14KB  |  444 lines

  1. /***** KCG.CMD for OS/2:  "Keep-Connected" to Internet
  2. Freeware  R.J.Holmgren  6/20/99
  3.  
  4. Please read KCG.TXT for full description and instructions!
  5.  
  6. The Keep-Connected Gimmick (KCG) is a flexible, diagnostic tool to
  7. determine the maximum amount of inactive time that an ISP will allow, and
  8. then to keep connected to the ISP with minimal load on both your CPU and
  9. your Internet bandwidth.  KCG prevents dial-up ISPs from disconnecting
  10. users for "idleness".
  11.  
  12.   Requirements:
  13.   ------------
  14. OS/2 Rexx
  15. RXFTP.DLL v2.0(+) located in the LIBPATH
  16. PING.EXE and RXQUEUE.EXE located in the PATH
  17.  
  18.   Command [nine optional arguments]:
  19.   -------
  20. kcg hostname|IPaddress
  21.       user_ID
  22.         password
  23.           minimum_interval_between_hits (in seconds)
  24.             maximum_interval_between_hits (in seconds)
  25.               increment_amount (increase laziness, in seconds)
  26.                 L|m (progressive "L"aziness, or "M"aximum interval only)
  27.                   F|p (use "F"TP or "P"ING method)
  28.                     A|d (run as "A"ttached, or "D"etached, process)
  29.   e.g.
  30. kcg 152.163.212.93 anonymous mikhail@bakunin.org.ru 120 660 60 L F A  [defaults]
  31. kcg ftp.aol.com anonymous mikhail@bakunin.org.ru  [as above but slower]
  32. kcg ftp.microsoft.com
  33. kcg 207.46.133.140  [same as "ftp.microsoft.com" above but faster]
  34. detach kcg.cmd idt.net . . . 510 . . . D [override defaults 1,5,&9; Detach KCG]
  35. kcg 192.20.225.4 . me@myisp.com  [override 1 ("ftp.research.att.com") & 3]
  36.  
  37. Arguments may be hard-coded in USER SETTINGS, below
  38.  
  39. *****/
  40.  
  41. /* BEGIN USER SETTINGS: Enter your personal or preferred settings below */
  42. myISP = '152.163.212.93'   /* Default myISP = 'ftp.aol.com' */
  43. myuserID = 'anonymous'
  44. mypassword = 'mikhail@bakunin.org.ru'
  45. myminwait = 120  /* specified in seconds */
  46. mymaxwait = 660  /* seconds */
  47. myincrement = 60 /* seconds */
  48. mymaxint = 'L'   /* 'L'azy; or use 'M'aximum interval only */
  49. mymethod = 'F'   /* 'F'TP; or 'P'ING only */
  50. mydetach = 'A'   /* 'A'ttached; or 'D'etached (no screen output) */
  51. retries = 20     /* If [my]method='P'ING but Host doesn't reply, number of
  52.                     times to retry PING before giving up */
  53. logfile = 'Y'    /* Write logfile KCG.LOG: 'Y'es or 'N'o */
  54.  
  55. rxftp=0
  56. rxutl=RxFuncQuery("SysLoadFuncs")
  57. if rxutl\=0 then do
  58.   call RxFuncAdd "SysLoadFuncs","RexxUtil","SysLoadFuncs"
  59.   call SysLoadFuncs
  60.   end
  61. parse arg host id pw minwait maxwait incrmnt maxint method detach
  62. if minwait=''|minwait='.' then minwait=myminwait
  63. if verify(minwait,'0123456789.')\=0 then minwait=120
  64. minwait=trunc(minwait)
  65. if maxwait=''|maxwait='.' then maxwait=mymaxwait
  66. if verify(maxwait,'0123456789.')\=0 then maxwait=660
  67. maxwait=trunc(maxwait)
  68. if incrmnt=''|incrmnt='.' then incrmnt=myincrement
  69. if verify(incrmnt,'0123456789.')\=0 then incrmnt=60
  70. incrmnt=trunc(incrmnt)
  71. perminc=incrmnt
  72. if verify(retries,'0123456789.')\=0|retries<1 then retries=10
  73. retries=trunc(retries)
  74. if maxint=''|maxint='.' then maxint=mymaxint
  75. if translate(maxint)='M' then do
  76.   maxint='M'
  77.   minwait=maxwait
  78.   end
  79. else maxint='L'
  80. if method=''|method='.' then method=mymethod
  81. if translate(method)='P' then method='PING'
  82. else method='FTP'
  83. if detach=''|detach='.' then detach=mydetach
  84. if translate(detach)='D' then do
  85.   detach='D'
  86.   minwait=maxwait
  87.   end
  88. else detach='A'
  89. if minwait>maxwait then minwait=maxwait
  90. if minwait=maxwait then incrmnt=0
  91. if host=''|host='.' then host=myISP
  92. if id=''|id='.' then id=myuserID
  93. if id=''|id='.' then id='anonymous'
  94. if pw=''|pw='.' then pw=mypassword
  95. if host=''|host='.'|host='?'|host='/?'|host='-?'|translate(host)='HELP' then call Help
  96. if method='FTP' then do
  97.   rxftp=RxFuncQuery("FtpLoadFuncs")
  98.   if rxftp\=0 then do
  99.     call RxFuncAdd "FtpLoadFuncs","rxFtp","FtpLoadFuncs"
  100.     call FtpLoadFuncs
  101.     end
  102.   end
  103.  
  104. a=''
  105. curwait=120
  106. init=0
  107. lastcon=0
  108. lastincr=0
  109. lastwait=0
  110. lastwait2=0
  111. logdel=-1
  112. pmbegin=time('N');pmdate=date()
  113. totmin=0
  114. totwait=0
  115.  
  116. log=directory()
  117. if length(log)>3 then log=log||'\'
  118. log=log||'KCG.LOG'
  119. r=stream(log,'C','query exists')
  120. if length(r)>0 then logdel=SysFileDelete(log)
  121.  
  122. if method='FTP' then call FtpSetUser host,id,pw
  123. if method='PING' then '@echo off'
  124.  
  125. do forever
  126.   if detach='A' then call SysCls
  127.   if init<2 then do
  128.     if detach='A' then do
  129.       call Charout ,d2c(255)||'Pinging to verify connection...        '
  130.       call Posit
  131.       end
  132.     c=''
  133.     conn=0
  134.     bt=time('E')
  135.     if method='PING' then do until c\=2
  136.       c=0
  137.       '@rxqueue.exe /CLEAR'
  138.       '@ping.exe' host '8 1 | rxqueue.exe'
  139.       do while queued()\=0
  140.         parse pull line
  141.         if pos('16 bytes from',line)>0 then do
  142.           c=1  /* Connected */
  143.           leave
  144.           end
  145.         if pos('ret=-1',line)>0 then leave  /* Disconnected */
  146.         if pos('unknown host',line)>0 then do  /* Bad IPaddress */
  147.           c=3
  148.           leave
  149.           end
  150.         if pos('0 packets received',line)>0 then c=2  /* No response */
  151.         end
  152.       if c\=2 then leave
  153.       conn=conn+1
  154.       if detach='A' then do
  155.         call SysCurPos l,r
  156.         say 'Ping' conn '(of' retries||')'
  157.         call SysCurState Off
  158.         end
  159.       if conn=retries then leave
  160.       end
  161.     else c=FtpPing(host,1)
  162.     end
  163.   if method='PING' then do
  164.     '@rxqueue.exe /CLEAR'
  165.     if c>0 then call Yes
  166.     else do
  167.       if init<2&detach='A' then do
  168.         call SysCurPos l,r
  169.         say copies(' ',31)
  170.         call SysCurPos l,r
  171.         say 'Not connected'
  172.         end
  173.       call No
  174.       end
  175.     end
  176.   else do
  177.     if left(c,4)='PING' then do
  178.       if c\='PINGREPLY'&c\='PINGRECV' then do
  179.         if init<2&detach='A' then say 'Not connected: "'||c||'"'
  180.         call No
  181.         end
  182.       else call Yes
  183.       end
  184.     else call Yes
  185.     end
  186.   totwait=totwait+curwait
  187.   if detach='A' then do
  188.     tt=time('N');t=time('S')
  189.     say d2c(255)
  190.     say d2c(255)||'        KCG.CMD for OS/2  6/20/99'
  191.     say d2c(255)
  192.     call Charout ,d2c(255)||'Currently '||a||'CONNECTED by '||method||':            '
  193.     text='"'||host||'" as "'||id||'"'
  194.     call Format
  195.     say d2c(255)||"Keep-Connected Gimmick launched at:        "||pmbegin "on" pmdate
  196.     if init>0 then do
  197.       call Charout ,d2c(255)||'  Connection detected at:            '||connect
  198.       if begdate\=pmdate then call Charout ,' on' begdate
  199.       say
  200.       if init=2 then do
  201.         call Charout ,d2c(255)||'  Disconnection detected at:            '||tt
  202.         if enddate\=pmdate then call Charout ,' on' enddate
  203.         say
  204.         call Charout ,d2c(255)||'  Duration of connection:            '
  205.         text='between '||trunc(totmin,1)-trunc(lastwait2/60,1)||' and '||trunc(totmin,1)||' minutes ['||trunc(totmin/60,1)-trunc(lastwait2/3600,1)||'-'||trunc(totmin/60,1)||' hours]'
  206.         if pos('[0-',text)>0 then do
  207.           t=pos('[0-',text)+1
  208.           text=left(text,t)||'.0'||right(text,length(text)-t)
  209.           end
  210.         call Format
  211.         end
  212.       end
  213.     if init<2 then do
  214.       say d2c(255)||'Elapsed time of current '||a||'connection:        '||trunc(totmin/60,2) 'hours|'||totmin||' minutes'
  215.       t=t+curwait
  216.       d=0
  217.       if t>86399 then do
  218.         d=trunc(t/86400)
  219.         t=t-d*86400
  220.         end
  221.       h=trunc(t/3600)
  222.       m=trunc((t-h*3600)/60)
  223.       s=t-h*3600-m*60
  224.       if h<10 then h='0'||h
  225.       if m<10 then m='0'||m
  226.       if s<10 then s='0'||s
  227.       call Charout ,d2c(255)||'Last attempt to contact Host at:        '
  228.       text=tt||' (next at' h||':'||m||':'||s
  229.       if d>0 then text=text||' +'||d2c(255)||d||d2c(255)||'day'
  230.       if d>1 then text=text||'s'
  231.       text=text||')'
  232.       call Format
  233.       if init=1 then do
  234.         lastcon=trunc(et,2)
  235.         say d2c(255)||'  Duration of last contact:            '||lastcon 'seconds'
  236.         call Charout ,'1B'x'[1;37;40m'
  237.         end
  238.       call Charout ,d2c(255)||'  Current interval between contact attempts:    '
  239.       text=trunc(curwait/60,2)||' min|'||curwait 'sec'
  240.       if init=1 then do
  241.         if maxint='L' then text=text||' (max='||maxwait||d2c(255)||'secs)'
  242.         else text=text||' (constant interval)'
  243.         end
  244.       call Format
  245.       if init=1 then call Charout ,'1B'x'[0;37;40m'
  246.       if init=1&incrmnt>0 then say d2c(255)||'  Getting lazier (incrementing interval) in:    '||incrmnt 'second steps'
  247.       say
  248.       end
  249.     end
  250.   if init=2 then do
  251.     call Charout ,'1B'x'[1;37;40m'||d2c(255)||'  Last successful interval between hits:    '||lastwait 'seconds|'||trunc(lastwait/60,1) 'minutes'
  252.     say '1B'x'[0;37;40m'
  253.     say d2c(255)||'  Final [failed]  interval between hits:    '||lastwait2 'seconds|'||trunc(lastwait2/60,1) 'minutes'
  254.     say d2c(255)||'  Duration of last successful contact:        '||lastcon 'seconds'
  255.     say d2c(255)||'  Duration of final contact:            '||trunc(et,2) 'seconds'
  256.     call Charout ,d2c(255)||'  When disconnected, incremental step was:    '
  257.     if lastwait2>lastwait|maxint='L'&lastwait2=minwait&lastincr>0 then text=lastincr||' seconds'
  258.     else do
  259.       if maxint='L' then text='Not in effect [was '||perminc||' secs]'
  260.       else text='N/A [constant interval]'
  261.       end
  262.     call Format
  263.     call Charout ,d2c(255)
  264.     if detach='A'&translate(logfile)='Y' then do
  265.       if logdel>2 then do
  266.         call Charout ,'1B'x'[1;37;40m'||'ERROR writing' log||d2c(13)||d2c(10)||' '
  267.         call Charout ,'1B'x'[0;37;40m'
  268.         end
  269.       if logdel>2 then leave
  270.       text=SysTextScreenRead(0,0,1360)
  271.       r=' '||d2c(255)
  272.       t=pos(r,text)
  273.       do while t>0
  274.         text=left(text,t-1)||right(text,length(text)-t)
  275.         t=pos(r,text)
  276.         end
  277.       do until r=''
  278.         t=pos(d2c(255),text)
  279.         r=right(text,length(text)-t)
  280.         if pos(d2c(255),r)<1 then r=''
  281.         text=left(text,t-1)||d2c(13)||d2c(10)||r
  282.         end
  283.       text=right(text,length(text)-4)
  284.       text=left(text,length(text)-2)
  285.       call stream log,'C','open write'
  286.       call lineout log,text,1
  287.       call stream log,'C','close'
  288.       end
  289.     end
  290.   if init=2 then leave
  291.   if detach='A' then call SysCurState Off
  292.   call SysSleep curwait
  293.   totmin=trunc(totwait/60,2)
  294.   if init=1 then do
  295.     if lastwait2>0 then lastwait=lastwait2
  296.     else lastwait=minwait
  297.     lastwait2=curwait
  298.     if curwait<maxwait then do
  299.       curwait=curwait+incrmnt
  300.       lastincr=incrmnt
  301.       end
  302.     if curwait=maxwait|curwait>maxwait then curwait=maxwait
  303.     if curwait=maxwait then incrmnt=0
  304.     end
  305.   end
  306. say 'Exit KCG.CMD'
  307. call Ex
  308.  
  309. EX:
  310. if rxftp\=0 then call FtpDropFuncs
  311. if rxutl\=0 then call RxFuncDrop 'SysLoadFuncs'
  312. exit
  313. RETURN
  314.  
  315. FORMAT:
  316.   if length(text)>31 then do
  317.     tab=''
  318.     sep=' '
  319.     do until length(text)<32
  320.       lt=lastpos(sep,text,32)-1
  321.       if lt<0 then do
  322.         if sep=' ' then do
  323.           sep='.'
  324.           lt=lastpos(sep,text,32)-1
  325.             if lt<0 then do
  326.             say tab||text
  327.             RETURN
  328.             end
  329.           end
  330.         else do
  331.           say tab||text
  332.           RETURN
  333.           end
  334.         end
  335.       say tab||left(text,lt)
  336.       text=right(text,length(text)-lt)
  337.       tab=d2c(255)||'                        '
  338.       end
  339.     say tab||text
  340.     end
  341.   else say text
  342. RETURN
  343.  
  344. POSIT:
  345. clr=SysCurPos()
  346. r=pos(' ',clr)
  347. l=left(clr,r-1)
  348. r=right(clr,length(clr)-r)
  349. RETURN
  350.  
  351. NO:
  352. a='DIS'
  353. if init=1 then do
  354.   et=time('E')-bt;enddate=date()
  355.   call Beep 400,200
  356.   call Beep 300,200
  357.   if detach='D' then call Ex
  358.   call SysCls
  359.   init=2
  360.   end
  361. RETURN
  362.  
  363. YES:
  364. if method='PING' then et=time('E')-bt
  365. if detach='A' then do
  366.   call SysCurPos l,r
  367.   say copies(' ',31)
  368.   call SysCurPos l,r
  369.   text='Connected'
  370.   if method='FTP' then text=text||':  "'||c||'"'
  371.   if method='PING' then do
  372.     if c>1 then text=text||' (WARNING: Host '
  373.     if c=2 then text=text||'not responding)'
  374.     if c=3 then text=text||'unknown [invalid IPaddress])'
  375.     end
  376.   call Format
  377.   end
  378. if init=0 then call Beep 3000,100
  379. if init=1&method='PING'&c>1 then do
  380.   call Beep 3000,200
  381.   call Beep 400,200
  382.   end
  383. if method='FTP' then do
  384.   if detach='A' then do
  385.     call Charout ,d2c(255)||'Querying Host to keep active...        '
  386.     call Posit
  387.     end
  388.   b=''
  389.   b=FtpSys(d)
  390.   et=time('E')-bt
  391.   if b='-1' then do
  392.     if detach='A' then do
  393.       call Beep 400,200
  394.       call Beep 300,200
  395.       call SysCurPos l,r
  396.       text='FTP '||host||' declines connection, or invalid IPaddress: KCG.CMD aborting'
  397.       call Format
  398.       end
  399.     call Help
  400.     end
  401.   if detach='A' then do
  402.     call SysCurPos l,r
  403.     text='Host OpSys: "'||b||'"'
  404.     call Format
  405.     end
  406.   end
  407. if init=0 then do
  408.   connect=time('N');begdate=date()
  409.   call Beep 3150,40
  410.   a=''
  411.   curwait=minwait
  412.   init=1
  413.   lastincr=incrmnt
  414.   totmin=0
  415.   totwait=0
  416.   end
  417. RETURN
  418.  
  419. HELP:
  420. if detach='A' then do
  421.   say
  422.   say 'KCG.CMD for OS/2:  "Keep-Connected" to Internet'
  423.   say '    R.J.Holmgren  6/20/99  Freeware'
  424.   say
  425.   say 'kcg.cmd [arguments (9)]'
  426.   say '    hostname|IPaddress'
  427.   say '      user_ID'
  428.   say '        password'
  429.   say '          minimum_interval_between_hits (in seconds)'
  430.   say '        maximum_interval_between_hits (in seconds)'
  431.   say '          increment_amount (increase laziness, in seconds)'
  432.   say '            L|M (progressive "L"aziness, or "M"aximum interval only)'
  433.   say '              F|P (use "F"TP, or "P"ING only)'
  434.   say '            A|D (run as "A"ttached, or "D"etached, process)'
  435.   say 'kcg ?|/?|-?|help  command summary (this screen)'
  436.   say 
  437.   say '  e.g.:'
  438.   say 'kcg 152.163.212.93 anonymous mikhail@bakunin.org.ru 120 660 60 L F A [default]'
  439.   say 'detach kcg.cmd idt.net . . . 510 . . . D [override defaults 1,5,&9; Detach KCG]'
  440.   say 'kcg . . . . 750 . M  [override 5&7; use Maximum interval only, of 750 seconds]'
  441.   end
  442. call Ex
  443. RETURN
  444.