Page 1 of 1 1
Topic Options
#108919 - 2003-11-25 09:07 PM IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
I am trying to assign a certain field a name based on the third octate (SP) of the IP address. So far the only thing I have is the INSTR command. This is not going to work however since I have third octates that are the same as the second one (10.250.*.* and 10.250.250. etc). Here is my code so far:

Select
Case InStr(@IPADDRESS0,"152") OR 153 OR 154 OR 155 OR 254 OR 206 OR 190 OR 196 OR 216 OR 43
$CENTER = "BRK"

Case InStr(@IPADDRESS0,"196") OR 198 OR 205 OR 206 OR 232 OR 233 OR 234 OR 243
$CENTER = "MLK"

Case InStr(@IPADDRESS0,"223") OR 225 OR 226 OR 227 OR 248
$CENTER = "OPE"

Case InStr(@IPADDRESS0,"229")
$CENTER = "STC"

Case InStr(@IPADDRESS0,"252")
$CENTER = "PEO"

EndSelect

I am wondering is there anyway I can tell it to only read the third octate (the 5 - 7 characters)?

Top
#108920 - 2003-11-25 09:11 PM Re: IP Addresses.....
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
check out substr() from manual
_________________________
!

download KiXnet

Top
#108921 - 2003-11-25 09:14 PM Re: IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
Will that allow me to only read those characters...?

Would that be -4 or -5? I am really confused on this. I messed with substr and got all messed up!

Top
#108922 - 2003-11-25 09:17 PM Re: IP Addresses.....
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Or...

$third = Trim(Split(@ipaddress0,".")[2])

Top
#108923 - 2003-11-25 09:22 PM Re: IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
What is that going to do for me? I have read through the manual and still don't get it.
Top
#108924 - 2003-11-25 09:25 PM Re: IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
I don't think the substr will work becuase I need only the third octate to return. If the 4th comes back it could also have duplicate numbers in it. I just need to parse out the third octate and read it to get the variable for my database. Any ideas?
Top
#108925 - 2003-11-25 09:32 PM Re: IP Addresses.....
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
use split as shown by Chris
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#108926 - 2003-11-25 09:38 PM Re: IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
I am kind of slow....can someone break that down and show me what each thing is doing?
Top
#108927 - 2003-11-25 09:41 PM Re: IP Addresses.....
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
http://www.scriptlogic.com/kixtart/htmlhelp/default.asp

look up Split() and Trim()
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#108928 - 2003-11-25 09:42 PM Re: IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
I have looked up all of the things mentioned above. I am not catching on to as what I need to do. I just want to say if the third octate is etc1 etc2 etc3 then output BRK?
Top
#108929 - 2003-11-25 09:48 PM Re: IP Addresses.....
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$third = Trim(Split(@ipaddress0,".")[2])

will return the third octet

if you are wanting to see if the $third is in a list of values.

$third = Trim(Split(@ipaddress0,".")[2])
if instr('236 237 239 241 242',$third)
? "third octect is in the above list"
endif
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#108930 - 2003-11-25 09:51 PM Re: IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
What does this do for me?

$third = Trim(Split(@ipaddress0,".")[2])

Top
#108931 - 2003-11-25 10:03 PM Re: IP Addresses.....
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Split will split the ip at the dots and trim will remove leading or tailing spaces.
So if your ip is 192.168.1.1 you will get 192 and 168 and 1 and 1seperate.
The first two octets are fully filled but the third is only one character and will be seen by kix as a 1 with two spaces in front of it.

This:
Code:

$third = Trim(Split(@ipaddress0,".")[2])



Will give you the third octet because it goes from 0 to 3 so [2] would be the third piece.
Just put ?$third and maybe sleep 5 at the bottom of the script to see the value of $third and let kix wait for 5 sec so you can actually see the output.


Edited by R2D2 (2003-11-25 10:04 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#108932 - 2003-11-25 10:11 PM Re: IP Addresses.....
Josh R Offline
Fresh Scripter
****

Registered: 2003-05-07
Posts: 49
$third = Trim(Split(@ipaddress0,".")[2])
if instr('236 237 239 241 242',$third)
? "third octect is in the above list"
endif


Can I change this to a CASE statement and add a variable for use later in the script? Such as...

Case $third = Trim(Split(@IPADDRESS0,".")[2])
InStr('153 154 155 254 206 190 196 216 ',$third)
$CENTER = "BRK"

Top
#108933 - 2003-11-25 10:16 PM Re: IP Addresses.....
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Then it would be like this.

Code:

$third = Trim(Split(@IPADDRESS0,".")[2])

Select
Case InStr('153 154 155 254 206 190 196 216 ',$third)
$CENTER = "BRK"
EndSelect

_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#108934 - 2003-11-25 10:54 PM Re: IP Addresses.....
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Yuck! Instr() will give you false positives on partials. F.E. you will find '1' in '1','10','101', etc.

I think you are taking a bad approach. Look at some of the subnet UDFs or read up on it in the FAQ.

Alternately, put all the octets into an array and use ASCan() which does not return partials.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#108935 - 2003-11-25 11:01 PM Re: IP Addresses.....
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
I like Les' suggestions. So that would be...

Code:

$third = Trim(Split(@IPADDRESS0,".")[2])
Select
Case Ascan(Split('153 154 155 254 206 190 196 216'),$third) > -1
$CENTER = "BRK"
EndSelect


Top
#108936 - 2003-11-25 11:03 PM Re: IP Addresses.....
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Quote:


Yuck! Instr() will give you false positives on partials. F.E. you will find '1' in '1','10','101', etc.

I think you are taking a bad approach. Look at some of the subnet UDFs or read up on it in the FAQ.

Alternately, put all the octets into an array and use ASCan() which does not return partials.





True, better options are available indeed. But if not searching for octets containing one or two digits the above given examples could be a quick and dirty option for doing some emergency stuff.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#108937 - 2003-11-25 11:12 PM Re: IP Addresses.....
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Quick I don't mind... dirty I hate.

I often find that when people post their problem they over sanitize their examples and therefore cannot build code on a silver platter that will work in all instances. THat, and others may adopt this code later, only to be bitten by the shortcomings.

Short of an array, the string could include delimiters that prevent false positives.

untested
Code:
$third = '~'+Trim(Split(@IPADDRESS0,".")[2])+'~'
Select
Case InStr('~153~154~155~254~206~190~196~216~',$third)
$CENTER = "BRK"
EndSelect

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#108938 - 2003-12-05 07:33 PM Re: IP Addresses.....
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
See also TCP/IP Primer, Part I - IP Addresses and use .e.g the IsInSubnet() UDF to differentiate between different subnets.
_________________________
There are two types of vessels, submarines and targets.

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
0 registered and 1044 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.121 seconds in which 0.075 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