Solved

Set static IP based on current IP address

Posted on 2007-04-10
9
467 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now