2

I'm teaching a course this summer. My students' first assignment will be to install a virtual machine that I created (the machine will run Ubuntu). Upon installation they are to run the following script

$ cat ~/bin/all-done
#!/bin/bash

timestamp() {
    date +"%Y-%m-%d_%H-%M-%S"
}

echo "Type your last name: " 
read name

SUBJECT="Class Update"
TO="my_email@my_domain.com"
MESSAGE="/tmp/message.txt"

echo "$name completed the assignment" >> $MESSAGE
echo "Time: $(timestamp)" >> $MESSAGE

/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE

rm $MESSAGE

echo "You completed the first assignment. Welcome to the class!"

The script records their last name and sends me an e-mail notifying me that they installed the machine.

The annoying part here is that I'll end up receiving 30 e-mails and that I have to parse all of this information myself. I'm wondering if there is a better way for me to collect this data. Ultimately, it would be great to have a bash script that appends each last name to a csv file that I can access.

Is there a tool in unix designed to do this kind of thing?

6
  • 1
    Perhaps you can have the script POST the information to a HTTP server which then adds the information to a CSV file. – F21 May 17 '16 at 5:18
  • @F21 This sounds like a good idea. Do you know any references where I could learn how to do this? – Brian Fitzpatrick May 17 '16 at 5:41
  • If you know how to write go, it should be pretty trivial to write a small HTTP server that you can run somewhere to achieve this. Having said that, is there a language you are familiar with? – F21 May 17 '16 at 5:46
  • @F21 I'm most comfortable with python. – Brian Fitzpatrick May 17 '16 at 5:48
  • I am not familiar with python, but it appears to have an HTTP server that you can use as a starting point: docs.python.org/2/library/simplehttpserver.html – F21 May 17 '16 at 6:03
2

Since, as you say, it's a machine you created, why not have it mount a shared drive (CIFS or NFS) and output to it?

That may be useful for other file sharing needs as well.

2
  • Would you let a bunch of students have write access to a shared drive unless they absolutely needed it? and without their own unique, personal login credentials? I've done years of sysadmin work at a university, and at an ISP that specialised in providing internet service to primary and secondary schools....I wouldn't. – cas May 17 '16 at 11:21
  • You could restrict their access to that single file if you wanted. At this point, they probably won't even know they have access to it or they wouldn't take that course. – Julie Pelletier May 17 '16 at 15:45
1

There are several Mail Delivery Agents (MDA) than can filter mail by various criteria (including Subject: header). Some of the more common ones are procmail, maildrop, and filter.

I've used procmail for decades. To filter mail with that Subject: header into a separate mailbox, I'd create a rule like this in my ~/.procmailrc:

:0
* ^Subject: Class Update
class-update/

procmail supports both mbox single-file mailboxes and Maildir style one-file-per-message mailboxes. The example above will store the message in a Maildir folder called ~/Mail/class-update/. Remove the trailing / if you prefer mbox format.

BTW, it would be a good idea to:

  1. Ask for the student ID too. In my experience, it's extremely common to have duplicate surnames (actually, you should ask for "Family Name" rather than "last name" or "surname" because family name isn't always the last name).

  2. Change the subject to "Class Update: $(timestamp) $name [$id]" or similar. Then your procmail (or other MDA) rules only needs to extract the Subject: header and can throw away (or better yet, archive to a mailbox) the rest of the message.

For example, the following procmail rule will save the message to a folder as above, and then extracts the Date: and Subject: headers and saves them to a file.

:0
* ^Subject: Class Update
{
    # first save a copy of the message (for a safety archive)
    :0c
    class-update/

    # then extract the Subject line and save it to a file
    :0 fw
    | formail -c -xSubject | sed -e 's/^Class Update: //' \
        >> /path/to/class-updates.txt
}

FYI, formail is a mail formatting tool that comes with procmail.

1
  • BTW, you may also want to collect the IP and MAC addresses of the VM and put them in the message body & Subject too - that'll make it harder for one student to just let other students use the VM they installed. Or if IP and/or MAC aren't even close to being unique, have the script increment and save a run counter (in ~/ or /var/tmp/ perhaps, but not /tmp as that gets erased on reboot) and include the counter in message body and header. A smart student could look at your script and easily defeat that, but this will detect cheating by the dumb ones (who are most likely to need to cheat). – cas May 17 '16 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.