这是我的自动化脚本片段,用于基于vlan从特定服务器进行映像。该脚本检查其IP及其所在的vlan然后使用它连接到特定服务器。我的问题是我得到了“如果此时是意外的”错误。请帮忙。
:getvlan
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IPv4 Address"') do set IP=%%a
for /f "tokens=3 delims=." %%j in ('echo %IP%') do set vlan=%%j
if %vlan% GEQ 100 (if %vlan% LEQ 119 (
set servernowack=server1
set seshtype=M
) if %vlan% GEQ 130 (if %vlan% LEQ 139 (
set servernowack=server2
set seshtype=D
) if %vlan% GEQ 140 (if %vlan% LEQ 149 (
set servernowack=server3
set seshtype=D
) if %vlan% GEQ 150 (if %vlan% LEQ 159 (
set servernowack=server4
set seshtype=M
) if %vlan% GEQ 160 (if %vlan% LEQ 169 (
set servernowack=server5
set seshtype=D
) else (
goto fail
)
goto getserverip
答案 0 :(得分:0)
还有比关闭括号更多的开场白。因此,cmd.exe
无法确定批处理代码中每个if
的块,这是一个容易解决的简单语法错误。
@echo off
:getvlan
for /f "tokens=2 delims=:" %%a in ('%windir%\system32\ipconfig.exe ^| %windir%\system32\find.exe "IPv4 Address"') do set IP=%%a
for /f "tokens=3 delims=." %%j in ('echo %IP%') do set vlan=%%j
if %vlan% GEQ 100 (
if %vlan% LEQ 119 (
set servernowack=server1
set seshtype=M
goto getserverip
)
)
if %vlan% GEQ 130 (
if %vlan% LEQ 139 (
set servernowack=server2
set seshtype=D
goto getserverip
)
)
if %vlan% GEQ 140 (
if %vlan% LEQ 149 (
set servernowack=server3
set seshtype=D
goto getserverip
)
)
if %vlan% GEQ 150 (
if %vlan% LEQ 159 (
set servernowack=server4
set seshtype=M
goto getserverip
)
)
if %vlan% GEQ 160 (
if %vlan% LEQ 169 (
set servernowack=server5
set seshtype=D
goto getserverip
)
)
echo Error: Failed to determine vlan!
goto :EOF
:getserverip