This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

lftp problems

Old posts that have not been replied to for several years.
Locked
t
tR
Voice
Posts: 19
Joined: Wed Dec 12, 2001 8:00 pm

lftp problems

Post by tR »

Hi,
im trying to code a tcl from that i can manage lftp commands from a channel so i can do backups and so on from that chan without connecting to a shell. Is it possible to have read and write acces to lftp commands from tcl. I tried with following method - but this seems not to work as i expected - and leads to bots timeouts:

set fxpclient ""
bind pub - "!open" p_open
bind pub - "!ls" p_ls
bind pub - "!close" p_close

proc p_ls { nick uhost handle channel text } {
global fxpclient
puts $fxpclient "ls"
}

proc p_close { nick uhost handle channel text } {
global fxpclient
close $fxpclient
}

proc p_open { nick uhost handle channel text } {
global fxpclient
putlog "open"
set fxpclient [open "|lftp whatever.com:21" r+]
utimer 1 p_gets
puts $fxpclient "USER test pass"
putlog "open done"
}

proc p_gets {} {
global fxpclient
set tmp [gets $fxpclient]
if {[llength $tmp] > 0 } {
putlog "$tmp"
}
utimer 1 p_gets
}

i tried to read every second the output from lftp to show that. But this seems not the way to do that :( someone any ideas? thanx in advance.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Use "fileevent readable" to see when data is ready to be read. Also put the file handle in async mode with fconfigure.
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

your useing an ftp lib i gues the one from the tcl tk site got same problem don't think it is the script but the ftplib and i have got it with all the tcl scripts that support ftp maybe use the ftp command of the shell instead
t
tR
Voice
Posts: 19
Joined: Wed Dec 12, 2001 8:00 pm

Post by tR »

no im using lftp - a external program. the ftp lib doesnt suply server to server transfer and i need that for the backups.
i tried with the commands suggested by stdragon but it seems not to work.

Here the code:

bind pub - "!cmd" p_cmd
set lftp_cmd ""

proc lftp_reader {lftp} {
putlog "reader"
gets $lftp text
putlog "read: $text"
}

proc lftp_writer {lftp} {
global lftp_cmd
puts $lftp $lftp_cmd
set lftp_cmd ""
}

proc p_cmd { nick uhost handle channel text } {
global lftp_cmd
putlog "cmd: $text"
set lftp_cmd $text
}

set lftp [open "|lftp" w+]
fconfigure $lftp -blocking 0 -buffering full
fileevent $lftp readable [list lftp_reader $lftp]
fileevent $lftp writable [list lftp_writer $lftp]

the strange think ist that after starting i got only writable events and never got a event that is readable. Even when i try in lftp_writer to read
something from $lftp with gets its empty :(
Anyone a idea or is it simply not possible in tcl to handle complete stdin and stdout for a external program?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

You don't need to use the writable event. That's really for if you write so much data that it starts blocking, but you're only going to send a few commands.

So, instead of the "filevent writable" bit, just have, "puts $lftp yourcommand". The other thing you're missing is "flush $lftp" whenever you use puts (immediately after). Because you have the "-buffering full" option, it doesn't write the data until you call "flush".

Also, in p_cmd, you'll want to again do "puts $lftp $text" and "flush $lftp" rather than wait for the writable event.
t
tR
Voice
Posts: 19
Joined: Wed Dec 12, 2001 8:00 pm

Post by tR »

Thanx for the fast reply. That works as described. Only problem remaining ist that it seems that stdout is cached somewhow :( so i had to do a few commands until stdout buffer is full and then the readable event fires.
i tried fconfigure $lftp -buffering none or a little bufersize but it seems that this not affects the stdout buffer. Maybe its the buffer outside tcl that makes the problems. Maybe a idea how i can avoid that? Thanx in advance.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Did you use flush after puts?
t
tR
Voice
Posts: 19
Joined: Wed Dec 12, 2001 8:00 pm

Post by tR »

yes. I tried now with the normal ftp command and this works fine. Only
on lftp there seems to be a buffer between stdout output. On bash i didnt see a difference between the ftp in/output and the lftp in/output. I also tried with different translate settings but this also dont help.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Hmm, if lftp itself is caching the output, there isn't a lot you can do. But, if you send lftp the "quit" command, it should exit and send all of the cached output at once. You have to check for eof (use the 'eof' command) in your lftp_reader proc, and then close $lftp when eof is detected.
Locked