Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extract specific octect from ip address

189 views
Skip to first unread message

-=- jd -=-

unread,
Feb 10, 2006, 11:51:16 PM2/10/06
to
I discovered a really great script that Phil Robyn wrote on how to grab
an IP and set it as an env var. This is a start for something that I'm
working on...

We have multiple locations at work, all using a class 'C' address space
(192.168.xxx.xxx - don't ask why they didn't use a 172.16.x.x space -
who knows? ) Anyway - corp office downtown might be 192.168.96.1-255,
the plant across town might be 192.168.111.1-255, another location in
another city might be 192.168.97.1-255, etc....

What I'm trying to come up with is a reliable means of extracting the
3rd octect, which could be either 2 or 3 digits depending the site.

A .cmd file that I'm playing with right now leaves me with an env var of
%IP1% which is perfect as it is the IP of <X> machine. I want to know
where specifically that machine is from, and one way I can determine
that is by it IP address - specifically in the 3rd octect. ( I want to
do some logging via a logon script )

Anything that this will be run against will be XP or 2K3.

Any ideas??

Thanks folks...

-=- jd -=-

Timo Salmi

unread,
Feb 11, 2006, 12:42:05 AM2/11/06
to
-=- jd -=- wrote:
> I discovered a really great script that Phil Robyn wrote on how to grab
> an IP and set it as an env var. This is a start for something that I'm

> the plant across town might be 192.168.111.1-255, another location in

> another city might be 192.168.97.1-255, etc....

> What I'm trying to come up with is a reliable means of extracting the
> 3rd octect, which could be either 2 or 3 digits depending the site.

@echo off & setlocal enableextensions
:: For demonstration testing a default is included
if "%~1"=="" (
set myip=192.168.97.255
) else (
set myip=%~1)
::
:: Get your third octet
for /f "tokens=3 delims=." %%i in ('echo %myip%') do set myip3=%%i
echo %myip%
echo %myip3%
endlocal & goto :EOF

The output
C:\_D\TEST>cmdfaq
192.168.97.255
97

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

-=- jd -=-

unread,
Feb 11, 2006, 9:13:09 AM2/11/06
to
Outstanding!! I can always count on you guys to come up with something
to help me out when I get stuck. I've said it before and I'll say it
again - there are a select few individuals that hang out in here who are
scary smart and slick when it comes to writing up these great little
scripts... I can come up with ideas of stuff that I would like to do,
and most of the time I can clobber my way through it and make it work -
might not be as pretty as you guys would do it ( my solution might
involve temp files and be 15 lines long, where you guys could do it in
one line and use no temp file... <grin> ) but I have learned over the
years to keep plugging away at it, and after I get tired of beating my
head - I'll just ask away and 9 times out of 10 one of you steps up with
something like what Timo provided. ;)

Thanks again! ;)

-=- jd -=-

muza9

unread,
Feb 15, 2006, 9:28:22 AM2/15/06
to
.......
skip
......

> >
> > @echo off & setlocal enableextensions
> > :: For demonstration testing a default is included
> > if "%~1"=="" (
> > set myip=192.168.97.255
> > ) else (
> > set myip=%~1)
> > ::
> > :: Get your third octet
> > for /f "tokens=3 delims=." %%i in ('echo %myip%') do set myip3=%%i
> > echo %myip%
> > echo %myip3%
> > endlocal & goto :EOF
> >
> > The output
> > C:\_D\TEST>cmdfaq
> > 192.168.97.255
> > 97
> >

Hello, colleagues!

Let to continue the thread?

I have variable VAR, containing a line of a kind:
" word1 word2 word3 ". (I.e. three strings of the symbols divided by
blanks)

My task is to allocate sub-string " word2 word3 ", i.e. it is necessary
for me to receive the second and third strings of symbols from VAR.

Example:
[begin of my bat-file]
set var =qwerty asdfgh zxcvbn
...
[Here your offers are realized !]
...
echo %var%
[end of my bat-file]

The output must be:
asdfgh zxcvbn

Thanks.

mu...@tut.by

0 new messages