代码之家  ›  专栏  ›  技术社区  ›  Furkan Gözükara

如何使用命令运行CMD命令

  •  -1
  • Furkan Gözükara  · 技术社区  · 10 年前

    好吧,我从来没有做过这样的编程,所以我真的需要帮助。

    假设我有以下ip列表

    ip子网掩码和默认网关

    23.22.11.162,255.255.255.240,23.22.11.161
    23.22.11.163,255.255.255.240,23.22.11.161
    23.22.11.164,255.255.255.240,23.22.11.161
    23.22.11.165,255.255.255.240,23.22.11.161
    23.22.11.166,255.255.255.240,23.22.11.161
    23.22.11.167,255.255.255.240,23.22.11.161
    23.22.11.168,255.255.255.240,23.22.11.161
    

    现在,以下cmd命令更改服务器ip

    int ip set address Ethernet static {0} {1} {2} 1
    

    {0}=新ip,{1}=子网掩码,{2}=默认网关

    我可以用C#程序轻松地完成这一任务,但我想在任务调度器中创建一个任务,该任务将按照此命令的顺序递归执行

    我的意思是,在第一次运行时,它将从ip索引0开始 第二次运行时,将在索引1处执行 在最后一个索引之后,它将再次从索引0开始

    我怎么能写这样的bat文件?

    非常感谢。

    我想一个案子会喜欢这项工作

    1: Define a list of these ips
    2: reads a TXT file to read index
    3: Then increase index 1 and execute command (need to split parameters and assign to command)
    Then write next index to that TXT file
    
    2 回复  |  直到 10 年前
        1
  •  2
  •   SachaDee    10 年前
    ::Counting the Number of  IP in the index to set the reset value
    
    for /f %%a in ('Find /V /C "" ^< Index.txt') do set $NbIP=%%a
    echo Number of IP in the index : [%$NBIP%]
    
    if not exist state.txt echo 0 >state.txt
    
    ::Getting the position in the index
    
    set /p $state=<state.txt
    echo Position in the index : [%$state: =%]
    call:StartIP %$state%
    set /a $state+=1
    
    ::writing the new state or reseting it if its value is equal to the total number of line in the index
    if %$state%==%$NBIP% (echo 0 >state.txt) else (echo %$state% >state.txt)
    
    exit/b
    
    :StartIP
    ::Setting the number of line to skip in the index
    if %1 equ 0 (set $skip="") else (set $skip=skip=%1)
    ::Getting the good line in the index and running the command
    for /f "%$skip%  tokens=1-3 delims=," %%a in (index.txt) do (
       echo int ip set address Ethernet static {%%a} {%%b} {%%c} 1
       exit/b)
    

    我打了一个 echo 在命令测试代码之前。如果输出正确,请将其拆下。 包含IP的文件是 Indext.txt

        2
  •  1
  •   Aacini    10 年前
    @echo off
    setlocal
    
    set "newIP="
    (for /F "tokens=1-3 delims=," %%a in (ipList.txt) do (
       if not defined newIP (
          set "newIP=%%a" & set "subnetmask=%%b" & set "defaultGateway=%%c"
       ) else (
          echo %%a,%%b,%%c
       )
    )) > newIPlist.txt
    echo %newIP%,%subnetmask%,%defaultGateway%>> newIPlist.txt
    del ipList.txt
    ren newIPlist.txt ipList.txt
    
    echo Processing: {%newIP%},  {%subnetmask%},  {%defaultGateway%}
    

    输出示例:

    C:\Users\Antonio\Documents> test
    Processing: {23.22.11.162},  {255.255.255.240},  {23.22.11.161}
    
    C:\Users\Antonio\Documents> test
    Processing: {23.22.11.163},  {255.255.255.240},  {23.22.11.161}
    
    C:\Users\Antonio\Documents> test
    Processing: {23.22.11.164},  {255.255.255.240},  {23.22.11.161}