Jump to content

Automagically Disable/Enable Network Adapter


Recommended Posts

I have a Windows 7 x64 machine that is used as a Security Surveillance machine. 95% of the access to the machine is done remotely..

Sometimes I will push the connection too much and the Network Adapter on the remote machine fails and severs the connection. Worse, it also severs the connection to the 25 IP cameras that the machine is monitoring.. Which, as you can imagine, is a very bad thing...

After such an event, when I finally get on-site local access to the machine, all I have to do is disable, then re-enable the Network Adapter and everything is fine..

What I need is a script or service that will ping an outside IP and, if a response is not received, to disable, then re-enable the Network Adapter.

I have been reading up on NetSH, which seems like it will serve my needs. But I am a hardware person and software programming/scripting is magic, as far as I am concerned..

How many software programmers does it take to change a light bulb?

None. It's a hardware problem.

:)

Another forum pointed me to AutoIt and said that it might help with what I need to do...

On the face of it, it sounds like a simple thing to do.. I was hoping someone here might be able to help out a software Noob... ;)

Thanx in advance...

Michale

Link to comment
Share on other sites

Maybe this helps:

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Something like this?

Adlib it every 15 minutes maybe,

$var = Ping("www.google.com",250)
If @error Then
    netsh interface set interface name="whatever your connection is called" admin=ENABLED"
    Sleep(3000)
    netsh interface set interface name="whatever your connection is called" admin=ENABLED"
EndIf
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Maybe this helps:

Thanx for the reference, funkey...

Reading thru the thread, it looks like smashly did something along the lines of what I need..

Unfortunately, what I read seems to confirm my hypothesis that software programming/scripting is magic! :)

And more unfortunately my subscription to Hogwarts Weekly has ran out... ;)

But I do see the ability to disable then enable a Network Adapter, I am at a lost to figure out how to set it up so that the system would PING an outside IP every x minutes and then do the disable/enable if a response is not forthcoming...

But thanx for the link... It's definitely moved me forward... ;)

Michale....

Link to comment
Share on other sites

Something like this?

Adlib it every 15 minutes maybe,

$var = Ping("www.google.com",250)
If @error Then
    netsh interface set interface name="whatever your connection is called" admin=ENABLED"
    Sleep(3000)
    netsh interface set interface name="whatever your connection is called" admin=ENABLED"
EndIf

Now THAT looks like something I can actually comprehend... somewhat :)

Thanx, Ken.....

So, could I put that in a bat file and call it with Task Scheduler?? Or is it something that I could run constantly??

Michale

Link to comment
Share on other sites

This would just run in the background 24/7 I'm downloading something so I can't test this right now. you can just pull out your network cable or unplug your router then run it and see if your connection is enabled/disabled.

I put comments on all the lines

You might want to take a look at this

;This sets up the connection to be checked every 15 minutes, just multiply the number of seconds by 1000(ms)
Adlibregister("MonitorNet",900000)

;This is just a loop to keep the program alive doing nothing while it's waiting for the next 15 minute check.
While 1
    Sleep(250)
Wend

; Adlib function  -  If google can't be pinged then the local area connection is disabled, waits 3 seconds, and re enables it.
Func MonitorNet()
$var = Ping("google.com",250)
If @error Then
  RunWait(@SystemDir & '\netsh interface set interface name="Local Area Connection" admin=DISABLED"') ;change connection name as needed
  Sleep(3000)
  RunWait(@SystemDir & '\netsh.exe interface set interface name="Local Area Connection" admin=ENABLED"') ;change connection name as needed
EndIf
EndFunc

;If you wanted to get creative use the help file and send out email alerts :)
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

This would just run in the background 24/7 I'm downloading something so I can't test this right now. you can just pull out your network cable or unplug your router then run it and see if your connection is enabled/disabled.

I put comments on all the lines

You might want to take a look at this

;This sets up the connection to be checked every 15 minutes, just multiply the number of seconds by 1000(ms)
Adlibregister("MonitorNet",900000)

;This is just a loop to keep the program alive doing nothing while it's waiting for the next 15 minute check.
While 1
    Sleep(250)
Wend

; Adlib function  -  If google can't be pinged then the local area connection is disabled, waits 3 seconds, and re enables it.
Func MonitorNet()
$var = Ping("google.com",250)
If @error Then
  RunWait(@SystemDir & '\netsh interface set interface name="Local Area Connection" admin=DISABLED"') ;change connection name as needed
  Sleep(3000)
  RunWait(@SystemDir & '\netsh.exe interface set interface name="Local Area Connection" admin=ENABLED"') ;change connection name as needed
EndIf
EndFunc

;If you wanted to get creative use the help file and send out email alerts :)

Thanx again, Ken... I'll refer to that link and go from here.

Thanx a bunch...

Michale....

Link to comment
Share on other sites

OK, I made a little progress.. Meaning I figured out how to convert what you gave me to a usable EXE..

As Alan Jackson said, "Not a big deal, but it's a gold star for me!"... :)

Anyways, a couple things I noticed..

First was a console window would flash very VERY briefly.. I assumed it was the While 1 (250) command thingy that was the delay. But it didn't seem to be regular..

Other than that, nothing happened. I set the RESET time to 1 minute and manually disabled the NIC...

So, then I redid the conversion to EXE but unchecked the x64 option, even though I am on an x64 system and so is the target machine...

This time, I did get the very very brief flash of a console window, but I also got a console window that stayed a bit longer. Long enough for me to see that it was the NETSH window. It did flash a message that appeared to be an error message, but I couldn't catch it..

Is there a way to put in some DEBUG commands or something that would display error messages??

I really appreciate the assistance.. This has been driving me to drink for a while.. Well, actually, it's more of a short walk than a drive. ;)

Michale

Link to comment
Share on other sites

Looks like microsoft remove microsoft disabled that function from netsh on local area and wireless connections.

You can take a look at that script url posted earlier.

If not look at this, it's really quite easy and once you have the command you can just drop that in where you have the netsh commands now, and boom your in business.

http://wlanbook.com/enable-disable-wireless-card-command-line/

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Here are my 2 cents:

; put the compiled EXE (or a reference to it) in the startup folder.
#NoTrayIcon   ; disable tray icon. So, no accidential pause can occour
Sleep(300000) ; allow 5 minutes boot time.
While 1 ; never ending loop
    Ping("google.com") ; try to reach google.
    If @error Then
        Ping("de.pool.ntp.org", 2000) ; try to reach a time server.
        If @error Then
            ShellExecuteWait("ipconfig.exe", "-release *", "", "", @SW_HIDE) ; release the adapters
            Beep() ; 1 second audiable alert
            MsgBox(262144, "", @LF & "NIC Interfasces disabled." & @LF, 5) ; visible alert for 5 seconds
            ShellExecuteWait("ipconfig.exe", "-renew *", "", "", @SW_HIDE) ; restart the adapters
            MsgBox(262144, "", @LF & "NIC Interfasces enabled." & @LF, 5) ; confirm restart
        EndIf
    EndIf
    Sleep(10000) ; sleep 10 seconds
WEnd ; merry go round

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Well, I am not sure what I did..

I had to F&R (Format & Reinstall) the OS on the Security Machine... Had to go with Win7 ULTIMATE..

Anyways, after I did the fresh install, I recompiled the .au3 script to rename the LOCAL LAN ADAPTER... Just for sheets and greens, I went ahead and compiled it with x64 box checked..

I'll be damned if the darn thing didn't work flawlessly!! :mellow:

Runs like a champ.. Checks the PING every three minutes and, if it doesn't get a response, it disables then enables the NIC..

WOOT!!!

Thanx to all who replied.. VERY much appreciated...

Link to comment
Share on other sites

Well, I am not sure what I did..

I had to F&R (Format & Reinstall) the OS on the Security Machine... Had to go with Win7 ULTIMATE..

Anyways, after I did the fresh install, I recompiled the .au3 script to rename the LOCAL LAN ADAPTER... Just for sheets and greens, I went ahead and compiled it with x64 box checked..

I'll be damned if the darn thing didn't work flawlessly!! :)

Runs like a champ.. Checks the PING every three minutes and, if it doesn't get a response, it disables then enables the NIC..

WOOT!!!

Thanx to all who replied.. VERY much appreciated...

strange, make up your mind MS, but glad it works :mellow:

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

  • 1 year later...

Here are my 2 cents:

; put the compiled EXE (or a reference to it) in the startup folder.
#NoTrayIcon ; disable tray icon. So, no accidential pause can occour
Sleep(300000) ; allow 5 minutes boot time.
While 1 ; never ending loop
    Ping("google.com") ; try to reach google.
    If @error Then
        Ping("de.pool.ntp.org", 2000) ; try to reach a time server.
        If @error Then
            ShellExecuteWait("ipconfig.exe", "-release *", "", "", @SW_HIDE) ; release the adapters
            Beep() ; 1 second audiable alert
            MsgBox(262144, "", @LF & "NIC Interfasces disabled." & @LF, 5) ; visible alert for 5 seconds
            ShellExecuteWait("ipconfig.exe", "-renew *", "", "", @SW_HIDE) ; restart the adapters
            MsgBox(262144, "", @LF & "NIC Interfasces enabled." & @LF, 5) ; confirm restart
        EndIf
    EndIf
    Sleep(10000) ; sleep 10 seconds
WEnd ; merry go round

Thanks for your 2 cents! The code works very well, I use it for another purpose.

MDV

Mad Dog (Maurice) Vachon, a great Canadian professional wrestler!

Link to comment
Share on other sites

Perhaps @Exit this should be added to the AutoIt Snippets section? Then old posts (like this) won't be dug up again.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 5 years later...

Hi,

I too want something to enable/ disable network adaptor.

My earlier laptop (HP) was intelligent - it used to detect LAN cable and switch to LAN automatically

When cable was removed, it would connect to the last known good wifi.

 

My current laptop is Dell and requires manual intervention, to switch adapters.

Does anybody know how to build such an intelligent script? 

thanks

Jeevan

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