windowsbatch-fileewon

BATCH - ROUTE ADD & PING WITH VARIABLES


I have a very noob issue on using variables on a ROUTE ADD syntax of a batch-file. In poor words I ask for inputting 2 value, then I use them to create 2 specific IP addresses and then I want to pass them to the ROUTE ADD syntax, but on my OS (Windows7 x64), when I run the script I can see correctly the IP01 and IP02 variables, but I cannot have the ROUTE ADD command to recognize IP01 and IP02... even I put %IP01% and %IP02% the command displayed writes nothing on the screen and so I cannot continue processing the batch file. Same problem also with PING command.

Here is the original code I've written:

@ECHO OFF
REM ******************************
REM ROUTE MAP for ENDIA-eWON
REM ******************************

CLS
ECHO.
COLOR 0A
ECHO.+ROUTE MAP 10.128.X.0 with eWON-sn + IP 172.31.1.X
ECHO.+PING ROUTE JUST DEFINED, TO CHECK IT
ECHO.----------------------------
ECHO.Author: aColussi@atomat.com
ECHO.v1.00
ECHO.
ECHO.
ECHO.
SET /P RGWsn=Insert eWON sn (RGWsn):
SET /P EndianIP=Insert last value X for the Endian network (172.30.1.X):

REM ******************************
REM DEFINE IP01 (eWON network) and IP02 (ENDIAN network)
REM ******************************

SET IP01 = 10.128.%RGWsn%.0
SET IP02 = 172.30.1.%EndianIP%

REM TEST THE 2 VALUES JUST DEFINED
SET IP01
SET IP02
PAUSE

@ECHO ON
REM ADD THE ROUTE
ROUTE ADD %IP01% MASK 255.255.255.0 %IP02% METRIC 1
PAUSE

@ECHO OFF
REM TEST THE ROUTE AND THE PING OF THE eWON ASSOCIATED DEVICE (.254)
ECHO.
ECHO.VIEW THE ROUTES ON OUR WORKSTATION
ROUTE PRINT |MORE
PAUSE
ECHO.
ECHO.PING-TEST FOR THE JUST-ADDED ROUTE/IP (USING eWON .254)
SET IP03 = 10.128.%RGWsn%.254
PING %IP03%
PAUSE

Solution

  • You have the problem when you are doing the SET. the SET for "RGWsn" and "EndianIP" is fine, but at other places, the spaces are causing the problem.

    SET IP01 = 10.128.%RGWsn%.0
    

    With this you are creating a variable "IP01 " and setting its value to " 10.128.%RGWsn%.0" Notice the spaces here.

    You should be doing

    SET IP01=10.128.%RGWsn%.0
    

    Similarly for IP02 and IP03.

    Take a look at this similar problem.