如何从TCL脚本运行多个实例

时间:2018-09-17 05:26:34

标签: multithreading tcl fork

我有一个tcl脚本。 在那我创建了3 telnet会话到服务器。但是,我想同时连续地对所有telnet运行命令集。实现它的最佳方法是什么。

For Ex: For 1st Telent session : i run "show ospf" 
        For 2nd Telent session : i run "show interfaces"
        For 3rd Telent session : i run "show eigrp"

我同时连续执行上述命令。直到指定的计数。

1 个答案:

答案 0 :(得分:0)

这是我们很久以前使用的一些代码。

set commands [ list ]
lappend commands "no ip prefix-list DDOS permit $c_targetip/24"    
lappend commands "ip prefix-list DDOS seq $data permit $c_targetip/24"

lappend commands "router bgp"
lappend commands "network $c_targetip/24"
lappend commands "exit"

lappend allconnections "br2.sjc1"
lappend allconnections "br3.lax3"
lappend allconnections "br6.lax10"
lappend allconnections "br7.lax10"

# spawn all connections
foreach conn $allconnections {

    spawn telnet $conn
    lappend spawn_id_list $spawn_id

  }

  # run expect script for all connections individually
  foreach id $spawn_id_list {

    # this is important - for unknown (to me) reasons
    set spawn_id $id
    set timeout 30

    expect_after {
      timeout { catch { close }; wait; return 2 }
      eof { catch { close }; wait; return 1 }
    }

    expect {
      "Username: " {
        send "$user\r"
        exp_continue
      }
      "Password: " {
        send "$password\r"
        exp_continue
      }
      "#" {
        send "configure terminal\r" 
      }
    }

    expect "(config)#" 
    foreach command $commands {  
      send "$command\r"
      expect ")#"
    }

    send "exit\r"
    expect "#"
    send "exit\r"
    expect ">"
    send "exit\r"
    expect "closed"

    exp_close
    exp_wait

  }




  return 0;
}

`

相关问题