Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Dark0Prince
- Posts: 4
- Joined: 13 Jul 2017 12:43
#1
Post
by Dark0Prince » 13 Jul 2017 12:49
I'm getting this error in bold and I'm new to batch code. It should change my static IP based on the option I chose A,B or C. "
Invalid address parameter (1). It should be a valid IPv4 address."
Here is my code below.
Code: Select all
@echo off
echo Choose:
echo [A] Set ABQ Static IP
echo [B] Set GJ Static IP
echo [B] Set PB Static IP
echo.
:choice
SET /P C=[A,B,C]?
for %%? in (A) do if /I "%C%"=="%%?" goto A
for %%? in (B) do if /I "%C%"=="%%?" goto GJ
for %%? in (C) do if /I "%C%"=="%%?" goto PB
goto choice
:A
@echo off
echo "Please enter Static IP Address Information"
echo "Static IP Address:"
set /p IP_Addr=10.1.4.53
echo "Default Gateway:"
set /p D_Gate=10.1.4.1
echo "Subnet Mask:"
set /p Sub_Mask=255.255.255.0
echo "Setting Static IP Information"
netsh interface ip set address "Local Area Connection" static %IP_Addr% %Sub_Mask% %D_Gate% 1
netsh int ip show config
pause
goto end
:GJ
@echo off
echo "Please enter Static IP Address Information"
echo "Static IP Address:"
set /p IP_Addr=10.1.2.53
echo "Default Gateway:"
set /p D_Gate=10.1.2.1
echo "Subnet Mask:"
set /p Sub_Mask=255.255.255.0
echo "Setting Static IP Information"
netsh interface ip set address "Local Area Connection" static %IP_Addr% %Sub_Mask% %D_Gate% 1
netsh int ip show config
pause
goto end
:PB
@echo off
echo "Please enter Static IP Address Information"
echo "Static IP Address:"
set /p IP_Addr=10.1.1.5
echo "Default Gateway:"
set /p D_Gate=10.1.1.254
echo "Subnet Mask:"
set /p Sub_Mask=255.255.255.0
echo "Setting Static IP Information"
netsh interface ip set address "Local Area Connection" static %IP_Addr% %Sub_Mask% %D_Gate% 1
netsh int ip show config
pause
goto end
:end
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#2
Post
by penpen » 13 Jul 2017 15:25
I haven't checked the ip addresses and subnets, but i noticed, that you are using "set /p":
Could it be, that you hit the enter key two times, before you get the error message?
The command "set /p" prompts the user for input, echoing alll after the equal sign ('=') as a message to the user.
I wonder if you only wanted to set the values for "IP_Addr", ... in which case you should remove the "/p" from those lines to use the default set command.
penpen
-
Dark0Prince
- Posts: 4
- Joined: 13 Jul 2017 12:43
#3
Post
by Dark0Prince » 13 Jul 2017 16:19
I tried typing it like that but then it doesn't seem to recognize I typed anything as the IP, gateway or mask.
Code: Select all
echo "Static IP Address:"
set IP_Addr=10.1.4.53
echo "Default Gateway:"
set D_Gate=10.1.4.1
echo "Subnet Mask:"
set Sub_Mask=255.255.255.0
-
Dark0Prince
- Posts: 4
- Joined: 13 Jul 2017 12:43
#4
Post
by Dark0Prince » 13 Jul 2017 16:39
I'm now reading a message under IPconfig that says "Autoconfiguration IPv4 Address. . :170.254.175.252"
When i pick option C instead of changing to the IP I gave it. A and B are now working
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#5
Post
by Compo » 14 Jul 2017 05:06
Is this not what you want:
Code: Select all
@ECHO OFF
SET "Con_Name=Local Area Connection"
SET "Sub_Mask=255.255.255.0"
ECHO [A] Set ABQ Static IP
ECHO [B] Set GJ Static IP
ECHO [C] Set PB Static IP
ECHO=
CHOICE /C ABC /M "Choose: "
IF ERRORLEVEL 3 (
SET "IP_Addr=10.1.1.5"
SET "D_Gate=10.1.1.254"
)
IF ERRORLEVEL 2 (
SET "IP_Addr=10.1.2.53"
SET "D_Gate=10.1.2.1"
)
IF ERRORLEVEL 1 (
SET "IP_Addr=10.1.4.53"
SET "D_Gate=10.1.4.1"
)
ECHO Setting Static IP Information . . .
NETSH INTERFACE IPV4 SET ADDRESS "%Con_Name%" STATIC %IP_Addr% %Sub_Mask% %D_Gate% 1
NETSH INTERFACE IPV4 SHOW CONFIG "%Con_Name%"
PAUSE
EditsChanged connection name and subnet mask to variables
Changed show config command to reflect only that changed connection;
(could also use SHOW ADDRESS).
Last edited by
Compo on 15 Jul 2017 05:55, edited 2 times in total.
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#6
Post
by penpen » 15 Jul 2017 04:44
Dark0Prince wrote:I tried typing it like that but then it doesn't seem to recognize I typed anything as the IP, gateway or mask.
If you want to dispaly the values, then you might do something like this (untested sketched):
Code: Select all
set "IP_Addr=10.1.4.53"
echo "Static IP Address: %IP_Addr%"
set "D_Gate=10.1.4.1"
echo "Default Gateway: %D_Gate%"
set "Sub_Mask=255.255.255.0"
echo "Subnet Mask: %Sub_Mask%"
Dark0Prince wrote:I'm now reading a message under IPconfig that says "Autoconfiguration IPv4 Address. . :170.254.175.252"
When i pick option C instead of changing to the IP I gave it. A and B are now working
Did Compo's code solve that issue?
if it does, and you still wish to bugfix your code, then please post the code you were using then.
penpen
-
pieh-ejdsch
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
#7
Post
by pieh-ejdsch » 15 Jul 2017 07:10
@compo The evaluation of the errorlevel by value has to take place from smallest to largest, since the error level with the C at the end is anyway greater than or equal to 3 and greater than 1.
That's why I use the selection in a forloop.
Code: Select all
rem ...
rem standarts (A)
SET "IP_Addr=10.1.4.53"
SET "D_Gate=10.1.4.1"
rem Message
<nul 2>nul choice /c abc /M "Choose:"
for /f "delims=" %%c in ('choice /c abc /n') do (
if %%c==B (
rem set for B
)
if %%c==C (
rem set for C
)
)
Phil
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#8
Post
by Compo » 15 Jul 2017 09:39
pieh-ejdsch wrote:@compo The evaluation of the errorlevel by value has to take place from smallest to largest
IF ERRORLEVEL 1 means if the error level is 1 or greater, that means entering A B and C will match.
From
IF /?Code: Select all
ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.
To evaluate error levels from 1 to 3 you'd need this construct:
Code: Select all
IF %ERRORLEVEL%==1 (
SET "IP_Addr=10.1.4.53"
SET "D_Gate=10.1.4.1"
)
IF %ERRORLEVEL%==2 (
SET "IP_Addr=10.1.2.53"
SET "D_Gate=10.1.2.1"
)
IF %ERRORLEVEL%==3 (
SET "IP_Addr=10.1.1.5"
SET "D_Gate=10.1.1.254"
)
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#9
Post
by penpen » 15 Jul 2017 10:18
@Compo:
I overlooked that error, but in this case pieh-ejdsch is right, because of the part of the if help you have quoted:
If the user has chooses 'C' then the errorlevel is 3, so all three if commands trigger; on errorlevel 2 the last two trigger; so no matter which case, you will end up with "10.1.4.53".
But his solution is also suboptimal, because if you choose for example 'C', then the values are set multiple times, which may produce errors with other "if bodies".
You probably meant to use:
Code: Select all
IF ERRORLEVEL 3 (
SET "IP_Addr=10.1.1.5"
SET "D_Gate=10.1.1.254"
) ELSE IF ERRORLEVEL 2 (
SET "IP_Addr=10.1.2.53"
SET "D_Gate=10.1.2.1"
) ELSE IF ERRORLEVEL 1 (
SET "IP_Addr=10.1.4.53"
SET "D_Gate=10.1.4.1"
) ELSE (
rem CTRL+C or similar
echo(Selection aborted.
goto :eof
)
penpen
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#10
Post
by Aacini » 15 Jul 2017 19:21
IMHO the simplest way to write this code is using an
array, that don't requires a single IF:
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM Define the IP_Addr and D_Gate arrays
SET "IP_Addr[3]=10.1.1.5"
SET "IP_Addr[2]=10.1.2.53"
SET "IP_Addr[1]=10.1.4.53"
SET "D_Gate[3]=10.1.1.254"
SET "D_Gate[2]=10.1.2.1"
SET "D_Gate[1]=10.1.4.1"
SET "Con_Name=Local Area Connection"
SET "Sub_Mask=255.255.255.0"
ECHO [A] Set ABQ Static IP
ECHO [B] Set GJ Static IP
ECHO [C] Set PB Static IP
ECHO=
CHOICE /C ABC /M "Choose: "
SET "IP_Addr=!IP_Addr[%ERRORLEVEL%]!"
SET "D_Gate=!D_Gate[%ERRORLEVEL%]!"
ECHO Setting Static IP Information . . .
NETSH INTERFACE IPV4 SET ADDRESS "%Con_Name%" STATIC %IP_Addr% %Sub_Mask% %D_Gate% 1
NETSH INTERFACE IPV4 SHOW CONFIG "%Con_Name%"
PAUSE
Antonio
-
Dark0Prince
- Posts: 4
- Joined: 13 Jul 2017 12:43
#11
Post
by Dark0Prince » 18 Jul 2017 08:31
Thanks Antonio that one worked cherry for me and I'm no longer getting that IP error.