diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-01-18 12:36:18 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-01-18 12:36:18 +0200 |
commit | 7c1d8f6ca246cf4a2ab1a4625d5cce659cce87ea (patch) | |
tree | ed017d5bc51b331cc535c588acc5af76475fd5f0 /rex | |
parent | 183bfad128343a5762495c531fb4a4e40abba510 (diff) | |
download | rex-7c1d8f6ca246cf4a2ab1a4625d5cce659cce87ea.tar.gz rex-7c1d8f6ca246cf4a2ab1a4625d5cce659cce87ea.tar.bz2 |
Improve timeout and error handling.
* rex (terror): New function. Use it to report errors.
(runcp,runcprev): Do not exit on errors.
(runcmd): Handle "*\r" and "*\n" in the final expect to
reset timeout.
(main): Report errors at the end of the run. Return 2
to the shell if they occurred.
Diffstat (limited to 'rex')
-rwxr-xr-x | rex | 131 |
1 files changed, 89 insertions, 42 deletions
@@ -103,6 +103,17 @@ proc debug {args} { } } +proc terror {args} { + global argv0 errors + + if ![info exist errors] { + set errors {} + } + set msg [join $args] + set errors [linsert $errors end $msg] + send_error "$argv0: $msg\n" +} + # Compare two version numbers. Return 0 if they are the same, a negative # value if "a" is older than "b", and a positive value otherwise. proc vercmp {va vb} { @@ -850,7 +861,7 @@ if [info exists config(option,hostgroup)] { if [info exists config(script)] { set name [scanlibpath $config(script).rex] if {$name == ""} { - puts stderr "$argv0: the script $config(script).rex not found" + terror "the script $config(script).rex not found" exit 1 } debug 2 "reading script file $name" @@ -883,7 +894,7 @@ if [config_option noop] { debug 2 "command mode (from script)" } elseif [config_option copy] { if { $argc < 2 } { - puts stderr "$argv0: too few arguments" + terror "too few arguments" exit 1 } debug 2 "copy mode" @@ -935,7 +946,7 @@ if {![config_option copy]} { } if {![info exists config(hosts)] || [llength $config(hosts)] == 0} { - puts stderr "$argv0: no host list" + terror "no host list" listgroups exit 1 } @@ -953,12 +964,12 @@ if {![config_option copy] && spawn -noecho ssh [hostuser $host]@$host expect { denied { - send_error "Bad password\n" + terror "bad password for [hostuser $host]@$host" exit 1 } "*assword:" { if $password_sent { - send_error "Bad password\n" + terror "bad password for [hostuser $host]@$host" exit 1 } else { send "[hostpass $host]\r" @@ -967,12 +978,12 @@ if {![config_option copy] && } } timeout { - send_error "Connection with $host timed out\n" + terror "connection to $host timed out" exit 1 } eof { - send_error "connection to $host failed: $expect_out(buffer)\n" - exit + terror "connection to $host failed: $expect_out(buffer)" + exit 1 } "Are you sure you want to continue connecting (yes/no)?" { send "yes\r" @@ -998,7 +1009,7 @@ if {![config_option copy] && } proc runcp {args} { - global config + global config argv0 set password_sent 0 set host [lindex $args 0] set retry_count 0 @@ -1010,13 +1021,15 @@ proc runcp {args} { eval spawn -noecho scp $cmd expect { denied { - send_error "Bad password\n" - exit 1 + terror "bad password for user [hostuser $host] on $host" + catch { close } + return } "*assword:" { if $password_sent { - send_error "Bad password\n" - exit 1 + terror "bad password for user [hostuser $host] on $host" + catch { close } + return } else { send "[hostpass $host]\r" incr password_sent @@ -1024,8 +1037,9 @@ proc runcp {args} { } } timeout { - send_error "Connection with $host timed out\n" - exit 1 + terror "connection to $host timed out" + catch { close } + return } eof } @@ -1034,23 +1048,25 @@ proc runcp {args} { if {[lindex $res 3] != 0} { incr retry_count if {$retry_count > $config(max_scp_retry_count)} { - send_error "failed to copy file to $host\n" - exit 1 + terror "failed to copy file to $host" + catch { close } + return } - send_error "Retrying connection to $host\n" + send_error "$argv0: retrying connection to $host\n" set retry 1 set password_sent 0 } } else { - send_error "Operating system error while copying script file to $host\n" - exit 1 + terror "operating system error while copying file to $host" + catch { close } + return } catch { close } } } proc runcprev {args} { - global config + global config argv0 set password_sent 0 set host [lindex $args 0] set source [lindex $args 1] @@ -1065,13 +1081,15 @@ proc runcprev {args} { spawn -noecho scp [hostuser $host]@$host:$source $dest expect { denied { - send_error "Bad password\n" - exit 1 + terror "bad password for [hostuser $host] on $host" + catch { close } + return } "*assword:" { if $password_sent { - send_error "Bad password\n" - exit 1 + terror "bad password for [hostuser $host] on $host" + catch { close } + return } else { send "[hostpass $host]\r" incr password_sent @@ -1086,8 +1104,9 @@ proc runcprev {args} { exp_continue } timeout { - send_error "Connection with $host timed out\n" - exit 1 + terror "connection to $host timed out" + catch { close } + return } eof } @@ -1096,16 +1115,18 @@ proc runcprev {args} { if {[lindex $res 3] != 0} { incr retry_count if {$retry_count > $config(max_scp_retry_count)} { - send_error "failed to copy file to $host\n" - exit 1 + terror "failed to copy file from $host" + catch { close } + return } - send_error "Retrying connection to $host\n" + send_error "$argv0: Retrying connection to $host\n" set retry 1 set password_sent 0 } } else { - send_error "Operating system error while copying script file to $host\n" - exit 1 + terror "operating system error while copying script file to $host" + catch { close } + return } catch { close } } @@ -1118,13 +1139,15 @@ proc runcmd {host command} { spawn -noecho ssh [hostuser $host]@$host expect { denied { - send_error "Bad password\n" - exit 1 + terror "bad password for [hostuser $host] on $host" + catch { close } + return } "*assword:" { if $password_sent { - send_error "Bad password\n" - exit 1 + terror "bad password for [hostuser $host] on $host" + catch { close } + return } else { send "[hostpass $host]\r" incr password_sent @@ -1132,12 +1155,13 @@ proc runcmd {host command} { } } timeout { - send_error "Connection with $host timed out\n" - exit 1 + terror "connection to $host timed out" + catch { close } + return } eof { - send_error "connection to $host failed: $expect_out(buffer)\n" - exit + terror "connection to $host failed: $expect_out(buffer)" + return } "Are you sure you want to continue connecting (yes/no)?" { send "yes\r" @@ -1149,7 +1173,8 @@ proc runcmd {host command} { send "$config(sudo)$command\r" } } - + + if {$config(sudo) != ""} { expect_before { "*assword*:" { @@ -1158,7 +1183,7 @@ proc runcmd {host command} { } } } - + if { ![config_option log] && [info exists config(silent)] } { log_user [expr !$config(silent)] } else { @@ -1174,6 +1199,17 @@ proc runcmd {host command} { -re $config(prompt) { send "logout\r" } + "*\r" { + exp_continue + } + "*\n" { + exp_continue + } + timeout { + terror "host $host timed out" + catch { close } + return + } eof } } @@ -1226,3 +1262,14 @@ if [info exists tempscriptname] { file delete $tempscriptname } +if [info exist errors] { + send_error "$argv0: there were [llength $errors] errors:\n" + foreach err $errors { + send_error "$argv0: $err\n" + } + exit 2 +} + +exit 0 + + |