Solved

run a command based on ip octet

Posted on 2010-09-19
15
1,058 Views
Last Modified: 2012-05-10
I am creating an unattended install of windows 7 that can be used for two different locations. When the computer is booted for the first time I need to silently install one of two programs based on the location. I would like to do this by using a batch file that checks the second octet in the IP address (one location will have a 16 in the second octet and one will have a 17.
0
Comment
Question by:USNHELPDESK
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 3

Expert Comment

by:arweeks
Comment Utility
I'll happily knock you up a vbscript that does this.  Doing it as a batch could be ugly.
0
 
LVL 66

Expert Comment

by:johnb6767
Comment Utility
Really simple in batch as well, using the find command and errorlevels...
@echo off

setlocal

REM replace X below with the 1st octect.....

ipconfig | find /i "x.16" 

if %errorlevel%==0 set subnet=16

ipconfig | find /i "x.17"

if %errorlevel%==0 set subnet=17



if %subnet%==16 "\\path\to\your\app.exe"

if %subnet%==17 "\\path\to\your\other\app.exe"



:end

endlocal

exit

Open in new window

0
 
LVL 3

Expert Comment

by:arweeks
Comment Utility
neat solution :)
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Or you can split it off into a field using my script from here:

http://scripts.dragon-it.co.uk/links/batch-get-tcpip-subnet

echo off
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|find "IP Address"') do call :process %%a & goto :next
:next
echo Subnet is %subnet% and host is %host%
set site=Unknown
if %network%==10 set site=SiteA
if %network%==20 set site=SiteB
if %network%==30 set site=SiteC
if %network%==40 set site=SiteD
if %subnet%==192.168.1 set site=SiteE
if %subnet%==128.127.1 set site=Dragon-IT

echo site is %site%

goto :Eof

:process
echo Found %1
for /f "tokens=1-4 delims=." %%a in ("%1") do (set subnet=%%a.%%b.%%c)&(set host=%%d)&(set network=%%c)



Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
The "network" is set there as Octet 3, "Subnet" as 1.2.3 and host as octet 4.

You may or not have problems if computers have multiple IP's due to VPN etc. or IPv6 but that can be dealt with if needed, or go for the simpler "FIND" solution above.
Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
The other way of course would be to put the computer into different OU or group in Active Directory etc. and use group policy to push the app. down?
0
 
LVL 11

Accepted Solution

by:
Ben Personick earned 500 total points
Comment Utility
try this:

@Echo Off

:: -- Set Constants --
::- Put the first octet here so we can match on it
SET "FirstOctet=192"
::- Enter your commands to match here
SET "Cmd16=C:\admin\test commands\notepad.exe"
SET "Cmd17=C:\admin\test commands\notepad.exe"
::- Leave this as-is
SET "MyIP=NOMATCH"

:: -- Find IP --
FOR /F "Tokens=*" %%I IN ('ipconfig.exe ^| find /i /v "IPv6" ^| find /i "IP" ^| find /i "Address" ^| find /i " %FirstOctet%."') DO SET "MyIP=%%I"
ECHO MyIP Is Now "%MyIP%"&IF /I "%MyIP%"=="NOMATCH" GOTO :End-Script
SET "MyIP=%MyIP:*: =%"
::- Set Command to run based off Second Octet:
FOR /F "Tokens=2 Delims=." %%O IN ("%MyIP%") DO CALL SET "CmdToRun=%%Cmd%%O%%"
::- Run the Command we've matched
ECHO Running "%CmdToRun%"
"%CmdToRun%"

::- End of Script
:End-Script
ECHO Script is Exiting!

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
I misread earlier btw about which octet.  To get second you would change this line:

for /f "tokens=1-4 delims=." %%a in ("%1") do (set subnet=%%a.%%b.%%c)&(set host=%%d)&(set network=%%c)



to

for /f "tokens=1-4 delims=." %%a in ("%1") do (set subnet=%%a.%%b.%%c)&(set host=%%d)&(set network=%%b)


and then %network% would be your 16 or 17.

Steve
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
Please Note: (as it may not be apparent) that in my script I am setting your commands to be called CMD(second octet) ie "Cmd17" for a reason.

My script uses the fact that the commands are held in variables encoded with the octet they run for to very quickly call the correct command instead of having a large series of IF statements. ^^

Also I am weeding out ipv6 addresses, and matching the first octet in case more than one address exists on the host machine.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
i suppose both techniques have their place depending upon the wider picture, i use the script to myself to determine whether on vpn or not and which of a handful of sites if not to decide which drives to map etc. And can easily do goto net%network% or call %site% etc. as needed

And so set the more general variables which can then be used in if / goto or whatever.
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
@Dragon,

Hey! =) Similarly I took mine from a script I wrote for someone to change a group of computer's IP addresses based off subnet (which I in turn took from a script I wrote to set the local administrator's account PW based off subnet.)

  I only saw John's and that is not the greatest because he could be pulling wrong info depending on the host address of the machine (granted it's a rare occurrence)

I like yours it works very similarly to mine, however I'm not a fan of those huge set of IFs you have there they are unavoidable at times though however in this case it works well to store a few commands inn variables and avoid a set of IFs.

About your script though why not just set sites = to Subnets and forget it, or why not create a set of definitions similar to how I did the command?

Actually looking at your script it strikes me that for your purposes your code does more than it needs to.  May I please suggest a slight change for use in your scripting purposes, I left your If statements assuming you prefer them but check this method out:

(Also your GOTO :Next is so that you won;'t check anything except the first adapter correct? -- Just trying to see if I follow why it's been used exactly ^^)




@ECHO OFF
FOR /F "Tokens=2 Delims=:" %%a IN ('IPConfig.exe ^| FIND /I "IP" ^| FIND /I "Address" ^| FIND /I /V "IPv6" ') DO SET "Subnet=%%~na"&SET "Host=%%~xa"&GOTO Next


:Next
ECHO Subnet is "%Subnet%" and host is "%Host%"

SET "Site=Unknown"
IF /I "%Subnet%"==" x.y.10" SET "Site=SiteA"
IF /I "%Subnet%"==" x.y.20" SET "Site=SiteB"
IF /I "%Subnet%"==" x.y.30" SET "Site=SiteC"
IF /I "%Subnet%"==" x.y.40" SET "Site=SiteD"
IF /I "%Subnet%"==" 192.168.1" SET "Site=SiteE"
IF /I "%Subnet%"==" 128.127.1" SET "Site=Dragon-IT"

ECHO Site is "%Site%"

GOTO :EOF

Open in new window

0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
@Dragon:

Also I am checking "IP" and "Address" as separate find statements because inn windows XP once IPv6 is added, and inn Windows Vista and Windows 7 the statements show as:

IPv4  IPv6 and are per adapter. so "IP Address" will not match anything, but "Address" will match two items.  since in OSs without IPv6 the line simply reads "IP Address" we can't catch for "IPv4 Address" because all newer systems woudl not match

SO  i catch for IP, then Address, then NOT IPv6.  whcih will properly ahandle this on all Windows OSs =)
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
fair enough.  is only a simplified version of one that was for a specific purpose with some example checks - I have it checking different octets for different purposes / customers.  Agreed the search needs amending for Win 7 though sadly few customers using it yet.  Also of course I imagine ipconfig output will change based on OS lanuage?

anyway watching 5 yr old do his soccer training at the mo. And had enough of typing on phone!
0
 

Author Comment

by:USNHELPDESK
Comment Utility
Thank you every one. With my novice knowledge using batch files, I was able to use QCubed's solution flawlessly.
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
@Dragon ahh K cool if you actually need to check all octets then you do ^^ just thorugh I saw a neat exception to the rule on your script is all.

@UNHelpDesk Glad the script is working appropriately for you =)

~Q
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

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

744 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