运行 思科交换机上的命令

Run command on cisco switch

我制作了以下脚本来登录交换机并执行命令。一旦我执行脚本,它就会登录以切换并退出,而无需 运行 命令。

#!/usr/bin/expect -f

set timeout 3000

spawn bash -c "ssh ananair@10.60.255.100"

expect "*assword:"

send "pass@123\n"

expect "*#"

send "show interfaces status"

我怀疑问题出在你最后的 send 中缺少 \n。如果您只是 send "string",那么 expect 永远不会在命令末尾按 Enter,并且由于没有最终的 expect,它认为它的工作已经完成并退出,甚至可能在它有机会之前回显它发送但从未执行的命令。

考虑以下几点:

#!/usr/bin/expect -f

set timeout 3000

spawn ssh switchhostname          # no need to run this inside a shell

expect "ssword:"

send "1ns3cur3\n"                 # it would be better to use key based auth...

expect "#"

send "term len 0\n"               # tell the switch to avoid "More" prompts

expect "#"

send "show interfaces status\n"   # note the final "\n"

expect "#"                        # don't quit until the "show" command finishes

您也可以考虑通过 SNMP 访问此信息。

尝试使用 \r 而不是 \n

我有我的 expect 脚本,可用于为我登录到我的 cisco 交换机。然后我在 cisco 提示符下使用 expect 脚本进行交互。

我需要对其进行修改以不显示密码,但我绝对可以帮助您。

我不需要超时。

#!/usr/bin/expect
proc enable {} {
 expect "assword:" {
  send "<enable password>\r"
  expect "#" {
  }
 }
}

proc login {} {
 send "<login password>\r"
 expect {
  "failed" {
   send "<username>\r"
   enable
  }
  ">" {
   send "en\r"
   enable
  }
  "#" {
 }}
}
set user_computer_attached [lindex $argv 0]
set user_computer [split $user_computer_attached "@"]
spawn ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 \
-oGlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null \
-oStrictHostKeyChecking=no $user_computer_attached
expect_after eof {
  wait
  spawn telnet [lindex $user_computer 1]
  expect "name:"
  send [lindex $user_computer 0]
  send "\r"
  expect "assword:" {
   login
  }
}
expect "assword:" {
 login
}
interact