home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / dhcpxref.zip / DHCPXREF.CMD
OS/2 REXX Batch file  |  2002-02-07  |  9KB  |  304 lines

  1. /**********************************************************************
  2.  * DHCPXREF
  3.  * by Peter Degotardi
  4.  * Revised MAY 6, 1998                                     
  5.  *
  6.  * DHCPXREF
  7.  * Return values: 0=successful, 1=Problem with NSSIG
  8.  *                              2=Problem with DADMIN
  9.  *
  10.  * Use NSSIG and DADMIN to build a list of DHCP leases and DNS 
  11.  * entries then cross-reference them to show leases that don't have
  12.  * a corresponding DNS entry, and DNS entries that don't have a lease
  13.  **********************************************************************/
  14. parse upper arg sort_type
  15.  
  16. if sort_type = '/?' then signal syntax
  17. if sort_type = '/A' then
  18.     ip_sort = 1
  19. else
  20.     ip_sort = 0
  21.  
  22. call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  23. call SysLoadFuncs
  24.  
  25. boot_drive = filespec('drive', value('comspec', ,'os2environment'))
  26. named_file = boot_drive'\mptn\etc\namedb\named.dmp'
  27.  
  28. /* Add a few colours */
  29. esc    = '1B'x
  30. yellow = esc || '[33;1m'
  31. red    = esc || '[31;1m'
  32. white  = esc || '[37;1m'
  33. normal = esc || '[37;0m'
  34.  
  35. numeric digits 12
  36. nssig_host.0    = 0
  37. nssig_address.0 = 0
  38. nssig_text.0    = 0
  39. nssig_leased.0  = 0
  40. dadmin_address.0= 0
  41. dadmin_named.0  = 0
  42. output.0 = 0
  43. h = 0
  44.  
  45. do queued(); pull .; end    /* Make sure queue is empty */
  46. call do_nssig            /* Process NSSIG -INT command */
  47. call do_dadmin            /* Process DADMIN -S command */
  48. call do_xref            /* Cross-reference both outputs */
  49. call do_combine            /* Make one list */
  50. call do_sort            /* Sort the list */
  51.  
  52. /* Output the list */
  53. call SysCls
  54. say left('-', 79, '-')
  55. say '* Leased/Static has DNS entry  ',
  56.      yellow'*'normal 'Leased/Static but no DNS entry  ',
  57.      red'*'normal 'No lease'
  58. say left('-', 79, '-')
  59. say 'Address         Registered Name' 
  60. say '                 Description'
  61. say left('--------------- ', 79, '-')
  62.     do h = 1 to output.0
  63.     parse value output.h with colour','address','host','text
  64.     say colour || left(address,15,' ') host text normal
  65.     end
  66. exit 0
  67.  
  68.  
  69. /**********************************************************************
  70.  * Run the NSSIG -INT command
  71.  * Read through the %ETC%\NAMED.DMP file created, for each line read:-
  72.  * IF not a comment line
  73.  *   IF $ORIGIN entry
  74.  *     keep track of $ORIGIN
  75.  *   ELSE
  76.  *     IF left char. is not a ' ' get the host name and the record type
  77.  *       IF we don't have IP address for this host
  78.  *         initialise stem entries
  79.  *       get the host name and record type
  80.  *     ELSE
  81.  *       get the record type
  82.  *     IF record type is 'A'
  83.  *       get the hosts IP address
  84.  *     IF record type is 'TXT'
  85.  *       get the txt entry
  86.  **********************************************************************/
  87. do_nssig:
  88. say 'Running NSSIG'
  89. call SysFileDelete named_file
  90. '@NSSIG -INT >nul 2>&1'
  91.  
  92. call SysSleep(2)
  93. say 'Reading input file 'named_file
  94. rc = stream(named_file, 'c', 'open read')
  95.  
  96. if rc \= 'READY:' then
  97.     do
  98.     say 'Could not read input file. rc='rc
  99.     say 'Possible problem running NSSIG -INT'
  100.     exit 1
  101.     end
  102. else
  103.     do while lines(named_file)
  104.     input = translate(linein(named_file), ' ', '09'x)
  105.     if left(input, 1) \= ';' then
  106.         do
  107.         if left(input, 7) = '$ORIGIN' then
  108.             parse value input with . origin
  109.         else
  110.             do
  111.             if left(input, 1) \= ' ' then
  112.                 do
  113.                 if nssig_address.h \= '' then
  114.                     do
  115.                     h = h + 1
  116.                     nssig_address.0 = h
  117.                     nssig_address.h = ''
  118.                     nssig_text.h = ''
  119.                     nssig_leased = 0
  120.                     end
  121.                 parse value input with nssig_host.h'IN' type more_data
  122.                 parse value nssig_host.h with nssig_host.h .
  123.                 nssig_host.h = nssig_host.h'.'origin
  124.                 end
  125.             else
  126.                 do
  127.                 parse value input with .'IN' type more_data
  128.                 end
  129.             end
  130.         if type = 'A' then 
  131.             do
  132.             parse value more_data with nssig_address.h .
  133.             end
  134.         if type = 'TXT' then 
  135.             do
  136.             parse value more_data with nssig_text.h';'.
  137.             parse value nssig_text.h with '"'nssig_text.h'"'
  138.             end
  139.         end
  140.     end
  141. rc = stream(named_file, 'c', 'close')
  142. if nssig_address.h = '' then nssig_host.0 = nssig_host.0 - 1
  143. return
  144.  
  145. /**********************************************************************
  146.  * Run DADMIN -s command, put all output into a queue
  147.  * FOR each item in the queue
  148.  *   IF item is LEASED, RESERVERD or N/A
  149.  *     add it to stem
  150.  **********************************************************************/
  151. do_dadmin:
  152. say 'Running DADMIN'
  153. '@dadmin -s | rxqueue'
  154. if queued() < 6 then
  155.     do
  156.     say 'Not enough data returned by DADMIN'
  157.     exit 2
  158.     end
  159. else
  160.     do 6; pull .; end    /* Ignore the headers */
  161. h = 0
  162.     do queued()
  163.     pull input
  164.  
  165.     if pos('LEASED', input) \= 0 | pos('RESERVED', input) \= 0 | pos('N/A', input) \= 0 then
  166.         do
  167.         h = h + 1
  168.         dadmin_address.0 = h
  169.         parse value input with dadmin_address.h .
  170.         dadmin_named.h = 0
  171.         end
  172.     end
  173. return
  174.  
  175. /**********************************************************************
  176.  * FOR every name found by NSSIG
  177.  *   FOR every address found by DADMIN
  178.  *     IF the two ip addresses are identical
  179.  *       flag that NSSIG entry has a lease
  180.  * FOR every address found by DADMIN
  181.  *   FOR every name found by NSSIG
  182.  *     IF the two ip addreses are identical
  183.  *       flag that DADMIN entry has a name
  184.  **********************************************************************/
  185. do_xref:
  186.     do p1 = 1 to nssig_address.0
  187.         do p2 = 1 to dadmin_address.0
  188.         if nssig_address.p1 = dadmin_address.p2 then
  189.             do
  190.             nssig_leased.p1 = 1
  191.             iterate
  192.             end
  193.         end
  194.     end
  195.     do p1 = 1 to dadmin_address.0
  196.         do p2 = 1 to nssig_address.0
  197.         if dadmin_address.p1 = nssig_address.p2 then
  198.             do
  199.             dadmin_named.p1 = 1
  200.             iterate
  201.             end
  202.         end
  203.     end
  204. return
  205.  
  206. /**********************************************************************
  207.  * Build 1 list for output from the 2 we already have
  208.  *
  209.  * initialise output list to be the same size as the NSSIG list
  210.  *   FOR each item in the NSSIG list
  211.  *     IF it has a lease
  212.  *       it is normal in colour
  213.  *     ELSE
  214.  *       it is red
  215.  *     add the address and name
  216.  *     IF there is a TXT entry
  217.  *       add the TXT entry
  218.  *   FOR each item in the DADMIN list
  219.  *     IF it doesn't have a DNS entry
  220.  *       it is yellow in colour
  221.  *     ELSE
  222.  *       ignore it as it's already in the output list (from NSSIG)
  223.  *     append a ',' so that sorting and output will work ok
  224.  **********************************************************************/
  225. do_combine:
  226. output.0 = nssig_address.0
  227.     do p1 = 1 to nssig_address.0
  228.     if nssig_leased.p1 = 1 then
  229.         output.p1 = normal','
  230.     else
  231.         output.p1 = red','
  232.     output.p1 = output.p1 || nssig_address.p1','nssig_host.p1','
  233.  
  234.     if nssig_text.p1 \= '' then
  235.         output.p1 = output.p1'0d0a'x left(' ', 15, ' ') nssig_text.p1
  236.     end
  237.     do p2 = 1 to dadmin_address.0
  238.     if dadmin_named.p2 = 0 then
  239.         do
  240.         p1 = output.0 + 1
  241.         output.0 = p1
  242.         output.p1 = yellow','
  243.         end
  244.     else
  245.         iterate        /* We already have this address */
  246.     output.p1 = output.p1 || dadmin_address.p2',' ','
  247.     end
  248. return
  249.  
  250. /**********************************************************************
  251.  * Sort the output list
  252.  * Go the bubble sort!
  253.  * 
  254.  * FOR every item in the output list
  255.  *   FOR every item in the output list
  256.  *     IF output is sorted by address
  257.  *       get the first item - which is an ip address
  258.  *       turn the ip address into a 12 digit number with no periods
  259.  *       get the second item - which is also an ip address
  260.  *       turn the ip address into a 12 digit number with no periods
  261.  *     ELSE
  262.  *       get the first item - which is a host name
  263.  *       get the second item - which is also a host name
  264.  *     IF first item is smaller than second item
  265.  *       swap them
  266.  **********************************************************************/
  267. do_sort: procedure expose output. ip_sort
  268.     do p1 = 1 to output.0
  269.         do p2 = 1 to output.0
  270.         if ip_sort then
  271.             do
  272.             parse value output.p1 with .','item1','.
  273.             parse value item1 with a1'.'a2'.'a3'.'a4
  274.             item1 = right(a1,3,'0') || right(a2,3,'0') || right(a3,3,'0') || right(a4,3,'0')
  275.  
  276.             parse value output.p2 with .','item2','.
  277.             parse value item2 with a1'.'a2'.'a3'.'a4
  278.             item2 = right(a1,3,'0') || right(a2,3,'0') || right(a3,3,'0') || right(a4,3,'0')
  279.             end
  280.         else
  281.             do
  282.             parse value output.p1 with .','.','item1','.
  283.             parse value output.p2 with .','.','item2','.
  284.             end
  285.         if item1 < item2 then
  286.             do
  287.             item_tmp = output.p1
  288.             output.p1 = output.p2
  289.             output.p2 = item_tmp
  290.             end
  291.         end
  292.     end
  293. return
  294.  
  295. /**********************************************************************
  296.  * Show the syntax
  297.  **********************************************************************/
  298. syntax:
  299. say 'DUMPER [/A]'
  300. say '       /A    Sort by address'
  301. say ''
  302. say '       Default is sort by name'
  303. exit
  304.