Jump to content

Detect Specific Network?


Recommended Posts

Hey guys, 

It has been a long time since posting on the forum.  (The Help manual is great!)

I have spent a good bit of time looking over the help manual, as well as searching the forum, and I have been unable to find a function that I can use in my latest program.

I am creating a simple program for the new guys at work, to help simplify some of the remote operations we perform.  History has shown that the learning curve can be pretty steep; but if I can help make it more simple for them, it would be great.

The program uses the DriveMap function to connect with a specific station (IP), then using a specified file path (kept in a SQLite database) performs a ShellExecute to view/modify the configuration file.

One thing I have learned with programming is that you have to try and think of every possible scenario the "User" may encounter.  One such case is when they try to connect to a station, without having first connected to the network.  It would be nice if the program could detect whether or not the computer is connected to a specified network.

Is there a function that could detect a connected network by comparing the network ID with the name pulled from a database?

Thanks in advance for any help guys!

If $uHappy = True And $uKnow_it = True Then

      For $i = 1 To 2 Step 1

              Clap()
      Next

EndIf

 

Link to comment
Share on other sites

My end users run an app I wrote that will self update.  I don't want them checking across the WAN so I have location checking.  Each location has a different IP subnet, (192.168.*, 192.169.*, etc).  So I look at the second octet in the IP.  What's your IP structure like between each location?

Are Gateways the same or different?  Is the machine's OU in a specific location OU?  You could query AD for this as well.  There are a lot different ways to do it.  

I did it IP based with a query of WMI to get all active network cards.  The array in the function is what determines the server based on client IP.  If no servers are found I return the first server to "cache" since my application can run without that connection.

$sServer = _PC_GetServerName()
 
If _PC_IsOnline($sServer) Then
ConsoleWrite($sServer & " is ON-LINE" & @CRLF)
Else
ConsoleWrite($sServer & " is OFF-LINE" & @CRLF)
EndIf
 
Func _PC_IsOnline($sRPC)
Local $iPing, $iError
$iPing = Ping($sRPC, 250)
If @error Then $iError = @error
If $iPing > 0 Then
Return 1
Else
Return SetError($iError,0,0)
EndIf
EndFunc
 
Func _PC_GetServerName($sRPC = @ComputerName)
Local $objWMIService, $colAdapters, $n, $asIP
 
Local $asServers[4][2]
$asServers[0][0] = "SERVER01"
$asServers[0][1] = "168"
$asServers[1][0] = "SERVER02"
$asServers[1][1] = "169"
$asServers[2][0] = "SERVER03"
$asServers[2][1] = "170"
$asServers[3][0] = "SERVER04"
$asServers[3][1] = "171"
 
$objWMIService = ObjGet("winmgmts:\\" & $sRPC & "\root\CIMV2")
$colAdapters = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
$n = 1
For $objAdapter in $colAdapters
If Not ($objAdapter.IPAddress) = " " Then
For $i = 0 To UBound($objAdapter.IPAddress)
If StringLeft($objAdapter.IPAddress($i), 3) = "172" Then
$asIP = StringSplit($objAdapter.IPAddress($i), ".", 3)
For $m = 0 To UBound($asServers) - 1
If $asServers[$m][1] = $asIP[1] Then Return $asServers[$m][0]
Next
EndIf
Next
EndIf
 
$n = $n + 1
Next
Return $asServers[0][0]
EndFunc
Link to comment
Share on other sites

The way it is setup, all stations have identical IP's except for the last 3 digit set. (192.168.24.XXX).  They're all connected to the same switch.

The program does work the way it is.  The DriveMap function sets @error if it can't connect, and i simply pop up a message box telling the user that they must be connected to the network.   I just thought it would be nice to check for the connection before attempting to connect.

If $uHappy = True And $uKnow_it = True Then

      For $i = 1 To 2 Step 1

              Clap()
      Next

EndIf

 

Link to comment
Share on other sites

Oh so you know the server than.

Just use the IsOnline function from the script above.

If _PC_IsOnline("NAMEOFFILESERVER") Then
  MsgBox(0, "Connecting Drives", "Hit ok to connect to drives..")
  ;DriveMapAdd(.............
Else
  MsgBox(0, "Error Reaching Network", "Connect to the network and try again..")
EndIf
 
Func _PC_IsOnline($sRPC)
  Local $iPing, $iError
  $iPing = Ping($sRPC, 250)
  If @error Then $iError = @error
  If $iPing > 0 Then
    Return 1
  Else
    Return SetError($iError,0,0)
  EndIf
EndFunc
Edited by ZacUSNYR
Link to comment
Share on other sites

PING! Dang it, why didn't I think of that.  I could ping any station to check for the connection.  I really appreciate that.  Thanks so much for you're time.

The @error that currently sets when the DriveMap function fails, causes a 10-15 second pause before it reports.  Most of these guys freak out when it just sits there and does nothing for too long.

I appreciate your posting the function above, but I have to try and figure this out on my own.  I've never copied anyone's code before.  Please don't be offended; like I said, I really appreciate your help!

Edited by NitNG4le
If $uHappy = True And $uKnow_it = True Then

      For $i = 1 To 2 Step 1

              Clap()
      Next

EndIf

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...