Solved

Set static IP based on current IP address

Posted on 2007-04-10
9
482 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

636 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