Scripting Example 62

; Example62.ftp

; =============

; This example is only a showcase, demonstrating

; the use of subroutines and string manipulation

; functions.

; ----------------------------------------------

; The script asks the user for an IP address.

; It then evaluates if the IP address is

; syntactically correct, contains the correct

; dots and digits and at last if checks if the

; machine with the IP is available or not.

; ----------------------------------------------

 

 

; -- Constants

numvariable FalseValue

Let FalseValue=0

numvariable TrueValue

Let TrueValue=1

 

; -- Global variables

numvariable iResult

charvariable sIp

charvariable sMessage

 

; -- Work variables

numvariable i

numvariable iTemp

numvariable iPos

numvariable iBeg

numvariable iEnd

numvariable iCount

 

; -- Saved dot-locations

numvariable DotLocation0

numvariable DotLocation1

numvariable DotLocation2

numvariable DotLocation3

numvariable DotLocation4

 

; -- Starting point...

:TryAgain

; -- Input the IP address...

Gosub GetIp

if "%%sIp%%x"="x" then

EndScript

endif

 

; Check dot positions an validate characters...

Gosub FirstCheck

 

if %%iResult%%=%%TrueValue%% then

; Check values of IP Segments...

Gosub SecondCheck

endif

 

if %%iResult%%=%%FalseValue%% then

message "%%sMessage%%"

Goto TryAgain

endif

 

; -- Now the IP is correct, check if it is available:

Host="%%sIp%%"

if not IPAvailable then

Message "This IP is not reachable."

Goto TryAgain

endif

 

; ***********************************************************

; -- Here we should insert the code to use the IP address....

; ***********************************************************

 

; --

; -- Script ends...

EndScript

; --

 

; -------------------------------------

 

; ----------- SubRoutines -------------

 

; -------------------------------------

 

; Input IP address from user...

:GetIp

Input sIp "Please provide an IP Address:"

Return

 

 

; Check dot positions an validate characters...

:FirstCheck

Let iTemp=Length("%%sIp%%")

Let iCount=0

for i=1 to %%iTemp%%

Let a=SubStr( "%%sIp%%", %%i%%, 1 )

if "%%a%%x"=".x" then

Let iCount= %%iCount%%+1

Let DotLocation%%iCount%%=i

else

Let iPos=position( "%%a%%", "0123456789" )

if %%iPos%% = 0 then

Let iResult=%%FalseValue%%

Let iPos=%%iCount%%+1

Let sMessage="Wrong character: '%%a%%' in segment %%iPos%%"

Return

endif

endif

next i

if %%iCount%%=3 then

Let iResult=%%TrueValue%%

else

Let iResult=%%FalseValue%%

Let sMessage="Insufficient number of segments."

endif

 

Return

 

; Check values of IP Segments...

:SecondCheck

Let DotLocation4=length( "%%sIp%%" )+1

Let DotLocation0=0

for i=1 to 4

Let iBeg=i-1

Let iBeg=%%DotLocation[%%iBeg%%]%%+1

Let iEnd=%%DotLocation[%%i%%]%%-%%iBeg%%

 

Let iTemp=SubStr( "%%sIp%%", %%iBeg%%, %%iEnd%% )

if %%iTemp%%>255 then

Let iResult=%%FalseValue%%

Let sMessage="Segment %%i%% has a wrong value of %%iTemp%%"

Return

endif

if %%iTemp%%<0 then

Let iResult=%%FalseValue%%

Let sMessage="Segment %%i%% has a wrong value of %%iTemp%%"

Return

endif

next i

Return