Help with a bat or cmd script

sunhux
sunhux used Ask the Experts™
on
I need a batch (ie .bat or .cmd) logon script that will check if the hostname is one of the 20
& if so, then only execute a series of commands on the PC.  The pseudo code will be like:

if %computername%="host1"  or ... or %computername%="host20"
then
   (
    command 1
    command 2
   )

Basically I need to get the syntax right
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Sorry, need more help with the script ie command 1, command 2, command 3:

command 1:
check what's the current default gateway IP, save it in a variable commands 2 & 3:
   route del ...    <remove the default gateway ie this is command 2>
   route add 10.6.5.0 mask 255.255.255.0 default_gateway_IP_that_was_saved    <this is command 3>

Author

Commented:
Typo correction:

command 1:  check what's the current default gateway IP, save it in a variable for commands 2 & 3
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
Here's a simple and efficient approach.  Create a SET for each host name you want to match on and then test for that to be defined in the IF statement.  I like to place something in front of the "data" in SET commands like this, in this case I used an underscore.  That way if there is already an environment variable of that computer name before the script runs it doesn't get confused.  The underscore can be longer, and anything you want, just adjust the IF DEFINED to match it.

Using the SETLOCAL at the top of the script assures that any SET commands done in the script do not persist beyond the life of the script.

@echo off
setlocal

set _host1=1
set _host2=1
set _host3=1
set _host4=1

if defined _%computername% (
    echo %computername%
)

Open in new window


»bp
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

IT / Software Engineering Consultant
Top Expert 2016
Commented:
This gets the gateway as you added.  Make sure you the setlocal EnableDelayedExpansion, since you want to update and access a variable inside a loop or if block.

@echo off
setlocal EnableDelayedExpansion

set _host1=1
set _host2=1
set _host3=1
set _host4=1
set _bp=1

if defined _%computername% (
    for /f "tokens=1* delims=:" %%A in ('ipconfig ^| findstr /i "Gateway"') do set gateway=%%B
    if "!gateway:~0,1!" EQU " " set gateway=!gateway:~1!
    echo gateway=[!gateway!]
    rem Your commands here referencing !gateway!
)

Open in new window


»bp

Author

Commented:
I'll test them tomorrow;  would like to clarify on  "rem your commands ... ".
So just enter it as  [!gateway!]  as in below?

  route del 0.0.0.0 mask 0.0.0.0 [!gateway!]
  route add 10.6.5.0 mask 255.255.255.0  [!gateway!]
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Yes, but without the [ ], I put those in just for the ECHO command (I like to do that so I can make sure there are no leading or trailing spaces in the variable value.  So in your case:

 route del 0.0.0.0 mask 0.0.0.0 !gateway!
  route add 10.6.5.0 mask 255.255.255.0  !gateway!



»bp

Author

Commented:
2 more queries:

Say the PC hostname is  PC12345, the line
  set _host1=1
should read as
  set _host1="PC12345"    Or
  set _host1=PC12345       (ie without the double quotes) ?

Likewise, if that PC's IP address is 10.3.2.3,  should that line read as
  set _host1="10.3.2.3"    Or
  set _host1=10.3.2.3       (ie without the double quotes) ?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
None of those, the hostnames in my example were host1, host2, host3, and host4.

So in your case, to define that hostname you would do:

set _PC12345=1

You would not set the IP address like this though, because we are comparing these values to %COMPUTERNAME%, which it the name of the computer, not its IP address.


»bp

Author

Commented:
Tested running the script & it gave the output below:
  gateway=10.3.200.1
  ECHO is off.


What I've updated in the script is as follows:
@echo off
setlocal EnableDelayedExpansion

set _PC01059=1
set _PC01057=1
set _host3=1
set _host4=1
set _bp=1

if defined _%computername% (
    for /f "tokens=1* delims=:" %%A in ('ipconfig ^| findstr /i "Gateway"') do set gateway=%%B
    if "!gateway:~0,1!" EQU " " set gateway=!gateway:~1!
    echo gateway=!gateway!
    echo %gateway%
    rem Your commands here referencing !gateway!
)


Why does the echo %gateway%  not echoing out the value of gateway ie 10.3.200.1 ?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
What does it echo?


»bp

Author

Commented:
I think that echo line outputs  the following :
  ECHO is off.


I remarked the 1st line & this is how it looks like:

C:\putty>REM @echo off

C:\putty>setlocal EnableDelayedExpansion

C:\putty>set _LTNBD01059=1

C:\putty>set _host2=1

C:\putty>set _host3=1

C:\putty>set _host4=1

C:\putty>set _host5=1

C:\putty>if defined _LTNBD01059 (
for /F "tokens=1* delims=:" %A in ('ipconfig | findstr /i "Gateway"') do set gateway=%B
 if "!gateway:~0,1!" EQU " " set gateway=!gateway:~1!
 echo gateway=!gateway!
 echo
 rem Your commands here referencing !gateway!
)

C:\putty>set gateway= 10.3.200.1
gateway=10.3.200.1
ECHO is on.

C:\putty>

Author

Commented:
Ahh, looks like the line
     echo %gateway%    
should read
     echo !gateway!
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Yes, that's correct.


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial