?
Solved

.vbs login script to run file based on IP address

Posted on 2011-04-26
8
Medium Priority
?
1,028 Views
Last Modified: 2012-05-11
Hello Experts,
I'm here yeilding to your knowledge.  I have a situation where I need some assistance building a .vbs script to do (what I believe) is a simple task.

I have a scenario which I need to have a .vbs script run on my user's login.  The script should take a look at the IP address of the current machine and based on that address make a decision to run 1 of 2 files.

In my head this is how I envision the code..

- User initiates login
- (this .vbs runs)
- the script will check current IP addresses on the system.
- the script will make a decision (it will challenge the current IP to a set of known static IPs)
- the script (based on the desision) will then run (File A) if the current IP matches the known static IPs or run (File B) if the current IP does not match the known static IPs.

Known static addresses:
10.48.64.14
10.48.64.15
10.48.64.16

Does anyone have a simple code to share with that setup?

Thanks
0
Comment
Question by:irishmic33
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:rlandquist
ID: 35467707
0
 
LVL 2

Author Comment

by:irishmic33
ID: 35467989
Ha... yeh, well thats actually a question I asked about a year ago here on experts exchange.  While that issue was dealing with a range instead of specific static IP addresses.  BTW that solution works perfect for testing the /24 networks.  

Funny you found that one.
0
 
LVL 12

Expert Comment

by:rlandquist
ID: 35468151
Well the first part of your solution would be to get the ip address.  I will upload an example for this, however, there could be multiple addresses for a computer.

Once you have the ip address it is just a matter of using the select case statement.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:irishmic33
ID: 35468237
hmm... you are right.  I wonder what the best way to handle multiple addresses would be.  
0
 
LVL 12

Expert Comment

by:rlandquist
ID: 35468248
I am doing testing on my Win7 machine and I am getting the IPv6 address in response.  I will keep looking into this and let you know what I find.
0
 
LVL 12

Accepted Solution

by:
rlandquist earned 2000 total points
ID: 35468491
OK, Here is a solution that I think will work.
The script get all the ip addresses of the computer.  It ignores 0.0.0.0.
Then it runs the function to check the format.  If the format meeets IPv4 then it exits checking and goes to the select step.

This should work except in the situation where a pc has 2 or more ipv4 addresses, but that is not too normal.

Let me know what you think.
Set objNetwork = CreateObject("Wscript.Network")
Set arrList = CreateObject("System.Collections.ArrayList")

strComputer = "."

'Connect to computer WMI Service
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Query computer network adapter configurations for ip addresses and add to array
Set IPConfigSettings = objWMIService.ExecQuery _
( "Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE" )
For Each IPConfig In IPConfigSettings
    If Not IsNull(IPConfig.IPAddress) Then
        For i = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
            If IPConfig.IPAddress(i) <> "0.0.0.0" And _
                Not arrList.Contains(IPConfig.IPAddress(i)) Then
                arrList.Add IPConfig.IPAddress(i)
            End If
        Next
    End If
Next

'Display IP Addresses
For Each strAddress In arrList
    If is_ip(strAddress) Then
        Exit For
    End If
Next

Select Case strAddress
    Case "10.48.64.14", "10.48.64.15", "10.48.64.16"
        WScript.Echo "run process A"
    Case Else
        WScript.Echo "run process B"
End Select

Function is_ip(ip_adress)
    is_ip = True
    ' split string into list
    ip_adress_lst = Split(ip_adress, ".")
    ' if number of IP adress parts is 4
    If UBound(ip_adress_lst) = 3 Then
        ' validate if every IP part is numeric
        For Each ip_part In ip_adress_lst
            If Not IsNumeric(ip_part) Then 
                is_ip = False
                Exit For
            End If
        Next
    Else
        ' if number of IP adress parts is not 4
        is_ip = False
    End If
End Function

Open in new window

0
 
LVL 2

Author Comment

by:irishmic33
ID: 35468597
Ok, i'm trying it now.  Thanks for your help btw.   I'm pretty wet behind the ears with .vbs.  I can read it, just can't write it.

M
0
 
LVL 2

Author Closing Comment

by:irishmic33
ID: 35469039
Works like a charm.  

Thanks rlandquist.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 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