MarcD
(Fresh Scripter)
2005-10-11 04:25 AM
If instr(@ipaddress) ???

Is there a way to perform an if statement according on a certain string in an IP address. (Like Below)
I am fairly new to coding so...;

IF instr(@ipaddress0) = "172.18.48."
AT (21,1) "Connected via VPN, Not mapping office Printers"
sleep 2
exit
else
....
This doesnt work, so surely there is a better way to do this...


AllenAdministrator
(KiX Supporter)
2005-10-11 05:55 AM
Re: If instr(@ipaddress) ???

Instr() returns either 0 if it doesn't find the string you are looking for or it returns the location within the string... so you might change your code to:

if instr(@ipaddress0,"172.18.48.")
...
endif


Les
(KiX Master)
2005-10-11 05:58 AM
Re: If instr(@ipaddress) ???

Don't forget the spaces.

if instr(@ipaddress0,"172. 18. 48.")...


NTDOCAdministrator
(KiX Master)
2005-10-11 06:26 AM
Re: If instr(@ipaddress) ???

As Les mentioned, don't forget the spaces returned from @IPADDRESS0

Code:
Break On
If InStr(Join(Split(@ipaddress0,"172.18.48"),' '),'')
'IP is valid' ?
Exit 1
Else
'IP is not in range' ?
EndIf



 


MarcD
(Fresh Scripter)
2005-10-11 07:07 AM
Re: If instr(@ipaddress) ???

Quote:

Instr() returns either 0 if it doesn't find the string you are looking for or it returns the location within the string...





So if the address is "172.18.100.100", it would return a 0

What would it return if the address was "172.18.48.12"


Les
(KiX Master)
2005-10-11 07:11 AM
Re: If instr(@ipaddress) ???

1

MarcD
(Fresh Scripter)
2005-10-11 07:19 AM
Re: If instr(@ipaddress) ???

Whilst playing around, I have come up with;

Code:
 
$address = instr(@ipaddress0, "172.18.48.")
if $address = 1
AT (8,10) "You are Logged on Locally. Starting Script"
sleep 2
goto start
endif

if $address = 0
AT (8,10) "You are Logged on via the VPN. Not mapping office printers"
sleep 2
goto drives
endif



Would this work??


MarcD
(Fresh Scripter)
2005-10-11 07:24 AM
Re: If instr(@ipaddress) ???

Oh the spaces... Looks like I got it working!

NTDOCAdministrator
(KiX Master)
2005-10-11 07:42 AM
Re: If instr(@ipaddress) ???

You shouldn't need 2 IF statements. The IF ELSE ENDIF shown above should be able to handle it, you don't even need an ELSE if you're not going to do anything. Just use an IF ENDIF statement.

The SLEEP statements also don't look like their really needed.

The usage of GOTO is frowned upon.

Dimming your variables is also recommended.
 


MarcD
(Fresh Scripter)
2005-10-11 07:58 AM
Re: If instr(@ipaddress) ???

Quote:


The SLEEP statements also don't look like their really needed.




Just for testing
Quote:


The usage of GOTO is frowned upon.





Will see if I can change it... Thanks. Why is this frowned upon?

Quote:


Dimming your variables is also recommended.





Have you got an example?


NTDOCAdministrator
(KiX Master)
2005-10-11 09:04 AM
Re: If instr(@ipaddress) ???

following or tracing flow of GOTO statements is quite difficult. Scope issues are difficult to control.

Example of dimming a variable.

You have:
$address

Dimming it would be like this:

Dim $address
$address = "something"

 


Sealeopard
(KiX Master)
2005-10-25 02:10 PM
Re: If instr(@ipaddress) ???

See also the FAQ Forum for TCP/IP articles and a whole list of TCP/IP-related UDFs.

sixdoubleo
(Starting to like KiXtart)
2005-10-25 09:39 PM
Re: If instr(@ipaddress) ???

I'm SURE there is a UDF to clean up the IP address...or even a way to strip spaces from a string elegantly, but I usually do something like this just so I'm comparing apples to apples...

$IP = @IPADDRESS0
$IP = Trim(Substr($IP,1,3))+"."+Trim(Substr($IP,5,3))+"."+Trim(Substr($IP,9,3))+"."+Trim(Substr($IP,13,3))


NTDOCAdministrator
(KiX Master)
2005-10-26 01:30 AM
Re: If instr(@ipaddress) ???

Code:
Break On
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')

Dim $IP
$IP=Join(Split(@IPADDRESS0,' '),'')
'My IP is: ' + $IP ?
'My IP without blank removal: ' + @IPADDRESS0 ?