A Windows XP help forum. PCbanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » PCbanter forum » Microsoft Windows 7 » Windows 7 Forum
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

.BAT or .CMD Routine To Truncate IP Addr's Last Digits?



 
 
Thread Tools Rate Thread Display Modes
  #1  
Old December 27th 14, 05:22 PM posted to alt.windows7.general
(PeteCresswell)
external usenet poster
 
Posts: 1,933
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

I can get the gateway or PC's IP addr. e.g. 10.0.0.1 or 10.0.0.123 or
192.168.0.33.

I want to truncate the last chars of the IP address.

e.g. 10.0.0.123 = 10.0.0 or 1.0.0.0.

I've been Googling for almost an hour now and no joy.


Seems like I need to either:

- Scan starting from left for the third occurrence of "."
OR
- Scan starting from the right for the first occurrence of "."

....and then trim what's to the right of said "."


Can anybody point me in the right direction?

--
Pete Cresswell
Ads
  #2  
Old December 27th 14, 08:35 PM posted to alt.windows7.general
Stan Brown
external usenet poster
 
Posts: 2,904
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

On Sat, 27 Dec 2014 12:22:38 -0500, (PeteCresswell) wrote:
I can get the gateway or PC's IP addr. e.g. 10.0.0.1 or 10.0.0.123 or
192.168.0.33.

I want to truncate the last chars of the IP address.

e.g. 10.0.0.123 = 10.0.0 or 1.0.0.0.

I've been Googling for almost an hour now and no joy.


I don't know how to do it in the native command line, though I'd bet
there's a way in PowerShell.

It's trivially easy with sed, or Vim, or my shareware GREP, or the
freeware TCC/LE. The problem is that, for all of those, it's
"trivially easy" if you already know how to use that program.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
  #3  
Old December 27th 14, 08:48 PM posted to alt.windows7.general
Joe Morris
external usenet poster
 
Posts: 289
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

"(PeteCresswell)" wrote:

I want to truncate the last chars of the IP address.


e.g. 10.0.0.123 = 10.0.0 or 1.0.0.0.


Can anybody point me in the right direction?


Use the FOR command.

Assuming that you have a command which will emit the IP address, or a file
containing it (see the help text for FOR) here's one way to do it. (The
example uses ECHO to emit the IP address, and again uses ECHO to return the
result.)

for /F "usebackq delims=. tokens=1-3" %f in (`echo 11.22.33.44`) do echo
%f.%g.%h

The above command returns:

11.22.33


Note that the emitting ECHO command is in backquotes, not forward quotes.
Also, note that the above example will work as a demo from the command line,
but you'll need to double the % signs when you use it in a script. In a
script the line would be:

for /F "usebackq delims=. tokens=1-3" %%f in (`echo 11.22.33.44`) do echo
%%f.%%g.%%h


Joe


  #4  
Old December 27th 14, 09:50 PM posted to alt.windows7.general
Gene E. Bloch[_2_]
external usenet poster
 
Posts: 7,485
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

On Sat, 27 Dec 2014 15:35:38 -0500, Stan Brown wrote:

On Sat, 27 Dec 2014 12:22:38 -0500, (PeteCresswell) wrote:
I can get the gateway or PC's IP addr. e.g. 10.0.0.1 or 10.0.0.123 or
192.168.0.33.

I want to truncate the last chars of the IP address.

e.g. 10.0.0.123 = 10.0.0 or 1.0.0.0.

I've been Googling for almost an hour now and no joy.


I don't know how to do it in the native command line, though I'd bet
there's a way in PowerShell.

It's trivially easy with sed, or Vim, or my shareware GREP, or the
freeware TCC/LE. The problem is that, for all of those, it's
"trivially easy" if you already know how to use that program.


Also AWK :-)

Same comment about "trivially easy".

Back in the day I really liked AWK, but by now I'd have to start at the
kindergarten level again...

It's still around:

Gawk for Windows
http://gnuwin32.sourceforge.net/packages/gawk.htm

--
Gene E. Bloch (Stumbling Bloch)
  #5  
Old December 27th 14, 10:09 PM posted to alt.windows7.general
Gene E. Bloch[_2_]
external usenet poster
 
Posts: 7,485
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

On Sat, 27 Dec 2014 13:50:19 -0800, Gene E. Bloch wrote:

On Sat, 27 Dec 2014 15:35:38 -0500, Stan Brown wrote:

On Sat, 27 Dec 2014 12:22:38 -0500, (PeteCresswell) wrote:
I can get the gateway or PC's IP addr. e.g. 10.0.0.1 or 10.0.0.123 or
192.168.0.33.

I want to truncate the last chars of the IP address.

e.g. 10.0.0.123 = 10.0.0 or 1.0.0.0.

I've been Googling for almost an hour now and no joy.


I don't know how to do it in the native command line, though I'd bet
there's a way in PowerShell.

It's trivially easy with sed, or Vim, or my shareware GREP, or the
freeware TCC/LE. The problem is that, for all of those, it's
"trivially easy" if you already know how to use that program.


Also AWK :-)

Same comment about "trivially easy".

Back in the day I really liked AWK, but by now I'd have to start at the
kindergarten level again...

It's still around:

Gawk for Windows
http://gnuwin32.sourceforge.net/packages/gawk.htm


I read this article:

http://www.drdobbs.com/open-source/g...-awk/240158351

AKA http://tinyurl.com/mmfvffg

Maybe I'd be starting out at the pre-school level, not kindergarten :-)

Reminds me of Basic. A long time ago, a coworker handed me a sheet of
paper. It was the definition of Basic at the time. Basic is now bigger
than that:-)

--
Gene E. Bloch (Stumbling Bloch)
  #6  
Old December 27th 14, 10:40 PM posted to alt.windows7.general
(PeteCresswell)
external usenet poster
 
Posts: 1,933
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

Per Joe Morris:

for /F "usebackq delims=. tokens=1-3" %%f in (`echo 11.22.33.44`) do echo
%%f.%%g.%%h


Nice!!!... thanks.... I could not have come up with that if
my life depended on it.

FWIW, here's how it comes out as a callable function:
================================================== ==============
ECHO OFF
SET fullIP=123.456.789.321

CALL :truncateIP %fullIP% myTruncatedIP

ECHO %myTruncatedIP%
PAUSE


:truncateIP theFullIP theTruncatedIP
:* --------------------------------------------------------
:* Take a complete IP address (e.g. 123.456.789.321) and
:* lop off the last set of digits and their dot, returning
:* only the remainder, without the dot (e.g. 123.456.789)

FOR /F "usebackq delims=. tokens=1-3" %%f in (`echo %~1`) do SET
"%~2=%%f.%%g.%%h"
:truncateIP_xit
================================================== ==============
--
Pete Cresswell
  #7  
Old December 27th 14, 11:33 PM posted to alt.windows7.general
Joe Morris
external usenet poster
 
Posts: 289
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

"(PeteCresswell)" wrote:
Per Joe Morris:


for /F "usebackq delims=. tokens=1-3" %%f in (`echo 11.22.33.44`) do echo
%%f.%%g.%%h


Nice!!!... thanks.... I could not have come up with that if
my life depended on it.


The FOR command can do a lot for you, but it can also do a lot *to* your
patience if you encounter some of its odd corners...especially since the
diagnostic error messages it uses comply with the alleged Microsoft policy
of taking lots of words to say absolutely nothing useful.

BTW: don't try to use WMIC as the emitter with FOR. It may be possible to
do that but I have yet to figure out how to use WMIC inside FOR.

I've used FOR in a lot of the scripts I write; while PowerShell could be
used, I generally prefer to use FOR in order to get quick execution since
the overhead incurred by loading POSH can be significant. (Of course, if
you do need POSH capabilities anyway (such as when querying WMI), it's
better to do the whole thing in POSH.)

Joe


  #8  
Old December 27th 14, 11:35 PM posted to alt.windows7.general
Stan Brown
external usenet poster
 
Posts: 2,904
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

On Sat, 27 Dec 2014 13:50:19 -0800, Gene E. Bloch wrote:
Also AWK :-)

Same comment about "trivially easy".

Back in the day I really liked AWK, but by now I'd have to start at the
kindergarten level again...

It's still around:

Gawk for Windows
http://gnuwin32.sourceforge.net/packages/gawk.htm


Indeed yes, though it strikes me as overkill for the OP's problem.

I use AWK (GAWK, really) for all my Web pages. I've got quite an
elaborate macro-processing problem written in AWK, to make sure that
all the parts of the final Web pages are consistent with each other.

I also use AWK to prepare grade reports for students. I export my
Excel gradebook as a CSV text file, and then my AWK program converts
that to human-readable text.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
  #9  
Old December 28th 14, 12:29 AM posted to alt.windows7.general
Gene E. Bloch[_2_]
external usenet poster
 
Posts: 7,485
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

On Sat, 27 Dec 2014 18:35:38 -0500, Stan Brown wrote:

Also AWK :-)

Same comment about "trivially easy".

Back in the day I really liked AWK, but by now I'd have to start at the
kindergarten level again...

It's still around:

Gawk for Windows
http://gnuwin32.sourceforge.net/packages/gawk.htm


Indeed yes, though it strikes me as overkill for the OP's problem.


I didn't realize that one had to use only simple tools for simple
problems.

AWK was a lot of fun for me, and when I was relatively fluent in it, I
was glad to use it for any old problem. Often, even with a simple
problem, it seemed easier to make a quick solution by using the powerful
tool.

BTW, the OP's problem is a bit ambiguous to me. He gave no indication of
where the IP addresses were coming from, and I was left wondering why he
didn't just manually remove the last octet from the one IP address he
was looking at (that's how I read it; I'm sure I was under-interpreting,
however).

--
Gene E. Bloch (Stumbling Bloch)
  #10  
Old December 28th 14, 03:13 AM posted to alt.windows7.general
(PeteCresswell)
external usenet poster
 
Posts: 1,933
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

Per Gene E. Bloch:
BTW, the OP's problem is a bit ambiguous to me. He gave no indication of
where the IP addresses were coming from, and I was left wondering why he
didn't just manually remove the last octet from the one IP address he
was looking at...


"Octet".... now I know a new word... -)

The long story is that I'm having trouble with some IP cameras, but the
first zinger is that only 3 cams are giving trouble and one is working
a-ok....

When I do a Ping -t against the 3 problem children, it looks as if they
are re-booting over-and-over again: a bunch of Replys, then a few
"Request timed out.", then a few "Destination host unreachable"....
repeated over-and-over.

The second zinger is that, every so often, one of the cams becomes
reachable long enough for the camera server to get a short clip.

So I wanted to make a .BAT file to ping the cams with -t and log the
time-stamped results so I could see if anything informative was revealed
around the time of a clip.

First thing, I wanted to be able to invoke the .BAT file just by typing
"PingLog 145" to start the process for the camera with IP address
10.0.0.145.

Then I figured "Let's make this thing portable", so I extracted the IP
address of the machine running the .BAT file with the intent of
stripping off "The last octet".... so the .BAT file would run on a LAN
where the addresses were, for instance, in the vein of 192.168.0.123
instead of 10.0.0.123.

That's where my question came from.

Here's the .BAT file as it is now... still needs work, but it's doing
the job I want it to do - with the caveat that I'm coming from VB and
doing this mostly by rote with little-or-no understanding.

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++
:* PURPOSE: To create a log of timestamped pings
:* ACCEPTS: Either the last 3 numerics of the device
:* address OR the entire address tb pinged
:* e.g. for 10.0.0.151, you would supply 151
:* For PeteCress.DynDNS.org you would
:* supply PeteCress.DynDNS.org.
:* EMBEDDED: - "myTargetDir": Target directory for output file
:*
:* NOTES: - This does NOT work under XP. Only 7
:* (and maybe 8) The ping part is what
:* does not work.
:*
:* PROBLEMS: - If we run during the midnight date changeover,
:* myCurDate never gets updated
:*
:* REV: .003

ECHO OFF

:* --------------------------------------------------------
:* Set embedded values

SET myTargetDir=D:


:* --------------------------------------------------------
:* Parse the command-line argument and concoct the target
:* IP address. If 3 chars, tack them on to local domain
:* otherwise just use the command-line argument as-is
:* e.g. "151" vs "ExtremeSurfcam.DynDNS.org"

SET myCommandLineParm=%1

CALL :stringLenCompute myStringLen myCommandLineParm

IF %myStringLen% gtr 3 GOTO :useEntireParm
:* ---------------------------------------------------
:* Get local IP addres, strip off the suffix, and then
:* concat the command-line argument to it

CALL :getLocalIP myFullIP
CALL :truncateIP %myFUllIP% myTruncatedIP
SET myTargetIP=%myTruncatedIP%.%1
GOTO :concattedParmToDomain

:useEntireParm
SET myTargetIP=%1
:concattedParmToDomain


:* --------------------------------------------------------
:* Capture/set other variables

SET myTargetPath=%myTargetDir%\PingLog.%1.txt

FOR /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (call :setDate %%i
%%j %%k %%l)
SET myCurDate=%YY% %MM%-%DD%


:* --------------------------------------------------------
:* Display variables (for debugging purposes)

ECHO commandLine=%1
ECHO stringLen=%myStringLen%
ECHO myCurDate=%myCurDate%
ECHO myFullIP=%myFullIP%
ECHO myTruncatedIP=%myTruncatedIP%
ECHO myTargetIP=%myTargetIP%
ECHO myTargetDir=%myTargetDir%
ECHO myTargetPath=%myTargetPath%


:* --------------------------------------------------------
:* Delete any existing output file

DEL %myTargetPath%



:* --------------------------------------------------------
:* Do the deed: keep looping the ping command and stamping
:* each reply with date/time

FOR /f "tokens=*" %%A in ('ping %myTargetIP% -n 1 ') DO (ECHO
%%A%myTargetPath% && GOTO Ping)

:Ping
FOR /f "tokens=* skip=2" %%A in ('ping %myTargetIP% -n 1 ') DO (ECHO
%myCurDate% %time% %%A%myTargetPath% && GOTO Ping)




:* ================================================== ========================================
:* Subroutines called from main loop above


:getLocalIP theLocalIP
:* --------------------------------------------------------
:* Obtain the local IPv4 address of the computer we are running on

for /f "delims=[] tokens=2" %%a in ('ping %computername% -n 1 -4 ^|
findstr "["') do (set %~1=%%a)
:getLocalIP_xit
EXIT /b



:setDate
:* --------------------------------------------------------
:* Extract month/day/year from command line's Date command
:* so we can time/date stamp our output
:* NB: We *should* to do this with each ping in order to cath
:* the switchover at midnight... but we do not

if "%1:~0,1%" gtr "9" shift
for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo,^|date') do (set
%%m=%1&set %%n=%2&set %%o=%3)
EXIT /b



:stringLenCompute resultVar stringVar
:* --------------------------------------------------------
:* Determine length of a given string
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)



:truncateIP theFullIP theTruncatedIP
:* --------------------------------------------------------
:* Take a complete IP address (e.g. 123.456.789.321)
:* and lop off the last octet and it's dot, returning
:* only the remainder, without the dot (e.g. 123.456.789)

FOR /F "usebackq delims=. tokens=1-3" %%f in (`echo %~1`) do SET
"%~2=%%f.%%g.%%h"
EXIT /b

:* ====================================== END OF FILE ===============================================

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++

--
Pete Cresswell
  #11  
Old December 28th 14, 02:16 PM posted to alt.windows7.general
Stan Brown
external usenet poster
 
Posts: 2,904
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

On Sat, 27 Dec 2014 16:29:47 -0800, Gene E. Bloch wrote:
I didn't realize that one had to use only simple tools for simple
problems.


:-)

AWK was a lot of fun for me, and when I was relatively fluent in it, I
was glad to use it for any old problem. Often, even with a simple
problem, it seemed easier to make a quick solution by using the powerful
tool.


I confess that I use AWK on some problems that might well be more
suited to SED, and for just that reason: I'm fluent in AWK and much
less so in SEC.



--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...
  #12  
Old December 28th 14, 11:41 PM posted to alt.windows7.general
Gene E. Bloch[_2_]
external usenet poster
 
Posts: 7,485
Default .BAT or .CMD Routine To Truncate IP Addr's Last Digits?

On Sun, 28 Dec 2014 09:16:45 -0500, Stan Brown wrote:

On Sat, 27 Dec 2014 16:29:47 -0800, Gene E. Bloch wrote:
I didn't realize that one had to use only simple tools for simple
problems.


:-)

AWK was a lot of fun for me, and when I was relatively fluent in it, I
was glad to use it for any old problem. Often, even with a simple
problem, it seemed easier to make a quick solution by using the powerful
tool.


I confess that I use AWK on some problems that might well be more
suited to SED, and for just that reason: I'm fluent in AWK and much
less so in SEC.


I've let those applications slide out of my area of competence, mostly
because I'm not doing anything that needs automation like that.

Sometimes I regret that and therefore plan to do something about it. I
probably mention AWK to myself twice a year.

But like my situation in Spanish, I let it slide again :-)

Occasionally I'll use SED or similar commands in a slangy way, such as
to post a correction in the form s/rror/error/

--
Gene E. Bloch (Stumbling Bloch)
 




Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off






All times are GMT +1. The time now is 05:55 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 PCbanter.
The comments are property of their posters.