Page 1 of 1 1
Topic Options
#149612 - 2005-10-11 04:25 AM If instr(@ipaddress) ???
MarcD Offline
Fresh Scripter

Registered: 2005-10-06
Posts: 7
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...

Top
#149613 - 2005-10-11 05:55 AM Re: If instr(@ipaddress) ???
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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


Edited by Allen (2005-10-11 05:58 AM)

Top
#149614 - 2005-10-11 05:58 AM Re: If instr(@ipaddress) ???
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Don't forget the spaces.

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


Edited by Les (2005-10-11 06:03 AM)
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#149615 - 2005-10-11 06:26 AM Re: If instr(@ipaddress) ???
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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



 

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

Registered: 2005-10-06
Posts: 7
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"

Top
#149617 - 2005-10-11 07:11 AM Re: If instr(@ipaddress) ???
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
1
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

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

Registered: 2005-10-06
Posts: 7
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??

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

Registered: 2005-10-06
Posts: 7
Oh the spaces... Looks like I got it working!
Top
#149620 - 2005-10-11 07:42 AM Re: If instr(@ipaddress) ???
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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.
 


Edited by NTDOC (2005-10-11 07:44 AM)

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

Registered: 2005-10-06
Posts: 7
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?


Edited by MarcD (2005-10-11 08:03 AM)

Top
#149622 - 2005-10-11 09:04 AM Re: If instr(@ipaddress) ???
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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"

 

Top
#149623 - 2005-10-25 02:10 PM Re: If instr(@ipaddress) ???
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
See also the FAQ Forum for TCP/IP articles and a whole list of TCP/IP-related UDFs.
_________________________
There are two types of vessels, submarines and targets.

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

Registered: 2004-02-06
Posts: 118
Loc: California, US
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))

Top
#149625 - 2005-10-26 01:30 AM Re: If instr(@ipaddress) ???
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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 ?



Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 382 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.074 seconds in which 0.045 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org