Switch to desktop version  
Create your own IP scanner - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: BASIC Script (https://techforum.ewon.biz/forum-52.html)
+--- Thread: Create your own IP scanner (/thread-419.html)



Create your own IP scanner - simon - 17-10-2017

Hi Guys,

This is an example of IP scanner in BASIC.
The idea is to know which device is connected to the LAN of your Flexy, especially when the Flexy is acting as a DHCP server on its LAN (See https://ewon.biz/sites/default/files/kb-0267-0-en_dhcp_server_on_lan.pdf).

Here the SCAN is performed every minute.

You'll find also attached a webpage that displays the status of the scan (which devices are connected or disconnected).  This page must be copied in the usr/ directory of your eWON.
  scan.zip (Size: 190 bytes / Downloads: 30)

PS : The script can be pasted as it is in the init section.

Code:
//Read DHCP Server parameters
ONDATE 1,"* * * * *","GOTO ScanIP"

ScanIpInit:
SETSYS COM, "LOAD"
DHCPSrvSTARTIP$ = GETSYS COM, "LANDHCPSStartIP"
DHCPSrvENDIP$ = GETSYS COM, "LANDHCPSEndIP"

//Compute the IP address range to test (I consider a mask of 255.255.255.0)
NetWorkAddr$ = @SplitString$(DHCPSrvSTARTIP$,1,".") + "." + @SplitString$(DHCPSrvSTARTIP$,2,".") + "." + @SplitString$(DHCPSrvSTARTIP$,3,".") + "."
FirstDigit% = VAL @SplitString$(DHCPSrvSTARTIP$,4,".")
LastDigit% = VAL @SplitString$(DHCPSrvENDIP$,4,".")
CurrentDigit% = FirstDigit%
ScanIsRunning% = 0
SCANSTATUS$ = "Scan is stopped"

ScanIP:
PRINT "IP SCAN START"
IF ScanIsRunning% = 1 THEN
  PRINT "Scan is already running"
  GOTO DontScan
ENDIF
ScanIsRunning% = 1
SCANSTATUS$ = "Scan is running"
CurrentDigit% = FirstDigit%
RESULT$ = ""

ScanNextIp:
  CLOSE 1
  ONSTATUS "goto ProcessStatus"
  Ip$ = NetWorkAddr$ +STR$ CurrentDigit%
  //We test the TCP port 80
  OPEN "tcp:"+Ip$+ ":80,10" FOR BINARY OUTPUT AS 1
  a% = GETSYS PRG,"ACTIONID"
DontScan:
END

ProcessStatus:
  b% = GETSYS PRG,"EVTINFO"
  IF b%=a% THEN
    SETSYS PRG,"ACTIONID",a%
    c% = GETSYS PRG,"ACTIONSTAT"
    IF c% = 0 THEN
      RESULT$ = RESULT$ + Ip$ + " is connected</br>"
      PRINT "PING "+Ip$+" success"
    ELSE
      RESULT$ = RESULT$ + Ip$ + " is disconnected</br>"
      PRINT "PING "+Ip$+" failed"
    ENDIF
    CurrentDigit% = CurrentDigit% + 1
    IF CurrentDigit% <= LastDigit% THEN
      GOTO ScanNextIp
    ELSE
      ScanIsRunning% = 0
      SCANSTATUS$ = "Scan is stopped"
      PRINT "IP SCAN END"
  ENDIF
END

Function SplitString$ ($StringToParse$,$Pos%, $char$)

$e% = 1
$loopnbr% = 0
$NextLine:
 $f% = INSTR $e% , $StringToParse$ , $char$
 //LAST ELEMENT
 IF $f% = 0 THEN  
    $B$ = $StringToParse$( $e% TO LEN $StringToParse$)
    GOTO $EndOfLine        
 ENDIF
 $loopnbr% = $loopnbr% + 1
 $B$ = $StringToParse$( $e% TO $f%-1)
 IF $Pos% = $loopnbr% THEN GOTO $EndOfLine
 $e% = $f% + 1 //REM Init for next loop/line            
 GOTO $NextLine
$EndOfLine:
$SplitString$ = $B$
EndFn

END