2014-10-17 42 views
0

我是一个新手期望(很多的shell编程虽然)。 我试图连接到交换机使用期望能够转储它的运行配置。但不知何故我的发送命令似乎没有发送到交换机。使用SSH和期望连接到交换机

脚本:

#!/usr/bin/expect -f 

proc connect {passw} { 
    expect { 
    "assword:" { 
     send "$passw\r" 
     expect { 
      "Prompt>" { 
      return 0 
      } 
     } 
    } 
    } 
    # timed out 
    return 1 
} 

set user [lindex $argv 0] 
set passw [lindex $argv 1] 
set host [lindex $argv 2] 

#check if all were provided 
if { $user == "" || $passw == "" || $host == "" } { 
    puts "Usage: <user> <passw> <host>\n" 
    exit 1 
} 

spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no [email protected]$host 
set rez [connect $passw] 
if { $rez == 0 } { 
    send {enable\r} 
    send {show running\r} 
    exit 0 
} 
puts "\nError connecting to switch: $host, user: $user!\n" 
exit 1 

唉,当我运行该脚本(从shell脚本触发),我可以看到它登录到交换机上,看到了“提示”,但在此之后脚本似乎已退出,因为我没有看到正在执行的'enable'和'show running'命令。

任何人都可以告诉我如何解决这个问题?

Richard。

+0

不是一个答案(因此评论),但你可能想看看如何腐臭(http://www.shrubbery.net/rancid/)做到这一点。 – abligh 2014-10-17 12:24:59

回答

1

您需要在使用send发送命令后再添加expect语句。使用send

spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no [email protected]$host 
set rez [connect $passw] 

set prompt "#" 

if { $rez == 0 } { 
    send {enable\r} 
    expect $prompt; # Waiting for 'prompt' to appear 
    send {show running\r} 
    expect $prompt; # Waiting for the prompt to appear 
    exit 0 
} 

没有expect命令后,然后期望无关从SSH过程指望它会简单的发送中这就是为什么你不能得到任何输出更快的方法的命令。

还有一个更新:

您正在使用的登录目的的单独proc。如果您正在使用单独的proc,则建议您传递产卵手柄。否则,剧本可能会失败。原因是,它不会从进程发送/期望,而是从stdin发送/期望。所以,你应该用它像这样,

proc connect {passw handle} { 
    expect { 
    -i $handle 
    "assword:" { 
     send "$passw\r" 
     expect { 
      -i $handle 
      "Prompt>" { 
      return 0 
      } 
     } 
    } 
    } 
    # timed out 
    return 1 
} 

set handle [ spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no [email protected]$host ] 
set rez [connect $passw $handle ] 
set prompt "#" 
if { $rez == 0 } { 
    send -i $handle "enable\r" 
    expect -i $handle $prompt 
    send -i $handle "show running\r" 
    expect -i $handle $prompt 
    exit 0 
} 

我们的SSH产卵手柄保存到变量'handle',它正在与sendexpect和标志-i使用是用来做他们等待相应的过程

+0

spawn_id是全局的,期望使用不同的范围机制,然后Tcl:你不应该明确地将spawn_id传递给过程。 – 2014-10-17 13:42:16

+0

是的格伦。正确。我这样做是为了避免任何可能的问题,比如在脚本中使用多个spawn – Dinesh 2014-10-17 13:44:27

0

@Dinesh有主要答案。一对夫妇的其他注意事项:

  • send {enable\r} - 因为你使用大括号,而不是报价,您发送的字符\[R,而不是一个回车。
  • 你从connect返回0和1就像你是一个shell程序员(不是侮辱,我也是)。改用Tcl的布尔值。

快速改写:

#!/usr/bin/expect -f 

set prompt "Prompt>" 
proc connect {passw} { 
    expect "assword:" 
    send "$passw\r" 
    expect { 
     $::prompt {return true} 
     timeout {return false} 
    } 
} 

lassign $argv user passw host 

#check if all were provided 
if { $user == "" || $passw == "" || $host == "" } { 
    puts "Usage: [info script] <user> <passw> <host>" 
    exit 1 
} 

spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no [email protected]$host 
if {[connect $passw]} { 
    send "enable\r" 
    expect $::prompt 
    send "show running\r" 
    expect $::prompt 
    send "exit\r"   # neatly close the session 
    expect eof 
} else { 
    puts "\nError connecting to switch: $host, user: $user!\n" 
}