From b73f299e731fbddae095c0b5eff04717b6dce1af Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 13 Feb 2008 20:34:57 +0000 Subject: Imported from rancid-2.3.2a8.tar.gz. --- bin/hlogin.in | 184 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 96 insertions(+), 88 deletions(-) (limited to 'bin/hlogin.in') diff --git a/bin/hlogin.in b/bin/hlogin.in index 8777021..1a7722c 100644 --- a/bin/hlogin.in +++ b/bin/hlogin.in @@ -1,27 +1,47 @@ #! @EXPECT_PATH@ -- ## -## $Id: hlogin.in,v 1.40 2006/12/08 21:28:26 heas Exp $ +## $Id$ ## ## @PACKAGE@ @VERSION@ -## Copyright (C) 1997-2006 by Terrapin Communications, Inc. +## Copyright (c) 1997-2007 by Terrapin Communications, Inc. ## All rights reserved. ## -## This software may be freely copied, modified and redistributed -## without fee for non-commerical purposes provided that this license -## remains intact and unmodified with any RANCID distribution. +## This code is derived from software contributed to and maintained by +## Terrapin Communications, Inc. by Henry Kilmer, John Heasley, Andrew Partan, +## Pete Whiting, Austin Schutz, and Andrew Fort. ## -## There is no warranty or other guarantee of fitness of this software. -## It is provided solely "as is". The author(s) disclaim(s) all -## responsibility and liability with respect to this software's usage -## or its effect upon hardware, computer systems, other software, or -## anything else. +## Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions +## are met: +## 1. Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## 2. Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in the +## documentation and/or other materials provided with the distribution. +## 3. All advertising materials mentioning features or use of this software +## must display the following acknowledgement: +## This product includes software developed by Terrapin Communications, +## Inc. and its contributors for RANCID. +## 4. Neither the name of Terrapin Communications, Inc. nor the names of its +## contributors may be used to endorse or promote products derived from +## this software without specific prior written permission. +## 5. It is requested that non-binding fixes and modifications be contributed +## back to Terrapin Communications, Inc. ## -## Except where noted otherwise, rancid was written by and is maintained by -## Henry Kilmer, John Heasley, Andrew Partan, Pete Whiting, and Austin Schutz. -## -# -# The login expect scripts were based on Erik Sherk's gwtn, by permission. -# +## THIS SOFTWARE IS PROVIDED BY Terrapin Communications, INC. AND CONTRIBUTORS +## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS +## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +## POSSIBILITY OF SUCH DAMAGE. +# +# The expect login scripts were based on Erik Sherk's gwtn, by permission. +# # hlogin - hp login # # Most options are intuitive for logging into a Cisco router. @@ -33,7 +53,7 @@ # # Usage line -set usage "Usage: $argv0 \[-autoenable\] \[-noenable\] \[-c command\] \ +set usage "Usage: $argv0 \[-dV\] \[-autoenable\] \[-noenable\] \[-c command\] \ \[-Evar=x\] \[-e enable-password\] \[-f cloginrc-file\] \[-p user-password\] \ \[-s script-file\] \[-t timeout\] \[-u username\] \ \[-v vty-password\] \[-w enable-username\] \[-x command-file\] \ @@ -90,8 +110,11 @@ for {set i 0} {$i < $argc} {incr i} { set arg [lindex $argv $i] switch -glob -- $arg { + # Expect debug mode + -d* { + exp_internal 1 # Username - -u* - + } -u* - -U* { if {! [ regexp .\[uU\](.+) $arg ignore user]} { incr i @@ -343,7 +366,7 @@ proc login { router user userpswd passwd enapasswd cmethod cyphertype } { expect_after { timeout { send_user "\nError: TIMEOUT reached\n" - catch {close}; wait + catch {close}; catch {wait}; if { $in_proc} { return 1 } else { @@ -351,7 +374,7 @@ proc login { router user userpswd passwd enapasswd cmethod cyphertype } { } } eof { send_user "\nError: EOF received\n" - catch {close}; wait + catch {close}; catch {wait}; if { $in_proc} { return 1 } else { @@ -373,17 +396,17 @@ proc login { router user userpswd passwd enapasswd cmethod cyphertype } { exp_continue } -re "(Connection refused|Secure connection \[^\n\r]+ refused|Connection closed by)" { - catch {close}; wait + catch {close}; catch {wait}; if !$progs { send_user "\nError: Connection Refused ($prog)\n"; return 1 } } "Host is unreachable" { - catch {close}; + catch {close}; catch {wait}; send_user "\nError: Host Unreachable!\n"; wait; return 1 } "No address associated with name" { - catch {close}; + catch {close}; catch {wait}; send_user "\nError: Unknown host\n"; wait; return 1 } -re "(Host key not found |The authenticity of host .* be established).*\(yes\/no\)\?" { @@ -400,13 +423,15 @@ proc login { router user userpswd passwd enapasswd cmethod cyphertype } { return 1 } eof { send_user "\nError: Couldn't login\n"; wait; return 1 } -nocase "unknown host\r" { - catch {close}; + catch {close}; catch {wait}; send_user "\nError: Unknown host\n"; wait; return 1 } -re "$u_prompt" { send "$user\r" expect { eof { send_user "\nError: Couldn't login\n"; wait; return 1 } - "Login invalid" { send_user "\nError: Invalid login\n"; vatch {close}; wait; return 1 } + "Login invalid" { send_user "\nError: Invalid login\n"; + catch {close}; catch {wait}; + return 1 } -re "$p_prompt" { send "$userpswd\r" } "$prompt" { set in_proc 0; return 0 } "Press any key to continue" { @@ -440,7 +465,7 @@ proc login { router user userpswd passwd enapasswd cmethod cyphertype } { } "$prompt" { break; } denied { send_user "\nError: Check your passwd for $router\n" - catch {close}; wait; return 1 + catch {close}; catch {wait}; return 1 } "% Bad passwords" {send_user "\nError: Check your passwd for $router\n"; return 1 } } @@ -480,7 +505,8 @@ proc run_commands { prompt command } { # Turn off the pager and escape regex meta characters in the $prompt send "no page\r" - regsub -all "\[)(]" $prompt {\\&} reprompt + regsub -all {[)(]} $prompt {\\&} reprompt + regsub -all {^(.{1,11}).*([#>])$} $reprompt {\1([^#>\r\n]+)?[#>](\\([^)\\r\\n]+\\))?} reprompt expect { -re $reprompt {} -re "\[\n\r]+" { exp_continue } @@ -488,63 +514,41 @@ proc run_commands { prompt command } { # this is the only way i see to get rid of more prompts in o/p..grrrrr log_user 0 # Is this a multi-command? - if [ string match "*\;*" "$command" ] { - set commands [split $command \;] - set num_commands [llength $commands] - # the pager can not be turned off on the PIX, so we have to look - # for the "More" prompt. the extreme is equally obnoxious, with a - # global switch in the config. - for {set i 0} {$i < $num_commands} { incr i} { - send "[subst -nocommands [lindex $commands $i]]\r" - expect { - -re "^\[^\n\r *]*$reprompt" { send_user -- "$expect_out(buffer)" - } - -re "^\[^\n\r]*$reprompt." { send_user -- "$expect_out(buffer)" - exp_continue } - -re "\[\n\r]+" { send_user -- "$expect_out(buffer)" - exp_continue } - -re "\[^\r\n]*Press to cont\[^\r\n]*" { send " " - expect { - # gag, 2 more prompts - -re "\[\r\n]*\r" {} - -re "\[^\r\n]*Press to cont\[^\r\n]*" { send " "; exp_continue } - } - exp_continue - } - -re "^<-+ More -+>\[^\n\r]*" { send " " - exp_continue } - -re "^-+ MORE -+\[^\n\r]*" { send " " - exp_continue } - -re "^-+More-+\[^\n\r]*" { send " " - exp_continue } - -re "\b+" { exp_continue } - } - } - } else { - # the pager can not be turned off on the PIX, so we have to look - # for the "More" prompt. the extreme is equally obnoxious, with a - # global switch in the config. - send "[subst -nocommands $command]\r" + set commands [split $command \;] + set num_commands [llength $commands] + # if the pager can not be turned off, we have to look for the "More" + # prompt. + for {set i 0} {$i < $num_commands} { incr i} { + send "[subst -nocommands [lindex $commands $i]]\r" expect { - -re "^\[^\n\r *]*$reprompt" { send_user -- "$expect_out(buffer)" - } - -re "^\[^\n\r]*$reprompt." { send_user -- "$expect_out(buffer)" - exp_continue } - -re "\[\n\r]+" { send_user -- "$expect_out(buffer)" - exp_continue } - -re "\[^\r\n]*Press to cont\[^\r\n]*" { send " " - expect { - -re "\[\r\n]*\r\r" {} - } - exp_continue - } - -re "^<-+ More -+>\[^\n\r]*" { send " " - exp_continue } - -re "^-+ MORE -+\[^\n\r]*" { send " " - exp_continue } - -re "^-+More-+\[^\n\r]*" { send " " - exp_continue } - -re "\b+" { exp_continue } + -re "^\[^\n\r *]*$reprompt" { send_user -- "$expect_out(buffer)" } + -re "^\[^\n\r]*$reprompt." { send_user -- "$expect_out(buffer)" + exp_continue } + -re "\[\n\r]+" { send_user -- "$expect_out(buffer)" + exp_continue } + -re "\[^\r\n]*Press to cont\[^\r\n]*" { + send " " + expect { + # gag, 2 more prompts + -re "\[\r\n]*\r" {} + -re "\[^\r\n]*Press to cont\[^\r\n]*" { send " "; exp_continue } + } + exp_continue + } + -re "^<-+ More -+>\[^\n\r]*" { send " " + exp_continue } + -re "^-+ MORE -+\[^\n\r]*" { send " " + exp_continue } + # 3 flavours of the more prompt, first -More-, then --More-- (for + # cisco/riverhead AGM), then with more dashes. + -re "^-More-\[^\n\r-]*" { send " " + exp_continue } + -re "^--More--\[^\n\r-]*" { send " " + exp_continue } + -re "^---+More---+\[^\n\r]*" { + send " " + exp_continue } + -re "\b+" { exp_continue } } } log_user 1 @@ -567,7 +571,7 @@ proc run_commands { prompt command } { send -h "exit\r" exp_continue } - timeout { catch {close}; wait + timeout { catch {close}; catch {wait}; return 0 } eof { return 0 } @@ -580,6 +584,7 @@ proc run_commands { prompt command } { # source_password_file $password_file set in_proc 0 +set exitval 0 foreach router [lrange $argv $i end] { set router [string tolower $router] send_user "$router\n" @@ -700,12 +705,14 @@ foreach router [lrange $argv $i end] { # Login to the router if {[login $router $ruser $userpswd $passwd $enapasswd $cmethod $cyphertype]} { + incr exitval continue } if { $enable } { if {[do_enable $enauser $enapasswd]} { if { $do_command || $do_script } { - catch {close}; catch {wait} + incr exitval + catch {close}; catch {wait}; continue } } @@ -719,6 +726,7 @@ foreach router [lrange $argv $i end] { if { $do_command } { if {[run_commands $prompt $command]} { + incr exitval continue } } elseif { $do_script } { @@ -726,7 +734,7 @@ foreach router [lrange $argv $i end] { send "no page\r" expect -re $prompt {} source $sfile - close + catch {close}; } else { label $router log_user 1 @@ -734,7 +742,7 @@ foreach router [lrange $argv $i end] { } # End of for each router - wait + catch {wait}; sleep 0.3 } -exit 0 +exit $exitval -- cgit