Automatically connect to wifi network when no internet detected - windows

EDIT: Found a fix without using Task Scheduler.
#echo off
:loop
set addresses=192.168.1.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
timeout /t 5
goto :loop
it repeats every 5 seconds
I am halfway there but have become stuck. I already have a batch file that can connect the laptop back to the network when manually run but wanted it to run when no internet is detected. So I went ahead and bound it to Task Scheduler with the Trigger being the "Microsoft-Windows-NetworkProfile/Operational" with an ID of 10001 (which is the Disconnect ID) but have no luck with it.
I believe it only triggers when it manually gets disconnected from the internet, not when the wireless card drops the signal on its' own. Any ideas as to what I can do?
OS: Windows 8.1
Wireless Interface: Cable Matters AC600 Dual Band Wireless dongle (doesn't allow Auto Connect to SSID and keeps dropping the connection for unknown reasons)

Found a solution!
#echo off
:loop
set addresses=192.168.1.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
timeout /t 5
goto :loop
What this does is ping an address once (in this case an internal address so I don't get DDoS threats from sites) and if it responds back with an error, it will load a wifi profile.
To get the profile first you will need to type
netsh wlan show profiles
to see what profiles you have (it only loads profiles for the wireless card currently in use). After seeing the names of the profiles you can download the profile by typing
netsh wlan export profile name=[profile name]
I believe it will save to the desktop and from there you can change the code listed at the top of this comment to match the filepath of the xml file to suit your needs.
Cheers!

What about a batch script that regularly performs pings and you inspect the result?
ping some.host > pinged.txt
find "some string which occurs if the network went down" pinged.txt
If %errorlevel% is 0 than the string was found and the connection went down.

Related

How to check internet connection in .cmd (batch script) and run another command based on whether WiFi is connected or not?

Logic is as provided under
Setup WiFi Connection
if connection was successful:
execute command1
else
execute command2
WiFi Setup is done using the following command
netsh wlan connect ssid=%ssid% name=%name%
What I'm interested in is how to formulate my if condition.
netsh wlan show interfaces | findstr "SSID.*%ssid%"
IF %ERRORLEVEL% EQU 0 GOTO connected

How can I assign the name of the current wifi network to a variable in windows batch scripting?

I am trying to add a line to my start up scrip which would automatically start the VPN if I am connected to WiFi networks outside of my work.
I can get the names from netsh wlan commands, but I am not able to just "pick" the SSID/network name from that.
Is there a way to extract particular information from the netsh output or is there a way to output just the wifi name?
Thanks in advance.
You can use the findstr command to find a string in a commands, specifically the SSID.
Script:
#echo off
set "wifiNetwork=example network"
(netsh wlan show networks | findstr /C:": %wifiNetwork%" >nul 2>&1)&&goto :found
echo Wi-Fi network "%wifiNetwork%" not found
goto :end
:found
echo Wi-Fi network "%wifiNetwork%" found!
:end
pause
NOTE: The findstr command is case-sensitive unless you add the /I option

Selenium hover action with IE doesn't work if RDP never connected to Jenkins slaves

We create a bunch of windows Jenkins slaves on Amazon EC2, and make them autologon after they are provisioned.
Once logged on, we use connect the slaves back to master via java web start by execute "ps", with all of this, we can run selenium perfectly with IE except we can't get a higher resolution than 1024*768. We got screenshot, we can see the browser running on the GUI if connected with RDP, just like running on a physical PC.
But a flaw is that, the hover action doesn't work on IE unless we connect to the slave through remote desktop one time before the test starts, just connect once, nothing else, I can even close the RDP session.
I guess something triggered by the RDP connection, just can't figure out what is that.
You can try to adapt your test scripts to have them make an RDP connection before you start your tests, if there is none. This is the code I used for one of our projects to ensure an RDP connection is active:
REM check if user has an RDP session. Will store the session ID of the user in the SESSION variable
call :getsession computername username SESSION
if "%SESSION%" EQU "" (
REM initiate an RDP connection to 'computername', wait, and disconnect by killing the process
mstsc.exe /v:computername & ping -n 5 1.1.1.1 & taskkill /F /IM mstsc.exe
)
REM check again the session
call :getsession computername user SESSION
if %SESSION% NEQ "" (
echo start your tests
)
:getsession
for /F "tokens=1,2,3,4" %%i in ('query session /SERVER:%1 ^| findstr %2') do if %%j EQU %2 (SET %3=%%k) else (SET %3=%%j)
goto :eof
Note that the automatic connection will only be made if you save credentials on the machine from which you start the remote desktop:
cmdkey /generic:computername /user:rdp_username /pass:rdp_password
After executing this once the mstsc.exe will be able to connect to the machine without user interaction.

batch file to detect if wifi adapter is enabled

I tried to complete this commands in cmd but im having trouble fixing it. Can anyone help me?
netsh wlan show networks | FIND "turned off" /I /C
if "dont know what should be here" == 0 (
echo enabled
) else (
echo disabled
)
pause
You're looking for the %errorlevel% variable, which indicates the exit status of the command last executed (in your case find). You have to revert your logic, though, because find returns 0 (i.e. "success") when the adapter is disabled. Also, I'd recommend to do a numeric comparison (equ) instead of a string comparison (==).
if %errorlevel% equ 0 (
echo disabled
) else (
echo enabled
)
All you have to do is attempt to enable it whether or not it's enabled or disabled
netsh interface set interface name="name of adapter" admin=enable || echo already enabled
If the adapter is already enabled then it won't do anything so the double pipes || means if there's an error in the first command it will execute whatever command is after it which is echo "already enabled."
If it says "this network connection does not exist" ignore that, it means the adapter is already enabled.
I put together this code for a batch script. It works like a charm to turn ON/OFF my wireless network connection:
netsh wlan show networks | FIND "Wireless network connection" /I /C
if %errorlevel% equ 1 (wmic path win32_networkadapter where NetConnectionID="Conexión de red inalámbrica" call enable) else (wmic path win32_networkadapter where NetConnectionID="Wireless network connection" call disable)
If you have more than one wireless network connection, change the name for your particular network connection name and that will do.

How to display my PC's IP Address on Windows (7) Startup?

I am on a work computer with dynamic IP address (Ipv4), which usually changes when I restart it. Since I sometimes need to use this PC through remote desktop connection at home I like to keep its IP address handy. But I sometimes forget to check the IP at restart, so is there a batch file or some code which can start up the cmd and display the IPv4 address everytime the computer restarts ?
thanks.
Simple, just create a batch file that runs ipconfig and displays the output:
start cmd /k ipconfig
exit
You'll see a console window appear on your screen each time you execute this batch file that contains the output of the ipconfig command. Among the information displayed will be the IP address for each of your computer's network adapters.
You can configure the command as appropriate, adding switches to ipconfig as desired. For example, adding the /all switch will cause additional information to be displayed.
If you wanted, you could parse the output of ipconfig, extract the IP address assigned to a particular network adapter, and display just that on the screen. That might reduce the cognitive overload. But any good Windows sysadmin can scan the output of ipconfig rather easily.
You can set a batch file to run upon startup.
The batch file should contain:
ipconfig
pause
I used this in a .bat file I created some days ago and it works fine:
#echo off
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo %ip%
This way the IP is defined as a variable named "ip", so you can use this to do other things with your current IP.
I hope it helped somehow.

Resources