Using Expect and Cadaver to automate WebDAV uploads (w\ example script)

February 13th, 2009

... or the one about how I finally got around to learning expect.

DISCLAIMER: I still hate windows.

I spent a decent portion of today hunting around for a way to script or otherwise automate (at the command line) uploading a file to one of $wageslavery’s external vendors. After diddling with curl -T only to find that it only works if the destination file doesn’t exist, I finally bit the bullet and installed Cygwin. Cygwin provides a win32 version of expect, the command line automation tool and cadaver, the command line WebDAV utility, basically smbclient for WebDAV. Working example code after the break!

The script is applicable to any platform really, but what’s below on *nix platforms may or may not require the “\n” at the end of the send statements. expect sends “\r” at the end of the line by itself, and I suspect the Cygwin *nix utils running on Windows instead of *nix is probably the cause.

A practical, santized, example including dealing with a self-signed certificate:
spawn /usr/bin/cadaver https://upload.externalvendor.com/myuploads
expect "Do you wish to accept the certificate"
send "y\n"
expect "Username:"
send "starbuck\n"
expect "Password:"
send "0mgBACON\n"
expect "dav:"
send "lcd /path/to/data/files\n"
expect "dav:"
send "rm datafile.csv\n"
expect "dav:"
send "put datafile.csv\n"
expect "dav:"
send "quit\n"

Hopefully this helps some other poor soul stuck dealing with WebDAV uploads instead of proper sftp!

Update 2009-02-24: I added the word “script” a few times to help the Google search results. Maybe I’ll be #1 on “cadaver expect script”!

14 Responses to “Using Expect and Cadaver to automate WebDAV uploads (w\ example script)”

  1. matt Says:

    WOOHOO! #1 on google for cadaver expect script. I hate SEO optimizing bastards, but I couldn’t find an example solution to this problem so I created my own!

  2. MK Says:

    Thanks for posting! I had been searching for something like this for a couple hours before I ran across this article. :-)

  3. Séb Says:

    I’ve been trying this with Cygwin, but I always get the error message “spawn: command not found”

    Any clue?

  4. matt Says:

    I’ll bet you don’t have spawn installed in your cygwin, or spawn can’t find what you’re trying to launch, which is cadaver in my examples.

  5. Séb Says:

    spawn is not in /bin or /usr/bin indeed, but I don’t see it anywhere on the list of Cygwin downloadable packages.

    I tried to launch cadaver as well.

  6. Séb Says:

    Ok, works fine. I understand now: spawn is available only through expect, when you run the script. Many thanks for the above example.

  7. matt Says:

    You are welcome, I’m glad this has helped someone! That’s really why I wrote this in the first place.

    If you read this and it helped, leave me a comment — PLEASE!

  8. andrea Says:

    IMPORTANT:
    you need to save the script provided in a text file,
    then launch it with expect:
    > expect -f filename

  9. jahn Says:

    there is a problem if you are uploading big files that take
    long: the expect timeout is coming in and aborts the upload..

    for simply putting a file online you could also
    edit the ~/.netrc (see: cadaver manual) file for
    automatic login without password and user prompt
    and then do the following in a script:

    cadaver “https://somewebdav.place” << EOF
    put /tmp/${FILENAME}
    exit
    EOF

  10. mikael Says:

    or you can just set the timeout value -1 eg.

    #Turn the timeout off so the script does not terminate, otherwise it will close down after 10 seconds in the transfer
    set timeout -1

  11. Linuxaria WebDAV su Linux : Linuxaria Says:

    [...] Per un esempio completo di automazione con Expect di Cadaver vi consiglio questo articolo: Using Expect and Cadaver to automate WebDAV uploads [...]

  12. Nathan Says:

    Hi everybody…this helped me too! I’ve got a webDav repository with a folder with a HUGE (11K) number of subfolders. Whenever I’m trying to get into that folder through cadaver, I eventually get a timeout….I’m not seeing any options in cadaver to set a timeout…? any help is much appreciated!

  13. matt Says:

    I am on the road (burning man -> crater lake np -> redwood np -> lasen volcanic np -> yosemite np -> kings canyon np and still going!) and don’t have a way to test the comment from mike (comment #10) but it appears that it looks something like ’set timeout -1′ in the expect script. Are you timing out from cadaver or expect?

  14. bisont87 Says:

    Thank you. I based on your post to make a script to let cadaver connect to dropdav.com on openwrt. I had to use empty instead of expect but I found your script useful.

Leave a Reply