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

x
?
Solved

Set static IP based on current IP address

Posted on 2007-04-10
9
Medium Priority
?
484 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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