Solved

Script to determine ip address and launch a web page based on first 3 octets

Posted on 2011-02-21
5
491 Views
Last Modified: 2012-05-11
I need a script in python, or vb script which, when run on a machine, will determine the machine's IP address and then open a web browser. What I'm trying to do is check a remote LAN's IP address range to see if it's the same as my own LAN. If it is, one URL is launched. If not, another URL is launched.
Python, or vb script would be preferred.
0
Comment
Question by:lessthan2
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Psy053
ID: 34947600
Try this:
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapter = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")

For Each objItem in colAdapter
	For Each strAddress in objItem.IPAddress
		If InStr(strAddress, "192.168.1.") Then
			Results=True
		End If
	Next
Next 

If Results=True Then
	WScript.Echo "Address is within the 192.168.1.* range"
Else
	WScript.Echo "Address is not within the 192.168.1.* range"
End If

Open in new window

0
 
LVL 14

Accepted Solution

by:
Psy053 earned 500 total points
ID: 34947620
Sorry, just to make it a little more complete, the script has been modified to now open IE to a particular page based on whether or not it is in the specified range:



strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapter = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
Set objIE = CreateObject("InternetExplorer.Application") 

For Each objItem in colAdapter
	For Each strAddress in objItem.IPAddress
		If InStr(strAddress, "192.168.1.") Then
			Results=True
		End If
	Next
Next 

If Results=True Then
	objIE.Navigate "http://www.example.com/TRUE"
	objIE.Visible = True
Else
	objIE.Navigate "http://www.example.com/FALSE"
	objIE.Visible = True
End If

Open in new window

0
 
LVL 9

Expert Comment

by:Scottyworld
ID: 34947637
in order to get it to open a web page you would need to add the following to Psy053's script above

2: Set Sh = WScript.CreateObject("WScript.Shell")

15: Sh.Run "http://www.google.com", 3

17: Sh.Run "http://www.experts-exchange.com/", 3
0
 
LVL 9

Expert Comment

by:Scottyworld
ID: 34947640
Lol - beaten to it!
0
 

Author Closing Comment

by:lessthan2
ID: 34947685
Excellent! Works perfect - Thank you.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

770 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