Solved

Set static IP based on current IP address

Posted on 2007-04-10
9
470 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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