Skip to content

Instantly share code, notes, and snippets.

@basinilya
Last active July 7, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basinilya/6217576bb044b4dfe23914ba0b2f3f8b to your computer and use it in GitHub Desktop.
Save basinilya/6217576bb044b4dfe23914ba0b2f3f8b to your computer and use it in GitHub Desktop.
restart-wifi-ap.bat: Solve hostednetwork failing after sleep by restarting the physical adapter
# Auto detect text files and perform LF normalization
* text=auto
# Windows CMD does not like LF in batch files
*.bat text eol=crlf
*.cmd text eol=crlf
netsh wlan show hostednetwork 2>&1 | findstr /R /C:"^ ..:..:..:..:..:.. " || ^
call %~dp0restart-wifi-ap.bat
@rem TODO: hostednetwork degrades over time.
@rem TODO: List clients periodically and restart if no clients
@rem TODO: https://superuser.com/questions/555229/it-is-possible-to-see-clients-who-connected-to-my-hostednetwork
@rem TODO: netsh wlan show hostednetwork
@rem TODO: Windows 7:
@rem TODO: ‘®áâ®ï­¨¥ à §¬¥é¥­­®© á¥â¨
@rem TODO: ------------------------
@rem TODO: ‘®áâ®ï­¨¥ : ‡ ¯ã饭®
@rem TODO: BSSID : 01:23:45:67:89:ff
@rem TODO: ’¨¯ à ¤¨®¬®¤ã«ï : 802.11n
@rem TODO: Š ­ « : 11
@rem TODO: —¨á«® ª«¨¥­â®¢ : 1
@rem TODO: 01:23:45:67:89:ab à®¢¥àª  ¯®¤«¨­­®á⨠¢ë¯®«­¥­ 
@set dotfile=%~dp0dotfile.dot
@set sleepcommand=ping 127.0.0.1 -w 1 -n
@echo %date% %time%
netsh interface set interface "Wireless Network Connection" disable
@echo %date% %time%
netsh interface set interface name="Wireless Network Connection" enable
:MOO
@echo %date% %time%
@set tmo=10
:LOO
@if %tmo%==0 goto :OK
@type %dotfile%
@%sleepcommand% 2 >nul
@set /A tmo=tmo-1
@goto :LOO
:OK
@echo.
@echo %date% %time%
netsh wlan start hostednetwork || @goto :MOO
schtasks /create /xml "schtasks-restart-wifi-ap.xml" /tn "restart-wifi-ap"
<?xml version="1.0" encoding="UTF-8"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2019-06-11T05:36:02.2715853</Date>
<Author>MAR2\il</Author>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-Diagnostics-Performance/Operational"&gt;&lt;Select Path="Microsoft-Windows-Diagnostics-Performance/Operational"&gt;*[System[Provider[@Name='Microsoft-Windows-Diagnostics-Performance'] and EventID=300]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\keep\restart-wifi-ap.bat</Command>
<Arguments>&gt;&gt;restart-wifi-ap.log 2&gt;&amp;1</Arguments>
<WorkingDirectory>C:\keep</WorkingDirectory>
</Exec>
</Actions>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment