Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I parse the octets of an IP address into environment variables on a batch file?

Posted on 2008-06-18
4
Medium Priority
?
2,490 Views
Last Modified: 2008-06-27
Greetings -

Does anyone know of a way to parse the octets of an IP address into environment variables in DOS?  There has to be some trick using loops or something to do this.  I would think... :)

What I would like to do is parse the first three octets into one environment variable and just ignore the final octet.

So if I had an IP address if 192.168.100.1, I would like to create an environment variable named %MYIP% and have it contain "192.168.100".

Is this doable?  Thanks in advance...

P.S.  If it would be easier to parse each octet into its own variable such as %OCTET1%, %OCTET2%, etc... I could use that too.  But I'd prefer to have the first three octets in one variable if possible.
0
Comment
Question by:amendala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21817700
I'm not sure how to do it with Batch, but here is a way to do it with VBScript.

This will get the IP of all adapters that TCP/IP is enabled on (usually there is just one) and has a first octet of 10.  You could use the split function to break up the IP address by using a period as a delimiter.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")	'Network Object
 
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set IPConfigSet = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
	'On error commands are a fix for script errors when a new pc runs the script for the first time.
	'On Error Resume next
	'On Error GoTo 0
	
	For Each IPConfig in IPConfigSet
	
		If Not IsNull(IPConfig.IPAddress) Then 
			For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
				ipadds = IPConfig.IPAddress(i)
				WScript.Echo "ipadds = " & ipadds
				If Left(ipadds,2) = "10" Then
					CurrentIP = IPConfig.IPAddress(i)
					MACAddress = IPConfig.Macaddress(i)
				End If
			Next
		End If
	Next

Open in new window

0
 

Author Comment

by:amendala
ID: 21817710
Thanks for the reply Jared by unfortunately, it needs to be done in a DOS batch context given the limitations of the environment that this function must work in.  That said, I'll certainly be keeping that snippet for future reference.  :)
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21822656
Do all these computers only have one NIC when you do an ipconfig?
0
 

Accepted Solution

by:
amendala earned 0 total points
ID: 21823641
They may have multiple NICs but I only want the one that Windows sees as the "primary slot" which is usually the one with a gateway defined.

I managed to write up a simple elegant solution on my own that works great!  I've included the snippet...

:: Ping the computer the batch file is running on one time and parse
:: out the IP address and assign it to environment variable %IP%.
for /f "tokens=2 delims=[]" %%i in ('ping -n 1 "%computername%"') do set IP=%%i
 
:: Echo %IP% to the screen and parse the tokens using a period as a
:: delimeter and assign each octet to a variable.
for /f "tokens=1 delims=." %%i in ('echo %IP%') do set OCTET1=%%i
for /f "tokens=2 delims=." %%i in ('echo %IP%') do set OCTET2=%%i
for /f "tokens=3 delims=." %%i in ('echo %IP%') do set OCTET3=%%i
for /f "tokens=4 delims=." %%i in ('echo %IP%') do set OCTET4=%%i
 
:: Assign octets 1 through 3 to a single variable.
set OCTET13=%OCTET1%.%OCTET2%.%OCTET3%

Open in new window

0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring Remote Assistance for use with SCCM
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question