2014-10-17 39 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" 
}