sunhux
asked on
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
& 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
ASKER
Typo correction:
command 1: check what's the current default gateway IP, save it in a variable for commands 2 & 3
command 1: check what's the current default gateway IP, save it in a variable for commands 2 & 3
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!]
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!]
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
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
ASKER
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) ?
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) ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ?
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 ?
What does it echo?
»bp
»bp
ASKER
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>
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>
ASKER
Ahh, looks like the line
echo %gateway%
should read
echo !gateway!
echo %gateway%
should read
echo !gateway!
Yes, that's correct.
»bp
»bp
ASKER
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_wa