Solved

Set static IP based on current IP address

Posted on 2007-04-10
9
477 Views
Last Modified: 2012-06-21
Hello,

I am trying to set a static IP address based on the third oct of the current IP address obtained via DHCP with either a batch file or vb script. Any ideas?
0
Comment
Question by:Seykre
[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
  • 5
  • 4
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18886925
Not sure what you mean by that?

This will grab the 3rd octect:

for /f "tokens=3-5 delims=:. " %%a in ('ipconfig^|find "IP A"') do set ThirdOctect=%%c

echo %ThirdOctet%
0
 
LVL 1

Author Comment

by:Seykre
ID: 18890030
I am not familiar with the statement above so in a nutshell I have a network setup with two subnets. One subnet hands out IP's DHCP the other is setup by static assignment. The issue is that I have the user getting an IP address automatically when they come in with their laptop of say 192.168.12.33 and if they get 12 as the third octect I want to assing them a static IP. The above statment may do this but can it be explained so I can set it up?

The whole reason for this is that I have two companies on the same firewall spliting the subnet so the other company does not have access to our systems.

Thank you for your help,

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18890118
This merely loops through the current IP address and determines the 3rd octect.
To add your criteria above, you could use the following, however I'm not sure how you're going to determine if that IP address is already in use?

@echo off
for /f "tokens=3-6 delims=:. " %%a in ('ipconfig^|find "IP A"') do set ThirdOctect=%%c
if [%ThirdOctet%]==[12] goto AssignStatic
goto :eof

:AssignStatic
REM Only come here if the 3rd octect is 12
netsh int ip set addr local static 192.168.12.100 255.255.255.0 192.168.12.1 1
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 1

Author Comment

by:Seykre
ID: 18891249
Here is what I have so far. When I use this script, it closes without doing anything. I think it has something to do with the If statement. I don't think it is reading the string. Any ideas?

@echo off
for /f "tokens=3-6 delims=:. " %%a in ('ipconfig^|find "IP A"') do set ThirdOctect=%%c

if [%ThirdOctet%]==12 goto AssignStatic

goto :eof

:AssignStatic
REM Only come here if the 3rd octect is 12
rem Reset network Settings for static IP
netsh interface ip set address name = "Local Area Connection" source = static addr = 192.168.12.100 mask = 255.255.255.0
netsh interface ip set address name = "Local Area Connection" gateway = 192.168.12.254 gwmetric = 1
netsh interface ip set dns name = "Local Area Connection" source = static addr = xxx.xxx.xxx.xxx
netsh interface ip add dns name = "Local Area Connection" addr = xxx.xxx.xxx.xxx
netsh interface ip show config
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18891269
This has some testing in it..


@echo off
for /f "tokens=3-6 delims=:. " %%a in ('ipconfig^|find "IP A"') do set ThirdOctect=%%c
echo %ThirdOctet%
if [%ThirdOctet%]==12 goto AssignStatic

goto :eof

:AssignStatic
echo AssignStatic
pause
REM Only come here if the 3rd octect is 12
rem Reset network Settings for static IP
netsh interface ip set address name = "Local Area Connection" source = static addr = 192.168.12.100 mask = 255.255.255.0
netsh interface ip set address name = "Local Area Connection" gateway = 192.168.12.254 gwmetric = 1
netsh interface ip set dns name = "Local Area Connection" source = static addr = xxx.xxx.xxx.xxx
netsh interface ip add dns name = "Local Area Connection" addr = xxx.xxx.xxx.xxx
netsh interface ip show config

0
 
LVL 1

Author Comment

by:Seykre
ID: 18892150
Not sure what I am doing wrong but it still does not work. It still keeps closing after the If statement without any further actions. This is a batch file right?
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 18892196
Oh - I'd copied your updated version...notice my original encloses the tests on both sides in brackets...

if [%ThirdOctet%]==12 goto AssignStatic
will not work - make it:
if [%ThirdOctet%]==[12] goto AssignStatic
0
 
LVL 1

Author Comment

by:Seykre
ID: 18892497
Thanks for your help.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18892517
Glad I could help. :^)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

739 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