Using Expect and Cadaver to automate WebDAV uploads (w\ example script)
February 13th, 2009

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”!
March 23rd, 2009 at 11:34 pm
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!
May 21st, 2009 at 3:27 pm
Thanks for posting! I had been searching for something like this for a couple hours before I ran across this article.
June 25th, 2009 at 2:42 pm
I’ve been trying this with Cygwin, but I always get the error message “spawn: command not found”
Any clue?
June 25th, 2009 at 11:52 pm
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.
June 26th, 2009 at 8:24 am
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.
June 26th, 2009 at 3:02 pm
Ok, works fine. I understand now: spawn is available only through expect, when you run the script. Many thanks for the above example.
June 28th, 2009 at 11:53 pm
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!
November 11th, 2009 at 4:30 am
IMPORTANT:
you need to save the script provided in a text file,
then launch it with expect:
> expect -f filename
December 16th, 2009 at 5:11 am
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
February 6th, 2010 at 8:04 am
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
September 19th, 2010 at 1:05 pm
[...] Per un esempio completo di automazione con Expect di Cadaver vi consiglio questo articolo: Using Expect and Cadaver to automate WebDAV uploads [...]
September 23rd, 2010 at 1:08 pm
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!
September 23rd, 2010 at 9:58 pm
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?
April 29th, 2011 at 3:17 pm
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.