Re: Need "Additional" help with Expect script
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Need "Additional" help with Expect script

From: Khaled <nospam.ksubs@free.fr>
Date: Sun May 29 2005 - 13:13:31 CEST

Hello,

A debugged output should tell you why yr code doesn't match the
"Connection refused" pattern. But I can bet that the pattern is being
'swallowed' by the asterisks at the end of your second and third
patterns. You should not really start or end your glob patterns by an
asterisk. Also, when you are looking for a pattern so as to send
something to the process, make sure this pattern is anchored by a "$"
and you are matching the last characters of this pattern; so that you
do not send to the process prematurely, i.e before the process is ready
to accept what expect is sending.

The above corrections should make it work and you'll be able to match
the "Connection refused" pattern, but what if scp fails with another
reason? how would you know? The better way IMO, is not to exclude the
conditions where you should not do the file manipulations, but rather
to look for the single condition where you were able to transfer the
file. You should look for the msgs telling you that file transfer was
succesful, "100%" or "completed" or even "ETA" and such. When you get a
match you know that you should manipulate the files, otherwise you know
you should not.

The [catch] command serves for capturing errors. See it's man page, it
is something else :^)

Look also at the man entry for [file]. You'll like it more than using
the shell utility "mv".

Rgrds,
Khaled

Daryl Rose wrote:
> How do I test the return of scp? I've run into several scenarios
> where scp did not transfer the files. i.e. could not get logged in,
> wrong password, "Connection refused". If I get a successful
> transfer, then I want to do some file manipulation. If the transfer
> did not complete successfully, then I want to log the error, and
> continue with the next server. The "Connection refused" is a good
> example of one problem that I've ran into. I would like to just log
> that, and then continue to the next server, but as the script is
> currently written, it will hit the "eof" and then try to do the
> file manipulation. I tried to use "Catch", but it didn't catch
> anything. Unless I did it wrong. I know there is a better way; I just
> don't know how to write it in TCL/Expect.
>
> **************************************************************
> set file [open $hosts_file r]
>
> while {[gets $file host] != -1} {
> set count 0
> set refused 0
> log_user 0
> spawn scp -q $host:$remote_file $local_file
> expect {
> -re "assword:.?$" {
> exp_send $password($count)\r
> incr count
> exp_continue
> } "The authenticity of host*" {
> exp_send yes\r
> exp_continue
> } "Warning:*" {
> exp_continue
> } "Connection refused" {
> puts "Connection refused on host: $host\r\r"
> set refused 1
> exp_continue
> } eof {
> catch {close -i $spawn_id}
> wait -nowait -i $spawn_id
> if {!$refused} {
> set finished 1
> }
> }
> }
> if {$finished} {
> set finished 0
> exec mv $local_file/file1 $local_file/$host:file1
> exec mv $local_file/file2 $local_file/$host:file2
> }
> }
> close $file
> puts "\r\r All done \r"
>
> Once again, I appreciate all of the help.
>
> Thank you.
>
> Daryl Rose
>
Received on Thu Sep 29 14:19:36 2005