-1

这是我的自动化脚本片段,用于从基于 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
4

1 回答 1

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
于 2014-07-31T12:13:42.073 回答