Skip to content

Instantly share code, notes, and snippets.

@joshkunz
Last active December 11, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkunz/4516316 to your computer and use it in GitHub Desktop.
Save joshkunz/4516316 to your computer and use it in GitHub Desktop.
This is what I have of a shell based moodle upload system. The username should be passed as the first argument. This (with the exception of logging in) does *not* work.
#!/bin/bash
# Where the cookies are stored
COOKIES='cookies.tmp'
# Where the pages are temporarily stored
PAGE='page.tmp'
# value matching regex constant
VALUE_MATCHER='^ value="([^"]*)"'
# Get the login page for the first cookie
curl -c $COOKIES \
http://www.learning2.eng.utah.edu/login/index.php > /dev/null
# prompt for a password
read -p "Password for moodle: " -s PASS
# Login
curl -Lv -b $COOKIES -c $COOKIES -d "username=$1" -d "password=$PASS" \
http://www.learning2.eng.utah.edu/login/index.php > /dev/null
# Go to the assignment page
curl -L -o $PAGE -b $COOKIES \
http://www.learning2.eng.utah.edu/mod/assignment/view.php?id=6072
# The above is the url for "Assignment #2", it should be the only thing that
# would need to be changed if you wanted to submit a different assignment
# Get the value of a form attribute
function get_value() {
xmllint --html --xpath "//input[@name='$1']/@value" $PAGE 2> /dev/null \
| sed -E "s/$VALUE_MATCHER/\1/"
}
USERID=`get_value userid`
CONTEXTID=`get_value contextid`
# Go to the assignment uploader
curl -o $PAGE -b $COOKIES \
"http://www.learning2.eng.utah.edu/mod/assignment/type/upload/upload.php?userid=$USERID&contextid=$CONTEXTID"
SESSKEY=`get_value sesskey`
MAXBYTES=`get_value maxbytes`
MAXFILES=`get_value maxfiles`
COURSE=`get_value course`
ITEM=`get_value itemid`
# Upload next step
curl -o $PAGE -b $COOKIES \
"http://www.learning2.eng.utah.edu/repository/draftfiles_manager.php?env=filemanager&action=browse&subdirs=1" \
"&itemid=$ITEM&ctx_id=$CONTEXTID&course=$COURSE&sesskey=$SESSKEY&maxbytes=$MAXBYTES&maxfiles=$MAXFILES"
# NextStep
curl -o $PAGE -b $COOKIES \
"http://www.learning2.eng.utah.edu/repository/filepicker.php?env=filemanager&draftpath=/&savepath=/&action=list" \
"&itemid=$ITEM&ctx_id=$CONTEXTID&course=$COURSE&sesskey=$SESSKEY&maxbytes=$MAXBYTES&maxfiles=$MAXFILES"
REPOID=`get_value repo_id`
# Actually upload the file
curl -o $PAGE -b $COOKIES \
-F action=upload -F 'draftpath=/' -F 'savepath=/' -F repo_id=$REPOID \
-F repo_upload_file=@name_of_file \
"http://www.learning2.eng.utah.edu/repository/filepicker.php?" \
"ctx_id=$CONTEXTID&itemid=$ITEM&env=filemanager&" \
"course=$COURSE&maxbytes=$MAXBYTES&maxfiles=$MAXFILES&subdirs=1&" \
"sesskey=$SESSKEY&action=browse&draftpath=/&savepath=/&repo_id=$REPOID"
# where name_of_file is the file name
# this should go all the way through the upload part, but it does not
# include the 'submit' part.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment