Currently, I'm using this:
netsh wlan add filter permission=block ssid="WLAN1" networktype=infrastructure
to hide specific WLAN SSIDs from showing up in the systray. Since there's usually more than one of them available, I've decided to put them all in a *.bat file, like so
#echo off
netsh wlan add filter permission=block ssid="WLAN1" networktype=infrastructure
netsh wlan add filter permission=block ssid="WLAN2" networktype=infrastructure
netsh wlan add filter permission=block ssid="AnotherWLAN" networktype=infrastructure
etc
and to block them all with a single click.
However, this keeps them hidden only while the blocked WLANs have those predefined SSIDs. If they change their SSID, they show up again, and I have to change my file. This isn't a problem when there's a few of them, but there's usually more than 20 showing up.
WHAT I WOULD LIKE TO DO
Is there a way for me to use netsh and, say, an if, for, or while loop, to block everything BUT the one SSID I choose? For example, in (broken) pseudo code
SET myWLAN = Home // e.g. home WLAN SSID = Home
if (! SSID == myWLAN) {
loop through the available SSID, and block them via netsh
}
Or, would you recommend that I just go with:
netsh wlan add filter permission=denyall networktype=infrastructure
and then create a special whitelist filter for my home WLAN
netsh wlan delete filter permission=block ssid="myWLAN SSID" networktype=infrastructure
I'm pretty much new to all of this, so any help would be more than welcome.
#ECHO OFF
SETLOCAL
SET "allow=WLAN1"
FOR /f "tokens=3*" %%a IN ('netsh wlan show all^|findstr /i /b /L /c:"SSID "') DO (
IF "%%b" neq "%allow%" ECHO(netsh wlan add filter permission=block ssid="%%b" networktype=infrastructure
)
GOTO :EOF
This batch searches the netsh wlan show all output for lines /b beginning /i regardless of case /L the literal /c: this constant string.
The tokenising is performed using tokens 3 and * (the remainder of the line) using default delimiters hich include space, so a typical SSID line filtered would be
SSID 5 : WLAN2
Token 1 is SSID, 2 is 5, 3 is : and 4 is WLAN2
Since the first nominated token is 3, that token is assigned to %%a and the next token is assigned to %%b.
Then if the token now in %%b does not match the allowed string, block it (well, the command is simply echoed for debugging - change the echo(netsh to netsh to activate.
If you were to use
set "allow=%~1" instead of `...WLAN1` then you could run<br>
thisbatch WLAN4
to block all except WLAN4.
Related
I need to configure multiple machines to automatically set their IP addresses from dynamic to static.
I have a batch file to set the IP address to static like this :
netsh interface ipv4 set address name="Ethernet" source=static ^
addr=192.168.200.200 mask=255.255.255.0 gateway=192.168.200.1
pause
exit /b 0
But I have to manually write the IP address, mask and gateway in the .bat file.
How can I retrieve the current IP address of the machine (as well as the current mask and gateway) and automatically set them to static, without modifying the .bat file every time?
First you need to retrieve the current ip address.
Probably this is easy, if you use always the same name for the interface.
setlocal
set "interface_name=Ethernet"
REM *** Convert the output lines from netsh into variables
for /F "tokens=1,* delims=:" %%a in ('"netsh interface ipv4 show addresses name="%interface_name%""') do (
REM *** Get rid of leaing spaces and TABS
for /F %%a in ("%%a") DO for /F %%b in ("%%b") DO (
set "__%%a=%%b"
)
)
REM *** Show all variables beginning with "__"
set __
if "%__DHCP%" NEQ "Yes" goto :no_dhcp
:check_ip_prefix
set "ip_addr=%__IP-address%"
if "%ip_addr:192.168.=%" EQU "%ip_addr%" goto :wrong_ip_prefix
netsh interface ipv4 set address name="%interface_name%" source=static ^
addr=%ip_addr% mask=255.255.255.0 gateway=%__default_gateway%
Probably you need to change some variable names, because the names are language dependent (and I'm using a German version).
With Windows 10 is it possible to setup up known networks and be able to connect to them without all the mouse movement and click?
Using Windows batch files, you can set it up to connect to networks you already know (Network1 or Network2, below) without ever touching the mouse.
#echo off
setlocal EnableDelayedExpansion
for %%i in ("Network1"
"Network2") do (
netsh wlan show networks mode=ssid | findstr /C:%%i
if !ERRORLEVEL! EQU 0 (
echo "Found %%~i - connecting..."
netsh wlan connect name=%%i
exit /b
) else (
echo "Did not find %%~i"
)
)
#echo on
Save the above to .bat and run it from cmd.exe or a program like Listary.
Some comments about the code:
If more than one of your listed networks are available, it will connect to whichever is first in the for loop list. You could also put the list in a file and change for %%i to for /F %%i
EnableDelayedExpansion and "!" around ERRORLEVEL
are needed to keep the variable ERRORLEVEL from being assigned
whatever it was at the beginning of the script. Since I don't
normally program Windows batch files, this is 2 hours of my life
gone that you won't have to deal with.
All the echoing is for debugging; the echo off at the top squelches it.
%% needed for variables in Windows batch files. The variable is referenced with % at the command line.
%%~i strips the quotation marks around the string when outputting to stdout.
My goal is to have a batch script that whenever launched if the machine is connected to my home wifi (SSID = The Sanctum Sanctorum - 5GHz) will launch Kodi from one folder ("C:\Program Files (x86)\Kodi at Home") but otherwise will launch Kodi from another ("C:\Program Files (x86)\Kodi").
Any help is much appreciated and thanks for your time I have attached updates with my code progress!
Edit #3 - Thanks to #treintje
Code Attempt #3
#echo off
set "ssid=The Sanctum Sanctorum - 5GHz"
netsh wlan show interfaces | findstr /r /c:"^ *SSID *: %ssid%$" 1>nul
if %errorlevel% equ 0 (
set "kodi=Kodi at Home\Kodi.exe" "-p"
) else (
set "kodi=Kodi\Kodi.exe"
)
"%programfiles(x86)%\%kodi%"
Now it seems even when connected to the specified network it always launches the "kodi=Kodi\Kodi.exe" version. I bet it's just a tiny syntax error I don't know to see as I'm sure treintje is right this is the way to do it!
Assuming your machine only has a single wireless network interface card installed, you could try something like this:
#echo off
set "ssid=The Sanctum Sanctorum"
netsh wlan show interfaces | findstr /r /c:"^ *SSID *: %ssid%$" 1>nul
if %errorlevel% equ 0 (
set "kodi=Kodi at Home\Kodi.exe" "-p"
) else (
set "kodi=Kodi\Kodi.exe"
)
"%programfiles(x86)%\%kodi%"
The netsh wlan show interfaces command lists information about all wireless network interfaces including the SSID of any wireless network that they might be connected with. If the findstr command finds the SSID specified by the ssid variable, the errorlevel is set to zero.
If the target application cannot handle command-line switches that are encapsulated within quotes, you could set the kodi variable and call the application as such:
set "kodi=Kodi at Home\Kodi.exe" -p"
"%programfiles(x86)%\%kodi%
This is not the full scope of what I'm doing. I've distilled down...
I'm sure there is something tiny and stupid I'm failing to account for here, I just don't see it.
Can someone please tell me why I can execute the following netsh command successfully:
netsh wlan show profile name="SomeWifi"
Yet, it fails in a for loop?
for /F "tokens=1,2 delims=:" %a in ('netsh wlan show profile name="SomeWifi"') do echo %a
Instead of the profile info, in the for loop it spits out this error message:
There is no such wireless interface on the system.
What am I missing? Is the context changed in the parenthesis (like the user)? Is there a character escape issue?
There are some characters that need to be escaped (using ^ as a prefix) when directly used in a for /f command.
Some of them are the usual & and | that having a special meaning to the parser seem that obviously need scaping. Another problematic character is the closing parenthesis ()) that can be seen as the closing parenthesis of the in clause of the for command.
But some characters (ex. ,, ;) need escaping just because they are seen as delimiters and removed. In your case = generates the problem
You can use
for /F "tokens=1,2 delims=:" %a in ('netsh wlan show profile name^="SomeWifi"') do echo %a
I've written a Windows script to change NIC interface metrics, and need to condense it to two commands, because of the manner in which it is executed. To render a long story short, I support an application (BladeLogic Server Automation [BSA]) that uses remote agents to call system commands.
I've hypothesized that when BSA runs the script, each command executes in a separate Command Prompt environment, so the environment variables used to store the route strings aren't persistent.
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=1.1.1.1"') do #set VAR1=%a
netsh interface ipv4 set %VAR1:~4% metric=200
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=2.2.2.1"') do #set VAR2=%a
netsh interface ipv4 set %VAR2:~4% metric=500
I've condensed the script as such and am testing it at the Command Prompt.
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=1.1.1.1"') do #set VAR1=%a && netsh interface ipv4 set %VAR1:~4% metric=200
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=2.2.2.1"') do #set VAR2=%a && netsh interface ipv4 set %VAR2:~4% metric=500
Unfortunately, it doesn't seem to recognize the proper syntax for the second command:
The following command was not found: interface ipv4 set %VAR1:~4% metric=200
Is there another way I could append the second command, so it's interpreted as being syntactically correct? I'm open to suggestions!
Matt,
You're correct that BladeLogic fires up separate shells for each statement, however, depending on what you're trying to achieve, it may be possible to "persuade" it to run a complete script remotely on your target.
If you're still facing this issue, please respond with a little more info about what you're trying to achieve and which approaches you've tried, and we can look a the alternatives.
-John.