Batch Script: Use a variable in a netsh command - windows

in my batch script I want that the user makes three inputs: IP-Address, Mask, Gateway, so I can set up my ip config.
I decided to make it like this:
set /p v_ipadress="IP Adresse: "
set /p v_mask="Maske: "
set /p v_gate="Gateway: "
netsh interface ip set address name="WiFi" source=static addr=%v_ipadress% mask=%v_mask% gateway=%v_gate%
The problem is, it doesn't change my ip config. What is the problem? Can't the variable save it in the form: 192.168.178.1 ???
Greetings

Modify the last line to
netsh interface ip set address name="WiFi" static %v_ipadress% %v_mask% %v_gate%
and make sure to run the script as administrator

Related

How to allow user input batch script & parse the value to cmd

#echo off
title Remote Message
set /p input1= "Type the PC name: "
set pcname=%input1%
set /p input2=echo Type a message:
set msgmsg=%input2%
cmd msg /server:%pcname% */time:999 "%msgmsg%"
pause
Above is the batch script
how can I allow user input the pc name and the message he wanted to send remotely thru cmd? I tried 100times above code and keep modify but still not work.
No need to set variables from variables, and please do NOT name your batch file msg.bat or msg.cmd just call it something like send_message.cmd:
#echo off
title Remote Message
set /p "pcname=Type the PC name: "
set /p "msgmsg=Type a message: "
msg /server:%pcname% */time:999 "%msgmsg%"
pause

Batch File not running, command works from CMD. IP Change

I am trying to get a basic IP change batch file to work. The commands I am using work in command prompt.
Batch File:
#echo off
netsh interface ipv4 set address name=”Ethernet” static 192.168.1.236 255.255.255.0 192.168.1.1
#echo off
netsh interface ipv4 set dns name=”Ethernet” static 8.8.8.8
pause
I am attempting to use this and another batch that sends it back to DHCP. This file worked 1 time, but doesn't work anymore. I am opening the file as administrator.
I can run these commands in CMD and they work fine. Any suggestions?
- Windows 10
I got this script off of Stack someplace once apon a batch code search. I have modified the DNS to use Google's DNS for the Primary and Secondary DNS's and also made some cosmetic changes for use in one of my own scripts but i'm sure it will answer your question. its has worked alright for me.
All the IP Address are default network setup address with exception to the DNS, you will have to change them manually in a TXT file for your own uses. Just save it as a batch script, if you have a problem, I can find and post the original code, I have it backed up on a cloud somewhere, just send me a message.
Hope it helps.
:again
cls
TITLE Ip Changer
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ECHO ~ 1. Change stack to static IP address, mask, and DNS Server(s) ~
ECHO ~ 2. Change stack to DHCP network configuration ~
ECHO ~ 3. Change stack to SB200 IP configuration ~
ECHO ~ 4. Change stack to SB600B IP configuration ~
ECHO ~ 5. Change stack to T_U configuration ~
ECHO ~ 6. Change stack to T_P configuration ~
ECHO ~ 7. Main Menu ~
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SET /p choice=
IF /I [%choice%]==[1] GOTO Static
IF /I [%choice%]==[2] GOTO DHCP
IF /I [%choice%]==[3] GOTO SB200
IF /I [%choice%]==[4] GOTO SB600
IF /I [%choice%]==[5] GOTO T_U
IF /I [%choice%]==[6] GOTO T_P
IF /I [%choice%]==[7] GOTO menu
CLS
ECHO INCORRECT CHOICE CHOOSE AGAIN
GOTO again
PAUSE>NUL
ECHO.
:Static
ECHO.
SET /P Addy=IP Address:
SET /P Sub_Mask=Subnet Mask:
SET /P GW=Gateway IP Address:
SET /P DNS_1=Primary DNS Server:
SET /P DNS_2=Secondary DNS Server:
netsh interface ip SET address "Local Area Connection" static %Addy% %Sub_Mask% %GW% 1
netsh interface ip delete dns "Local Area Connection" all
IF NOT [%DNS_1%]==[] netsh interface ip SET dns "Local Area Connection" static %DNS_1%
IF NOT [%DNS_2%]==[] netsh interface ip add dns "Local Area Connection" %DNS_2% index=2
ECHO.
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:DHCP
ECHO.
#ECHO Setting up Local Area Connection for DHCP Configuration
netsh interface ip SET address "Local Area Connection" source=dhcp
netsh interface ip SET dns "Local Area Connection" source=dhcp
ECHO.
ipconfig
ECHO.
PAUSE
GOTO again
:SB200
ECHO.
#ECHO 10.10.241.205
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 10.10.241.205 255.255.255.0 10.10.241.201 1
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:SB600
ECHO.
#ECHO 192.168.0.102
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 192.168.0.102 255.255.255.0 192.168.0.1 1
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:T_U
ECHO.
#ECHO 172.16.233.215
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 172.16.233.215 255.255.255.0 172.16.233.214 1
netsh interface ip show config
ECHO.
PAUSE
GOTO again
:T_P
ECHO.
#ECHO 10.10.10.10
netsh interface ip delete dns "Local Area Connection" all
netsh interface ip SET address "Local Area Connection" static 10.10.10.10 255.255.255.0 10.10.10.1 1
netsh interface ip show config
ECHO.
PAUSE
GOTO :again
P.S. I'v been looking for the person who originally wrote this code. Incase said person reads this, Thanks.

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

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.

How to open ports on Windows firewall through batch file

Is there any way within a batch file to open up specific ports on Windows through a batch file? It would be nice to have the installer do this for our server application rather than having the user manually do it.
Use netsh.exe. A very simple batch file that takes a port argument:
#echo off
rem -- open port (first argument passed to batch script)
netsh advfirewall firewall add rule name="Open Port %1" dir=in action=allow protocol=TCP localport=%1 remoteip=10.15.97.0/24,10.17.0.0/16
This is an extension of solution provided by #Kevin Richardson.
Note that "netsh advfirewall add rule" command will create a new rule with the same name every time you run the same command. The script below helps to prevent it
ECHO OFF
set PORT=8081
set RULE_NAME="Open Port %PORT%"
netsh advfirewall firewall show rule name=%RULE_NAME% >nul
if not ERRORLEVEL 1 (
rem Rule %RULE_NAME% already exists.
echo Hey, you already got a out rule by that name, you cannot put another one in!
) else (
echo Rule %RULE_NAME% does not exist. Creating...
netsh advfirewall firewall add rule name=%RULE_NAME% dir=in action=allow protocol=TCP localport=%PORT%
)

Resources