RxNsLookUp - просмотр DNS

Автор: VicTor Smirnoff

Дата: 2001

Скачать в ZIP архиве.

/**************************************************************************/
/* RxNsLookUp.cmd - просмотр DNS                                          */
/*                                                                        */
/* Синтаксис: RxNsLookUp [<адрес>    ] [/l] [/i] [/m]                     */
/*                       [@[<список>]]                                    */
/*                                                                        */
/* Параметры: адрес - Inet-адрес (например, www.somebody.net),            */
/*                    IP-адрес (например, 192.168.0.127),                 */
/*                    шаблон IP-адреса (например, 192.168.0.*),           */
/*                    диапазон IP-адресов (например, 192.168.0.0+127      */
/*                       или 192.168.0.0-192.168.0.127).                  */
/*      @[
] - получать адреса из файла <список> или из stdin,     */
/*                    если имя файла опущено                              */
/*           список - имя файла, содержащего список адресов               */
/*                    (Inet, IP, шаблонов или диапазонов).                */
/*                    Например:                                           */
/*                       :комментарий                                     */
/*                       www.somebody.net                                 */
/*                       192.168.0.23                                     */
/*                       192.168.0.*                                      */
/*                       192.168.0.1-192.168.0.127                        */
/*                       192.168.0.128+127                                */
/*               /l - вывод результатов в виде списка (иначе, протокол).  */
/*               /i - игнорировать сообщения об ошибках.                  */
/*               /m - отображать процесс обработки на STDERR.             */
/*   без параметров - вывод подсказки.                                    */
/*                                                                        */
/*========================================================================*/
/* (c) VicTor Smirnoff, 2001                         sva@water.karelia.ru */
/**************************************************************************/
trace off
numeric digits 12

parse arg xParm
xParm = space( strip( xParm ), 1 )
if ( xParm = '' ) then xHowUse()
rc = xParseParm( xParm )
xMask = ''

if xList = 0 then
   do
   say ':'
   say substr( ': '||Date('L')||' ('||Time('C')||')',1,62,' ')||'RxNsLookUp v0.01'
   say  ': Parameters: '||xParm
   say substr( ':=', 1, 78, '=' )
   end

/**************************************************************************/
/* Инициализация функций                                                  */
/**************************************************************************/
if RxFuncQuery( 'SysLoadFuncs' ) then
   do
   rc = RxFuncAdd( 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs' )
   rc = SysLoadFuncs()
   end
if RxFuncQuery( 'SockLoadFuncs' ) then
   do
   rc = RxFuncAdd( 'SockLoadFuncs', 'RxSock', 'SockLoadFuncs' )
   rc = SockLoadFuncs()
   end

/**************************************************************************/
/* Получение адреса                                                       */
/**************************************************************************/
if xFile \= '' then rc=Stream( xFile, 'C', 'OPEN READ' )

do forever
   if xAddr \= '' then
      do
      if xMask \= '' then leave
      xMask = xAddr
      end
   else
      do
      xLine = LineIn( xFile )
      if Stream( xFile, 'S' ) = 'NOTREADY' then leave
      xLine = strip( xLine )
      if length( xLine ) = 0 | substr( xLine, 1, 1 ) = ':' then iterate
      parse var xLine xMask' 'xA
      end

/**************************************************************************/
/* Вычисление границ адресного пространства и шага сканирования           */
/**************************************************************************/
   rc = xAddressRange( xMask )
   if (rc = 0) then
      do

      if xMonitor = 1 then rc = LineOut('STDERR:', xMask )

      rc = SockGetHostByName( xMask, 'xHost.!' )
      if (rc = 0) then
         do
         if xNoErr = 0 then say ': UNRESOLV   '||xMask
         if xNoErr = 0 & xList = 0 then say substr( ':-', 1, 78, '-' )
         end
      else
         do
         if xList=1 then say xHost.!addr
         else
            do
            say substr( xHost.!addr, 1, 22, ' ' )||xHost.!name
            xTo = xHost.!alias.0
            if xTo < xHost.!addr.0 then xTo = xHost.!addr.0
            do i=1 to xTo
               if i>xHost.!alias.0 then xA=''
               else xA = '*'||xHost.!alias.i
               if i>xHost.!addr.0 then xB=''
               else xB = xHost.!addr.i
               say substr( ': '||xB, 1, 22, ' ' )||xA
            end
            say substr( ':-', 1, 78, '-' )
            end
         end
      iterate
      end

/**************************************************************************/
/* Проход по всем адресам                                                 */
/**************************************************************************/
   do iHost=x2d(xHostA,9) to x2d(xHostB,9) by xStep

      if xMonitor = 1 then rc = LineOut('STDERR:', xHexToDot(d2x(iHost,8)) )

      rc = SockGetHostByAddr( xHexToDot(d2x(iHost,8)), 'xHost.!' )
      if (rc = 0) then
         do
         if xNoErr = 0 then say ': UNRESOLV   '||xHexToDot(d2x(iHost,8))
         if xNoErr = 0 & xList = 0 then say substr( ':-', 1, 78, '-' )
         end
      else
         do
         if xList=1 then say xHost.!name
         else
            do
            say substr( xHost.!name, 1, 63, ' ' )||xHost.!addr
            xTo = xHost.!alias.0
            if xTo < xHost.!addr.0 then xTo = xHost.!addr.0
            do i=1 to xTo
               if i>xHost.!alias.0 then xA=''
               else xA = xHost.!alias.i
               if i>xHost.!addr.0 then xB=''
               else xB = '*'||xHost.!addr.i
               say substr( ': '||xA, 1, 62, ' ' )||xB
            end
            say substr( ':-', 1, 78, '-' )
            end
         end
   end


end

if xFile \= '' then rc=Stream( xFile, 'C', 'CLOSE' )
exit




/**************************************************************************/
/* xIsTemplate()                                                          */
/*========================================================================*/
/* Описание: проверка задания шаблона в dot-нотации                       */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xIsTemplate( xAddr )                                           */
/*                                                                        */
/* Параметры: xAddr - адрес                                               */
/*                                                                        */
/* Возврат: RC=0 - адрес не в виде шаблона в dot-нотации                  */
/*            =1 - адрес в виде шаблона в dot-нотации                     */
/*                                                                        */
/*------------------------------------------------------------------------*/
xIsTemplate: procedure
   xAddr = arg( 1 )
   parse var xAddr x.3'.'x.2'.'x.1'.'x.0
   do i = 0 to 3
      l = length( x.i )
      if l=0 | l>3 then return 0
      if l=1 & x.i='*' then iterate
      if verify( x.i, '0123456789' ) \= 0 then return 0
      if x.i > 255 then return 0
   end
   return 1
/*------------------------------------------------------------------------*/
/* конец функции xIsTemplate()                                            */
/**************************************************************************/



/**************************************************************************/
/* xIsScope()                                                             */
/*========================================================================*/
/* Описание: проверка задания границ адресов в dot-нотации с приращением  */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xIsScope( xAddr )                                              */
/*                                                                        */
/* Параметры: xAddr - адрес                                               */
/*                                                                        */
/* Возврат: RC=0 - адрес не в виде границ в dot-нотации с приращением     */
/*            =1 - адрес в виде границ в dot-нотации с приращением        */
/*                                                                        */
/*------------------------------------------------------------------------*/
xIsScope: procedure
   xAddr = arg( 1 )
   parse var xAddr xA'+'xB
   rc = xIsDot( xA )
   if rc=1 then
      do
      l = length( xB )
      if l=0 then return 0
      if verify( xB, '0123456789' ) \= 0 then return 0
      end
   return rc
/*------------------------------------------------------------------------*/
/* конец функции xIsScope()                                               */
/**************************************************************************/



/**************************************************************************/
/* xIsRange()                                                             */
/*========================================================================*/
/* Описание: проверка задания границ адресов в dot-нотации                */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xIsRange( xAddr )                                              */
/*                                                                        */
/* Параметры: xAddr - адрес                                               */
/*                                                                        */
/* Возврат: RC=0 - адрес не в виде границ в dot-нотации                   */
/*            =1 - адрес в виде границ в dot-нотации                      */
/*                                                                        */
/*------------------------------------------------------------------------*/
xIsRange: procedure
   xAddr = arg( 1 )
   parse var xAddr xA'-'xB
   rc = xIsDot( xA )
   if rc=1 then rc = xIsDot( xB )
   return rc
/*------------------------------------------------------------------------*/
/* конец функции xIsRange()                                               */
/**************************************************************************/



/**************************************************************************/
/* xIsDot()                                                               */
/*========================================================================*/
/* Описание: проверка задания адреса в dot-нотации                        */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xIsDot( xAddr )                                                */
/*                                                                        */
/* Параметры: xAddr - адрес                                               */
/*                                                                        */
/* Возврат: RC=0 - адрес не в dot-нотации                                 */
/*            =1 - адрес в dot-нотации                                    */
/*                                                                        */
/*------------------------------------------------------------------------*/
xIsDot: procedure
   xAddr = arg( 1 )
   parse var xAddr x.3'.'x.2'.'x.1'.'x.0
   do i = 0 to 3
      l = length( x.i )
      if l=0 | l>3 then return 0
      if verify( x.i, '0123456789' ) \= 0 then return 0
      if x.i > 255 then return 0
   end
   return 1
/*------------------------------------------------------------------------*/
/* конец функции xIsDot()                                                 */
/**************************************************************************/



/**************************************************************************/
/* xDotToHex()                                                            */
/*========================================================================*/
/* Описание: преобразование адреса из dot-нотации в hex-форму             */
/*                                                                        */
/* Синтаксис:                                                             */
/*    hexAddr = xDotToHex( dotAddr )                                      */
/*                                                                        */
/* Параметры: dotAddr - адрес в десятичной нотации (dot-нотация)          */
/*                                                                        */
/* Возврат: hexAddr - адрес в шестнадцатиричной форме                     */
/*                                                                        */
/*------------------------------------------------------------------------*/
xDotToHex: procedure
   xDot = arg( 1 )
   parse var xDot x.3'.'x.2'.'x.1'.'x.0
   return d2x( x.3, 2 )||d2x( x.2, 2 )||d2x( x.1, 2 )||d2x( x.0, 2 )
/*------------------------------------------------------------------------*/
/* конец функции xDotToHex()                                              */
/**************************************************************************/



/**************************************************************************/
/* xHexToDot()                                                            */
/*========================================================================*/
/* Описание: преобразование адреса из hex-формы в dot-нотацию             */
/*                                                                        */
/* Синтаксис:                                                             */
/*    dotAddr = xHexToDot( hexAddr )                                      */
/*                                                                        */
/* Параметры: hexAddr - адрес в шестнадцатиричной форме                   */
/*                                                                        */
/* Возврат: dotAddr - адрес в десятичной нотации (dot-нотация)            */
/*                                                                        */
/*------------------------------------------------------------------------*/
xHexToDot: procedure
   xHex = arg( 1 )
   do i = 0 to 3
      x.i = substr( xHex, 7-i*2, 2 )
   end
   return x2d( x.3, 4 )||'.'||x2d( x.2, 4 )||'.'||x2d( x.1, 4 )||'.'||x2d( x.0, 4 )
/*------------------------------------------------------------------------*/
/* конец функции xHexToDot()                                              */
/**************************************************************************/



/**************************************************************************/
/* xAddressRange()                                                        */
/*========================================================================*/
/* Описание: вычисление границ адресного пространства                     */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xAddressRange( xMask )                                         */
/*                                                                        */
/* Параметры: xMask - шаблон                                              */
/*                    global.host.name                                    */
/*                    xxx.xxx.xxx.xxx   где xxx=ddd или *                 */
/*                    ddd.ddd.ddd.ddd-ddd.ddd.ddd.ddd                     */
/*                                                                        */
/* Возврат: xHostA - нижняя граница                                       */
/*          xHostB - верхняя граница                                      */
/*          xStep  - шаг                                                  */
/*          RC=0   - Inet-адрес                                           */
/*            =1   - IP-адрес                                             */
/*                                                                        */
/*------------------------------------------------------------------------*/
xAddressRange: procedure expose xHostA xHostB xStep
   xMask = arg( 1 )
   xDot = 1
   if xIsDot( xMask ) = 1 then
      do
      xHostA = xDotToHex( xMask )
      xHostB = xHostA
      xStep = 1
      return 1
      end
   if xIsRange( xMask ) = 1 then
      do
      parse var xMask xA'-'xB
      xHostA = xDotToHex( xA )
      xHostB = xDotToHex( xB )
      xStep = 1
      if xHostA>xHostB then xStep = -1
      return 1
      end
   if xIsScope( xMask ) = 1 then
      do
      parse var xMask xA'+'xB
      xHostA = xDotToHex( xA )
      xHostB = d2x( x2d(xHostA,9) + xB, 8 )
      xStep = 1
      if xHostA>xHostB then xStep = -1
      return 1
      end
   if xIsTemplate( xMask ) = 1 then
      do
      parse var xMask x.3'.'x.2'.'x.1'.'x.0
      y.0 = 1
      y.1 = 256
      y.2 = 65536
      y.3 = 16777216
      do i = 0 to 3
         l = length( x.i )
         if l=1 & x.i='*' then
            do
               xStep = y.i
               leave
            end
      end
      xHostA = xDotToHex( translate(xMask, '0', '*') )
      xTemp = xMask
      do while pos( '*', xTemp ) \= 0
         parse var xTemp xA'*'xB
         xTemp = xA||'255'||xB
      end
      xHostB = xDotToHex( xTemp )
      return 1
      end

   xStep = 1
   return 0
/*------------------------------------------------------------------------*/
/* конец функции xAddressRange()                                          */
/**************************************************************************/



/**************************************************************************/
/* xError()                                                               */
/*========================================================================*/
/* Описание: вывод сообщения об ошибке и выход                            */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xError( xParm )                                                */
/*                                                                        */
/* Параметры: xParm - строка параметров                                   */
/*                                                                        */
/* Возврат: нет                                                           */
/*                                                                        */
/*------------------------------------------------------------------------*/
xError: procedure
   xParm = arg( 1 )

   say ''
   say 'Invalid parameters - '||xParm
   say 'Type RxNsLookUp without parameters.'
   say ''

   exit 1
/*------------------------------------------------------------------------*/
/* конец функции xError()                                                 */
/**************************************************************************/



/**************************************************************************/
/* xParseParm()                                                           */
/*========================================================================*/
/* Описание: разбор параметров командной строки                           */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xParseParm( xParm )                                            */
/*                                                                        */
/* Параметры: xParm - строка параметров                                   */
/*                                                                        */
/* Возврат: xAddr - адрес или '', если задан список                       */
/*          xFile - имя файла-списка, 'STDIN:' или '', если списка нет    */
/*          xMonitor=0 - нет отображения адресов на STDERR                */
/*                  =1   отображение обрабатываемого адреса на STDERR     */
/*          xList=0 - результат в виде протокола                          */
/*               =1   результат в виде списка                             */
/*          xNoErr=0 - выводить сообщения об ошибках                      */
/*                =1 - не выводить сообщения об ошибках                   */
/*                                                                        */
/*------------------------------------------------------------------------*/
xParseParm: procedure expose xAddr xFile xList xNoErr xMonitor
   xParm = arg( 1 )

   xAddr = ''
   xFile = ''
   xList = 0
   xMonitor = 0
   xNoErr = 0
   parse var xParm x.0' 'x.1' 'x.2' 'x.3' 'x.4
   do i=0 to 4
      x.i = strip( x.i )
   end
   if x.4 \= '' then xError( xParm )

   do i=0 to 3
      if x.i  = '' then leave
      if translate( x.i ) = '/M' then
         do
         if xMonitor = 1 then xError( xParm )
         xMonitor = 1
         iterate
         end
      if translate( x.i ) = '/L' then
         do
         if xList = 1 then xError( xParm )
         xList = 1
         iterate
         end
      if translate( x.i ) = '/I' then
         do
         if xNoErr = 1 then xError( xParm )
         xNoErr = 1
         iterate
         end
      if substr( x.i, 1, 1 ) = '@' then
         do
         if xFile \= '' | xAddr \= '' then xError( xParm )
         xFile = substr( x.i, 2 )
         if xFile = '' then xFile = 'STDIN:'
         iterate
         end
      if xFile \= '' | xAddr \= '' then xError( xParm )
      xAddr =  x.i
   end
   if xFile = '' & xAddr = '' then xError( xParm )

   return ''
/*------------------------------------------------------------------------*/
/* конец функции xParseParm()                                             */
/**************************************************************************/



/**************************************************************************/
/* xHowUse()                                                              */
/*========================================================================*/
/* Описание: вывод описания синтаксиса командной строки и выход           */
/*                                                                        */
/* Синтаксис:                                                             */
/*    rc = xHowUse()                                                      */
/*                                                                        */
/* Параметры: нет                                                         */
/*                                                                        */
/* Возврат: нет                                                           */
/*                                                                        */
/*------------------------------------------------------------------------*/
xHowUse: procedure
   say 'RxNsLookUp v0.01   (c)VicTor Smirnoff, 2001   E-mail: sva@water.karelia.ru'
   say ''
   say 'Syntax: RxNsLookUp [

| @[] ] [/l] [/i] [/m]'
   say ''
   say 'Parameters: address - the Inet-address (i.e. www.somebody.net),'
   say '                      the IP-address (i.e. 192.168.0.127),'
   say '                      the template of the IP-addresses (i.e. 192.168.0.*),'
   say '                      the range of the IP-addresses (i.e. 192.168.0.0+127'
   say '                         or 192.168.0.0-192.168.0.127)'
   say '          @[
] - get the addresses from the file or from stdin,'
   say '                      when the file name is omitted.'
   say '               list - the file name. This file should contain the list '
   say '                      of addresses (Inet, IP, template or range), example:'
   say '                         :comment'
   say '                         www.somebody.net'
   say '                         192.168.0.127'
   say '                         192.168.0.*'
   say '                         192.168.0.0-192.168.0.127'
   say '                         192.168.0.0+127'
   say '                 /l - display the results as the list (else as the protocol).'
   say '                 /i - ignore the error messages.'
   say '                 /m - display the processed addresses to STDERR.'
   say ' without parameters - display the help.'
   exit 1;
/*------------------------------------------------------------------------*/
/* конец функции xHowUse()                                                */
/**************************************************************************/



/**************************************************************************/
/* конец файла RxNsLookUp.cmd                                             */
/**************************************************************************/