Home > Archive > Tcl > March 2006 > FTP using expect
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ranjan 2006-03-27, 4:02 am |
| Hello,
I am new to Expect.( This is a very basic expect program, please let me
know if there is a
separate group where I can post this query)
I am simply trying to ftp my local mail server , entering username and
password read from a file and then exitting.
Sometimes this program runs fine till the end and exits properly, but
sometimes it hangs in between. Like sometimes after entering user name
, or sometimes after entering passowrd or sometimes during ftp to the
server.
Following is the program, please let me know if there is something
wrong with the program.
I tried it on various server and even tried telnet program but face the
same problem.
#!c:/Tcl/bin/tclsh
package require Expect
spawn ftp indmail
set timeout -1
set file [open "C:/Tcl/bin/password.txt" "r"]
gets $file username
gets $file password
expect "User*"
send "$username\r"
expect "Password:"
send "$password\r"
expect "ftp>"
send "binary\r"
expect "ftp> "
send "bye\r"
| |
| Uwe Klein 2006-03-27, 4:02 am |
| Ranjan wrote:
> Hello,
> I am new to Expect.( This is a very basic expect program, please let me
> know if there is a
> separate group where I can post this query)
bo need to go any further
> I am simply trying to ftp my local mail server , entering username and
> password read from a file and then exitting.
> Sometimes this program runs fine till the end and exits properly, but
> sometimes it hangs in between. Like sometimes after entering user name
> , or sometimes after entering passowrd or sometimes during ftp to the
> server.
one of the more popular problems with expect is
sending a reply before the other side has finished sending.
i.e expecting "word:" when the actual sequence is "word: " (note the trailing space)
or "ftp>" versus "ftp> "
as an explanation( in the password case):
after the password is requested "..word: " all previous input is flushed,
the tty is changed to nonecho mode. the app now waits for input.
if your script started sending this after the ":" part or all will have
been lost in the flushing. casuing either "wrong password" or a timeout
symptoms are scripts that have a tendency to work in an eratic way
dependend on cpu speed, load and airpressure.
> Following is the program, please let me know if there is something
> wrong with the program.
> I tried it on various server and even tried telnet program but face the
> same problem.
>
> #!c:/Tcl/bin/tclsh
> package require Expect
>
> spawn ftp indmail
> set timeout -1
> set file [open "C:/Tcl/bin/password.txt" "r"]
> gets $file username
> gets $file password
> expect "User*"
mine does "Name (localhost:uwe): "
you might try:
expect -re "User.*?: "
recheck your clients output
> send "$username\r"
> expect "Password:"
just checked some ftp servers one sends "..word:" and the other "..word: "
recheck your clients output
> send "$password\r"
>
> expect "ftp>"
try
expect "ftp> "
> send "binary\r"
obs, the next one waits for trailing space.
> expect "ftp> "
> send "bye\r"
>
uwe
| |
| Donal K. Fellows 2006-03-27, 4:02 am |
| Uwe Klein wrote:
> Ranjan wrote:
[...][color=darkred]
> symptoms are scripts that have a tendency to work in an eratic way
> dependend on cpu speed, load and airpressure.
An alternative that might work for Ranjan's case is to not use Expect to
automate the ftp program, but rather to use the ftp package out of
tcllib which talks FTP directly. To put it another way, you'd be cutting
out at least two of the middlemen from your interaction. :-)
If the purpose of this is to learn how to use Expect to automate things
and not to just transfer files, ignore what I just said. Making FTP not
require baby-sitting is a pretty good way to learn Expect...
Donal.
| |
|
|
| Ranjan 2006-03-27, 8:02 am |
| Thanks a lot for all your replies.
Donal,
I would like to use Expect as I will be needing this for other purposes
also
Vinayak,
I will try your link if the current package with me doesnt work.
Uwe,
I have modified the code and now waits for full line using reg. exp.,
but still problem is coming in between.(I even tried putting
some delay before any send command). Now it accepts username and
password but after displaying message 'the user has
logged in' , it sometime hangs.
Below are the two outputs , first one is OK but in second one it hangs
after logging in (ftpinmail.tcl is the file name)
Ranjank@ranjan /cygdrive/c/tcl/bin/ranjan
$ ./ftpinmail.tcl
Connected to inmail.
220 inmail FTP server (SunOS 5.8) ready.
User (inmail:(none)): 331 Password required for ranjan.
Password:
230 User ranjan logged in.
ftp>
Ranjank@ranjan /cygdrive/c/tcl/bin/ranjan
$ ./ftpinmail.tcl
Connected to inmail.
220 inmail FTP server (SunOS 5.8) ready.
User (inmail:(none)): 331 Password required for ranjan.
Password:
230 User ranjan logged in.
Here "ftp> " does not come in next line after it displays "User ranjan
logged in." So what can be the problem now?
I also saw once or twice it hanged after displaying "Connected to
inmail"
Thanks,
Ranjan
| |
| Ranjan 2006-03-27, 8:02 am |
| Thanks a lot for all your replies.
Donal,
I would like to use Expect as I will be needing this for other purposes
also
Vinayak,
I will try your link if the current package with me doesnt work.
Uwe,
I have modified the code and now waits for full line using reg. exp.,
but still problem is coming in between.(I even tried putting
some delay before any send command). Now it accepts username and
password but after displaying message 'the user has
logged in' , it sometime hangs.
Below are the two outputs , first one is OK but in second one it hangs
after logging in (ftpinmail.tcl is the file name)
Ranjank@ranjan /cygdrive/c/tcl/bin/ranjan
$ ./ftpinmail.tcl
Connected to inmail.
220 inmail FTP server (SunOS 5.8) ready.
User (inmail:(none)): 331 Password required for ranjan.
Password:
230 User ranjan logged in.
ftp>
Ranjank@ranjan /cygdrive/c/tcl/bin/ranjan
$ ./ftpinmail.tcl
Connected to inmail.
220 inmail FTP server (SunOS 5.8) ready.
User (inmail:(none)): 331 Password required for ranjan.
Password:
230 User ranjan logged in.
Here "ftp> " does not come in next line after it displays "User ranjan
logged in." So what can be the problem now?
I also saw once or twice it hanged after displaying "Connected to
inmail"
Thanks,
Ranjan
| |
| Uwe Klein 2006-03-27, 8:02 am |
| Hi,
obs: i have never used expect on windows.
a couple of ways to debug this:
do not change timeout to "forever" ( set timeout -1 )
leave it as it is.(10sec)
1. add to your calls to expect some handling of exceptions
like eof and timeout:
#######################
set phrase "word: "
expect \
-re "$phrase" {
puts stderr "input:\"$phrase\" seen"
# do something or not
} eof {
puts stderr "input:EOF seen, exit" ;# the spawned process has gone
exit
} timeout {
puts stderr "input:TIMEOUT seen, exit?"
# now you can either continue:
exp_continue ;# restarting the call.
# or have a look what input is lingering:
expect -re ".*" ;# as long as log_user is active you will see the output
# and/or exit
exit
}
#########################
2.use expects verbose mode :
exp_internal 1
will give a running comentary what expect sees and thinks it should do.
G!
uwe
| |
| Cameron Laird 2006-03-27, 7:04 pm |
| In article <1143457187.895980.313420@j33g2000cwa.googlegroups.com>,
Ranjan <ranjan.kapoor@gmail.com> wrote:
>Thanks a lot for all your replies.
>Donal,
>I would like to use Expect as I will be needing this for other purposes
>also
>Vinayak,
>I will try your link if the current package with me doesnt work.
>Uwe,
>I have modified the code and now waits for full line using reg. exp.,
>but still problem is coming in between.(I even tried putting
>some delay before any send command). Now it accepts username and
>password but after displaying message 'the user has
>logged in' , it sometime hangs.
|
|
|
|
|