Help with a bat or cmd script

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
sunhuxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunhuxAuthor 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>
0
sunhuxAuthor Commented:
Typo correction:

command 1:  check what's the current default gateway IP, save it in a variable for commands 2 & 3
0
Bill PrewCommented:
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
1
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Bill PrewCommented:
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
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunhuxAuthor 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!]
0
Bill PrewCommented:
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
1
sunhuxAuthor 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) ?
0
Bill PrewCommented:
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
1
sunhuxAuthor 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 ?
0
Bill PrewCommented:
What does it echo?


»bp
0
sunhuxAuthor 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>
0
sunhuxAuthor Commented:
Ahh, looks like the line
     echo %gateway%    
should read
     echo !gateway!
0
Bill PrewCommented:
Yes, that's correct.


»bp
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.