FOR循环比较列表中的用户输入并运行预定义命令

时间:2013-11-26 02:42:46

标签: batch-file for-loop

我正在使用.bat来促进我公司的telnet。它将获得用户输入,如果在预定义列表中匹配的用户输入的ip将携带一些命令,如果用户输入不匹配,则下面将运行另一组命令是我的批处理中有问题的命令区域;任何xtra信息需要请求它。谢谢先进...

:portinfo
cls
echo.
echo    ====================
echo    1. Switch VLan Menu:
echo    ====================
echo.
set /p ip=Access Switch IP : 
set /p uname=Uname : 
set /p passwd=Password : 
set /p faint=Interface : Fa0/ 
set /p vlan=Vlan to switch to : 

FOR LOOP STARTS HERE

for %ip% in ('10.195.30.5' '10.195.102.42' '10.195.102.109') do
(
echo %ip% 23> swvlandetail.txt
echo WAIT "Password:">> swvlandetail.txt
echo SEND "d0n07en7er\m">> swvlandetail.txt
echo WAIT ">">> swvlandetail.txt
echo SEND "en\m">> swvlandetail.txt
echo WAIT "Password:">> swvlandetail.txt
echo SEND "d0n07en7er\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "config t\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "int fa0/%faint%\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "switchport acc vlan %vlan%\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "no sh\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "end\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "q\m">> swvlandetail.txt
echo WAIT>> swvlandetail.txt
set /P confirm=Are you sure you want to continue[Y/N] or [U] to update you data ? 
if /I "%confirm%" EQU "Y" goto switchvlan
if /I "%confirm%" EQU "N" goto mainmenu
if /I "%confirm%" EQU "U" goto portinfo
)

FOR LOOP ENDS HERE

echo %ip% 23> swvlandetail.txt
echo WAIT "Username:">> swvlandetail.txt
echo SEND "%uname%\m">> swvlandetail.txt
echo WAIT "Password:">> swvlandetail.txt
echo SEND "%passwd%\m">> swvlandetail.txt
echo WAIT ">">> swvlandetail.txt
echo SEND "en\m">> swvlandetail.txt
echo WAIT "Password:">> swvlandetail.txt
echo SEND "%passwd%\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "config t\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "int fa0/%faint%\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "switchport acc vlan %vlan%\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "no sh\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "end\m">> swvlandetail.txt
echo WAIT "#">> swvlandetail.txt
echo SEND "q\m">> swvlandetail.txt
echo WAIT>> swvlandetail.txt
set /P confirm=Are you sure you want to continue[Y/N] or [U] to update you data ? 
if /I "%confirm%" EQU "Y" goto switchvlan
if /I "%confirm%" EQU "N" goto mainmenu
if /I "%confirm%" EQU "U" goto portinfo

:switchvlan
cls
echo Starting ...
Ping.exe localhost -n 2 >NUL
cls
echo.
tst10.exe /r:swvlandetail.txt
echo Returning to Main Menu...
Ping.exe localhost -n 2 >NUL
goto mainmenu

问题是: 有两种类型的接入交换机配置(CISCO)。一种类型的tacacs正在运行,另一种类型只会提示在交换机的running-config中预定义的telnet密码(这样只有本地支持才能访问接入交换机)。当用户想要为仅提示输入telnet密码的交换机配置交换机时。 telnet程序无法使用临时文件(swvlandetail.txt)将命令发送到telnet会话。它会挂起,因为它假设要等到提示符提示用户名:而不是密码:

解释
 1. .bat将帮助一级支持(对CISCO零知识):   - 在所需的FastEthernet端口上切换vlan(例如:Fa0 / 13)或
  - 重新启动所需的FastEthernet端口或
  - 禁用所需的FastEthernet端口

  1. 如果要配置的交换机未使用cisco tacacs服务(例如:10.195.30.5或10.195.102.42),则下面的命令应该正在运行:
  2. 代码应该正在运行:

    echo %ip% 23> swvlandetail.txt
    echo WAIT "Password:">> swvlandetail.txt
    echo SEND "d0n07en7er\m">> swvlandetail.txt
    echo WAIT ">">> swvlandetail.txt
    echo SEND "en\m">> swvlandetail.txt
    echo WAIT "Password:">> swvlandetail.txt
    echo SEND "d0n07en7er\m">> swvlandetail.txt
    echo WAIT "#">> swvlandetail.txt
    echo SEND "config t\m">> swvlandetail.txt
    echo WAIT "#">> swvlandetail.txt
    echo SEND "int fa0/%faint%\m">> swvlandetail.txt
    echo WAIT "#">> swvlandetail.txt
    echo SEND "switchport acc vlan %vlan%\m">> swvlandetail.txt
    echo WAIT "#">> swvlandetail.txt
    echo SEND "no sh\m">> swvlandetail.txt
    echo WAIT "#">> swvlandetail.txt
    echo SEND "end\m">> swvlandetail.txt
    echo WAIT "#">> swvlandetail.txt
    echo SEND "q\m">> swvlandetail.txt
    echo WAIT>> swvlandetail.txt
    set /P confirm=Are you sure you want to continue[Y/N] or [U] to update you data ? 
    if /I "%confirm%" EQU "Y" goto switchvlan
    if /I "%confirm%" EQU "N" goto mainmenu
    if /I "%confirm%" EQU "U" goto portinfo
    

    这将创建swvlandetail.txt,如下所示

    10.195.30.5 23
    WAIT "Password:\m"
    SEND "d0n07en7er\m"
    WAIT ">"
    SEND "en\m"
    WAIT "Password:"
    SEND "d0n07en7er\m"
    WAIT "#"
    SEND "config t\m"
    WAIT "#"
    SEND "int fa0/13\m"
    WAIT "#"
    SEND "switchport acc vlan 82\m"
    WAIT "#"
    SEND "no sh\m"
    WAIT "#"
    SEND "end\m"
    WAIT "#"
    SEND "q\m"
    WAIT
    
    1. 如果要配置的交换机是运行tacacs进行身份验证的普通交换机。下面的命令应该运行:
    2. 代码应该正在运行:

      echo %ip% 23> swvlandetail.txt
      echo WAIT "Username:">> swvlandetail.txt
      echo SEND "%uname%\m">> swvlandetail.txt
      echo WAIT "Password:">> swvlandetail.txt
      echo SEND "%passwd%\m">> swvlandetail.txt
      echo WAIT ">">> swvlandetail.txt
      echo SEND "en\m">> swvlandetail.txt
      echo WAIT "Password:">> swvlandetail.txt
      echo SEND "%passwd%\m">> swvlandetail.txt
      echo WAIT "#">> swvlandetail.txt
      echo SEND "config t\m">> swvlandetail.txt
      echo WAIT "#">> swvlandetail.txt
      echo SEND "int fa0/%faint%\m">> swvlandetail.txt
      echo WAIT "#">> swvlandetail.txt
      echo SEND "switchport acc vlan %vlan%\m">> swvlandetail.txt
      echo WAIT "#">> swvlandetail.txt
      echo SEND "no sh\m">> swvlandetail.txt
      echo WAIT "#">> swvlandetail.txt
      echo SEND "end\m">> swvlandetail.txt
      echo WAIT "#">> swvlandetail.txt
      echo SEND "q\m">> swvlandetail.txt
      echo WAIT>> swvlandetail.txt
      set /P confirm=Are you sure you want to continue[Y/N] or [U] to update you data ? 
      if /I "%confirm%" EQU "Y" goto switchvlan
      if /I "%confirm%" EQU "N" goto mainmenu
      if /I "%confirm%" EQU "U" goto portinfo
      

      将创建另一个swvlandetail.txt外观,如下所示:

      10.195.102.100 23
      WAIT "Username:"
      SEND "aaasaud\m"
      WAIT "Password:"
      SEND "n3ssus_m4n1\m"
      WAIT ">"
      SEND "en\m"
      WAIT "Password:"
      SEND "n3ssus_m4n1\m"
      WAIT "#"
      SEND "config t\m"
      WAIT "#"
      SEND "int fa0/15\m"
      WAIT "#"
      SEND "switchport acc vlan 89\m"
      WAIT "#"
      SEND "no sh\m"
      WAIT "#"
      SEND "end\m"
      WAIT "#"
      SEND "q\m"
      WAIT
      

      将会有另一个程序启动telnet会话并将swvlandetail.txt中的所有内容输入到该telnet.exe 将启动telnet.exe的部分如下:

      :switchvlan
      cls
      echo Starting ...
      Ping.exe localhost -n 2 >NUL
      cls
      echo.
      tst10.exe /r:swvlandetail.txt
      echo Returning to Main Menu...
      Ping.exe localhost -n 2 >NUL
      goto mainmenu
      

3 个答案:

答案 0 :(得分:1)

对不起。这是有太多信息的问题之一,但没有明确解释这一点。我从你的问题中提取了这些短语:

  • “如果在预定义列表中匹配的用户输入的ip将携带一些命令,如果用户输入不匹配则会运行另一组命令”
  • 如果用户输入匹配列表中的ip的ip,我希望程序继续创建临时文件(swvlandetail.txt)“

此代码段实现了该选择:

setlocal EnableDelayedExpansion

set predefinedList=/10.195.30.5/10.195.102.42/10.195.102.109/

set /p ip=Access Switch IP : 
if "!predefinedList:/%ip%/=!" neq "%predefinedList%" (
   rem The given ip IS in the list:
   rem Continue create the temp files
   rem and carry some commands
) else (
   rem Run another set of command
)

如果这不是您想要的,请清楚说明......

答案 1 :(得分:0)

你的问题在于你的for循环。您不能在for循环中使用%ip%变量 - for %ip% in ('10.195.30.5' '10.195.102.42') do

正确的语法是for %%A in (10.195.30.5 10.195.102.42) do,然后您可以将ip设置为%%A

然而,您似乎尝试使用变量ip两次,为什么要在此处设置它?set /p ip=Access Switch IP : - 但是然后循环遍历两个完全独立的IP地址?

如果ip变量的双重赋值是偶然的,那么您只需将访问开关IP地址的初始声明更改为AccessIP

答案 2 :(得分:0)

这里有很多代码有很多错误,但没有太多关于所需内容的详细信息。您似乎假设每个人都熟悉您要发出的命令应该

首先,问题是FOR循环的元变量可能只是一个字母。一般语法是

for %%x in (list of items) do command

所以,如果%ip%循环中有FOR,则需要替换%%xx可以是您喜欢的任何字母,但 IS 区分大小写)

接下来,列表中的项目是 LITERALLY ,因此,如果您'quote',则替换元变量的项目将为'quoted'。我们没有关于这是否正确的信息。

接下来,在复合语句中,左括号必须DO位于同一物理行上。

for %%x in (item1 item2) do (
 echo %%x
 echo I repeat - %%x
)

我们不知道%vlan%%faint%来自哪里。批量将替换循环中任何%variable%的变量值 - 替换值将是变量的值时间FOR循环已分时 - 不是因为循环操作而改变了。这同样适用于%confirm% - 在启动FOR循环时设置该值。 (请参阅此处有关DELAYED EXPANSION的任何数量的帖子,了解解决此问题的方法)

下一个问题是echo ...> filename将在每个循环上重新启动文件,因此退出时的内容将是最后一次迭代设置的内容。

真的,不知道\m对你意味着什么。该文本将在目标文本文件中完全复制为 \ m - 如果符合您的预期,它将不会被转换为控制字符。