Jump to content

Anyway to monitor network connection?


evansullivan
 Share

Recommended Posts

I am wondering if there is a way to write a script that always stays open and monitors a network connection for a connection?  We have new desktops at our school and they have wireless cards, I am looking for a poor mans LAN\WLAN switching since it's not in the BIOS of the desktops.

Link to comment
Share on other sites

  • Moderators

So are you looking for a network-side tool, something like a constant ping between hosts? Or are you looking on the client side to throw an alert if it drops the wifi connection?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

Correct, was just seeking clarification on your initial post as it was a bit fuzzy. So you want a client side script that will switch automatically to wifi when lan drops. I have an old script that does this, I'll see if I can dig it up and dust it off. In the meantime, if you want to have a go at it, the basics are:

  • Monitor the Event Viewer (Task Scheduler - when a specific event is logged) for the "Network cable unplugged" event
  • When the event occurs, enable the wifi card through netsh.
  • Monitor the Event Viewer for the "Network cable plugged in" event
  • Disable wifi through netsh.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I was going to make a scheduled task based on the event, but I cannot find an event that gets logged when I unplug the cable.

I did find this, I have been playing with it:

Dim $strComputer, $SINK1, $SINK2, $objWMIService
$strComputer = "127.0.0.1"
$SINK1 = ObjCreate("WbemScripting.SWbemSink")
$SINK2 = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($SINK1, "SINK_")
ObjEvent($SINK2, "SINK_")
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\wmi")
If Not @error Then
    $objWMIService.ExecNotificationQueryAsync ($SINK1, "SELECT * FROM MSNdis_StatusMediaConnect")
    $objWMIService.ExecNotificationQueryAsync ($SINK2, "SELECT * FROM MSNdis_StatusMediaDisconnect")
EndIf
ConsoleWrite("In monitoring mode. Press Ctrl+C to exit." & @CRLF)
While 1
    Sleep(10000)
WEnd
;******************************************************************************
Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    ;Trap asynchronous events.
    Switch $objLatestEvent.Path_.Class()
        Case "MSNdis_StatusMediaDisconnect"
            If $objLatestEvent.InstanceName = "Realtek PCIe GBE Family Controller" Then
               MsgBox(0,"","A network connection has been lost: " & $objLatestEvent.InstanceName & @CRLF)
            EndIf
        Case "MSNdis_StatusMediaConnect"
            If $objLatestEvent.InstanceName = "Realtek PCIe GBE Family Controller" Then
               MsgBox(0,"","A network connection has been made: " & $objLatestEvent.InstanceName & @CRLF)
            EndIf
    EndSwitch
EndFunc   ;==>SINK_OnObjectReady

 

Link to comment
Share on other sites

  • Moderators

I recognize that vbscript, have seen something similar online before. It does not alert for me when I unplug network cable. Just to clarify, you're not seeing any event written to the System log when you unplug a cable? That is pretty odd.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Yea, I was looking in system, didn't see anything.

I think what I posted above is going to work, just tweaking it to work how I want it.  I will post my end result when I think I have it.

Thanks for your help Logan (or should I call you Wolverine? Shnickty shnickty shnoine!)

Link to comment
Share on other sites

  • Moderators

If you want to go WMI, you could always do this:

Local $oWMI, $oNICs

$oWMI = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMI) Then
        $oNICs = $oWMI.ExecQuery("SELECT * FROM Win32_NetworkAdapter")
            If IsObj($oNICs) then
                For $sNIC In $oNICs
                    ConsoleWrite("Device: " & $sNIC.Caption & " - Status: " & $sNIC.NetConnectionStatus & @CRLF)
                Next
            EndIf
    EndIf

Connected will show with a NetConnectionStatus of 2

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 4 weeks later...

Just decide what you find important to log using something like you see above that Jlogan3013 provided you.  

obviously you wouldnt want to log 24 by 7 while its constantly detected but if you want to log only when it switches then do something like the following:

;declare your log file for wifi
$wifi_events =  c:\wifi\wifistatus.log ; or wherever you want to keep your log pending on how long you dont mind wanting it to get

;hence if a condition that you configure is met then in reality its a meaningful event
If Not fileexists($wifi_events) then
DirCreate (c:\wifi\)

;can get kinky and put your login connection information here too so that you can put that in your events

$wifi1login = ;declare your ssid or whatever

$wifi1pass = ;declare your password

 

and after you create your condition to switch to wifi because lan is no longer detected right below the instruction to switch to wifi insert this before an end if or however you decide to do it.

_FileWritelog($wifi_events, & @ComputerName & "This wifi utility has determined that the Lan connection has been disconnected.")

after your autoit scripts corrective action to attach to wifi before endif  or however you have decided to configure it

_FileWritelog($wifi_events, " <---> " & @ScriptName & " The Wifi Utility has now begun connecting to Wifi Connection:   " & $wifi1login)

_FileWritelog($wifi_events, " <---> " & "Connecting with password:  " & $wifi1pass )

if @error then MSGBOX(16, "Wifi Utility is having issues connecting to your your WIFI and is longer detecting your lan settings", "Please check your logs for username and password and validate they are still correct.") 

_FileWriteLog($wifi_events, "Your System detected an error connecting to your wifi, a pop up notification has been sent to the user)

 

;Note you can really do anything this was a somewht lazy attempt at helping consider another way.  else there is ways to pull and sort the .evt logs

 

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