Magomatic Brute Forcing?

April 5th, 2009

I was writing up the page for the Magomatic and started on the improvements section.  I realized I started going on and on about a possible brute-forcing function and I decided that it would be better suited for a post instead of putting it on the page.  So prepare yourself, as I am about to dump everything I can think of regarding brute forcing magstripe card door locks.

I was thinking that since I can just read card data with a computer,  I should be able to read a room number off the card, alter that data to another room number, and put that information on my emulator.  This would work in theory, but what if the card contains encrypted data?  Then you would not know what represents the room number.  What’s more, what if the card does not conform to the track 2 standard?  They could use their own protocol.  Either way, all I will see are 1’s and zeros.

Since the encrypted data is still only 1’s and 0’s, you could potentially try every possible combination of 1’s and 0’s until you found a combo that worked. Analyzing multiple cards of the same type, (door keys for example) you could potentially see how much data changes from room to room or date to date.  You could then lower the amount of bits you would have to brute force to just the number of bits that change.  This may be impractical seeing that to brute force each bit would mean that the total number of possible combinations is equal to 2^n where n is equal to the number of bits required to brute force.  That means for just 10 bits you would have to try 2^10 = 1024 combinations.  It would probably take the magstripe reader about 1.5 – 2 seconds to deny a card.  If it took 2 seconds, that means brute forcing 10 bits would take (1024 * 2) / 60 = 34.13 minutes.  That might not be worth the time.

Another option for brute forcing is to brute force each byte, rather than each bit.  This will only work if the magnetic stripe key follows valid track protocol and is not encrypted.  In this case, you could just read the data on your computer and alter whatever you wanted, however what if you wanted a hand held device to do everything for you automatically?  It is rather cumbersome to hook up a reader to a laptop, scan the card, alter the data, program a microcontroller, put the micro in your emulator, and then open a door.  It would be much nicer to have a device that could just brute force open any door.

If one of these cards follows valid track 2 format, then you could just brute force every 5 bits (there are 5 bits in a byte in track 2 format) rather than every single bit.  However, now you have more possibilities for each byte.  It’s not just either a one or a zero.  Each byte can represent 11 different characters (0-9, =).  I found this information by consulting this resource.  Track 2 also states that there are 37 data bytes between the start and end sentinal values.  This means that the total possible combinations you would have to brute force would be 11^37= way too many to be worth the time.  In this case, the best thing to do would be to analyze the data on a computer, figure out where the room number and/or expiration date is stored, and then program a microcontroller to try every possible room number while keeping the other data the same.  It could then make sure that the date was some date way in the future to ensure it would work.  Better than this, you could put an interface on the device to program the exact room number.  Using a serial LCD and a few buttons, you could view the data after it is scanned into the device.  Then the buttons can be used to alter the data or just punch in the room number.

Brute forcing these things seems mostly impractical due to the fact that it would take forever to brute force all the data on a card but in the event of encryption, it may be necessary.  If you can narrow down just a few bits that need brute forcing it would be worth it.  I’ll have to experiment once I have some data to analyze.

Magstripe Emulator Device Complete

April 4th, 2009

I first became interested in magstripes a few years back.  I found a few resources online that explained how you can build a primitive magstripe reader using just a magnetic readhead from a walkman and a headphone jack.  You can use custom software to look at the waveform that is created and decode the binary data.  I never was able to get that working right, most likely due to now being able to swipe the read head over the card in the exact right spot.  A few weeks back I finally decided to bite the bullet and just buy a magstripe reader online so I could play with magnetic stripes.

After reading through the StripeSnoop site I decided to buy a 30$ TTL magstripe reader.  These seem to be the simplest to use and they work with StripeSnoop.  However, StripeSnoop requires a gameport or parallel port connection, and most computers nowadays do not have either of those.  I wanted to be able to use my reader on any system.  I found this guide that shows how to make a sort of USB adapter for the TTL reader.  It basically reads the TTL signals from the magstripe reader, and then “types” them into the computer as though it is a keyboard.  If you have a notepad window open it will just dump a long string of 1’s and 0’s into the window.  StripeSnoop has a -i option that takes input from the keyboard so all of this should work together.  I purchased one of the adapter boards along with the magstripe reader. Five days later they both showed up at my door and in about an hour I had everything hooked up on my computer and functioning.  My reader only reads track two, although I cold read track one if I put a small piece of plastic in the bottom of the reader to raise up my cards by exactly one track size.  For now, reading just track two will work fine since my research showed that most cards use that track anyway.

Enter the idea for the magomatic.  I’ve had this idea for a while but it keeps changing slightly in my mind.  I essentially want the ability to read a magstripe card and then emulate it back.  This is different from cloning a magstripe card onto another card.  This is basically “recording” the magstripe data and then “playing” it back as though it is coming from a magstripe card, even though it is not. I first thought that the easiest way to accomplish this goal would be to record the magstripe data as audio, and then play it back out through an amplifier, into an electromagnet.  I had found this instructable where someone did something similar and prooved that it works.  This person would scan in the data, and then put it into a C program.  The c program would encode the binary data into a wav file.  He could then put the wav file on his iPod, play it out through an amplifier and into an electromagnet.  He included a video to proove that the concept works.  This is how I started the project.

I had to proove to myself that his idea worked. I created an electro magnet and downloaded his source code.  I also purchased a small battery powered amplifier from RadioShack that was able to boost an audio signal pretty loud.  I wasn’t able to get the C program to work correctly so I ended up changing some of the code around.  I altered it so I could just paste the raw binary data from my card into an array in the program.  It would then encode that data, rather than converting symbols and letters into binary data and having to generate valid checksums.  After some fiddling with the code and with volume settings I was able to get this working.  I could play the audio file out of my headphone jack, through the amplifier, into the electromagnet, and then into the card reader.  The card reader thought I had swiped my card.  Success.

The next step was figuring out how I could store the data on something portable.  I didn’t want to have to lug around a computer.  My idea was to have a small, handheld device that could read a card and then instantly play it back.  I bought a small picture frame from RadioShack that included a 10 second voice recording module.  It is supposed to be used to store a message along with your photograph.  I had other plans.  I ripped that picture frame appart and pulled out the small recording module.  I removed the microphone and the speaker and just left some wires attached.  To test the module, I hooked my computer’s headphone port up to the microphone wires of the circuit using some aligator clips and a 1/8 inch mono jack.  I pressed the record button on the module and then played the working wav file through the headphones.  I then moved the aligator clips to the speaker wires and plugged the headphone jack into the audio amplifier.  I then had another set of clips going from the amplifier to the electro magnet.  After fiddling with the volumes for recording and playback, I had it working.  I now knew that it was possible to record magstripe audio data onto this module and play it back without losing the data.

The last step was to create my own simplistic reader from a magnetic read head.  I bought an old Walkman from the local Goodwill for $4.  The read head was not difficult to remove.  Unfortunately, I had a terrible time figuring out how to build a mechanism that would line up the read head exactly to track two of the magstripe card and swipe in a nice, straight line.  I tried a few things but everything failed.  I could tell it was picking up data but I never knew what track(s) it was from.  Eventually, after all that work, I had to give up on the audio idea.

It was time to come up with a new plan.  How else could I record the data and play it back though?  I had already been thinking it would be nifty to be able to record the data digitally, rather than via an analog signal.  Having the actual data would allow the possibility of data manipulation.  Why would it be good to manipulate the magstripe data?  One application is in hotels.  Most hotels use magstripe cards as room keys.  What if that data was not encrypted?  What if I could just read the data, see the room number in the data, and then change it to another room number?  I could open any room in the hotel!  I could even put a number pad on the device to allow me to choose what room I wanted to enter.  This is just one interesting application that I thought of.  But how could I accomplish this?

I pretty much instantly thought of using a microcontroller.  I assumed that a Basic Stamp would be too slow to read the magstripe data, and also, Basic Stamps are expensive at around $50 a pop.  My next thought was the use the Parallax SX chip, since it’s the only other microcontroller that I have used and have a programmer for.  To prevent myself from re-inventing the wheel I Googled around to see if anyone else had interfaced a magstripe reader to an SX chip before.  I got lucky and found one article where someone did just that.  He also used the easier to understand SX/B code rather than assembly so it worked rather well for me.

I was going to have to edit the code, though.  He was using a serial LCD for output but I don’t have one of those.  My only real option was to set WATCH’s on the variables that hold the data and then poll for the variables while debugging.  After a few days of fiddling, frustration, code editing, etc I had to give up for a bit.  I was having a terrible time making that code work with my reader.  The author had used a similar, yet different reader and his code just wasn’t working right for me.  At this point I had changed pretty much all of it and simplified it as much as possible and still wasn’t getting anywhere.  I decided to focus my attention on the emulation part of the project.

I figured it would be a waste of time to finally get this reader working, only to find out that I was unable to emulate magstripe data with the SX.  Emulating the data turned out to be a piece of cake.  I created two SX/B functions to output either a one or a zero depending on which function was called.  You essentially have the electromagnet hooked up to two SX pins.  If you want to output a one, you just turn one pin off, and the other on, then after about 1ms you switch them.  For a zero, you turn one pin on and the other off, but after the 1ms delay you leave them in the same position for 1 ms.  You can then “flip the pins” to make the current travel through the electromagnet in the other direction.  I wrote a simple program to output a fake credit card number, including the start sentinal, end sentinal, checksum bits, and the LRC byte.  The extra information was necessary for StripeSnoop to properly decide data.  More information on magstripe protocols can be found at this great resource.

Now it was time to get back to the card reader.  It took another 2-3 days of fiddling before I finally got this part working, but I did.  I was able to store the credit card information in a byte array, and then play it back through an electromagnet into my computer.  StripeSnoop thought I swiped my credit card.  Victory at last!  But I still had to merge the reader function with the emulation function.

Initially, the reader function would specifically wait for the start sentinal character and then collect data in 5 bit intervals.  This is how the typical stripe 2 protocol works.  After thinking about it, though, I realized that hotel systems and other systems might not follow that protocol.  They could very well use their own system.  That’s when it occured to me that for simply replaying the data, I didn’t even have to collect it in such a tedious manner anyway.  That is only benneficial if I want to view the data in a way that I can read it.  Instead, I altered the code to just fill up each byte all the way, instead of only the first 5 bits.  I ended up with two 16-byte arrays.  As you swipe the card, it stores the binary data in each bit of those arrays.  Once the data is stored in the variables you just put the magnet inside the card reader, press a button, and it reads through the variables bit by bit and powers the electromagnet as necessary.  Everything worked as it should.  I now had a working magstripe emulator.

I spent all morning and early afternoon drawing up a schematic, collecting parts, soldering, and troubleshooting.  After a few hours I had a working circuit board.  About 4 hours ago I was able to fit everything into a project box the way I wanted.  I did one last test with everything fitted to make sure it still worked and it worked just fine.  My goal of having a portable, battery operated device capable of cloning magstripe cards is now a reality.  I’m heading to Las Vegas in one week for a short vacation with my family.  I can’t wait to test this thing out.  I’m hoping that the hotel uses track two so I won’t have to try and read tracks one or three and mess with my reader.  I’m also hoping that the keys are encrypted in any way.  This might let me program the SX with my laptop to open other doors.  I’ll post an update once I get back with the results of my experiment.

I’ll also take some photos of the device and post them on the project page.  If I am able to get it to open my hotel room door I’ll definitely post up a video as well.

Packet Radio

December 9th, 2008

Last February I got my Technician class amateur radio license.  Why?  Because I became really interested in high altitude ballooning.  After looking at those pictures how could anyone NOT become interested?  The hobby consists of launched a weather balloon up in excess of 90,000 feet.  The balloon carries a payload that contains a GPS unit for tracking, and a radio to transmit the GPS coordinates back down to the ground, among other things.  Another device called a Terminal Node Controller (TNC) takes the GPS data and turns it into an audio signal to send over the radio.  The TNC also presses the Push to Talk (PTT) switch on the radio before sending the audio.  Essentially a TNC is a radio modem.  It takes digital data and turns it into audio to transmit through the air.  TNC’s can allso to the reverse, and take an audio signal input from a radio to turn it back into digital data.  Without this technology, high altitude ballooning wouldn’t be possible.  Why send up a balloon to take photos if you can’t recover the payload?

This got me thinking.  You can send GPS data over radio, so why can’t you send other data over radio?  Well, you can.  In fact, people have been doing this for a LONG time.  My problem was, TNC’s are expensive.  Several hundred dollars expensive.  After some Googling, it found out that you can actually use your computer’s sound card as a modem.  Makes sense right?  There is software called soundmodem that runs on multiple operating systems that does just this.  It creates a virtual KISS TNC interface in your system for use with packet radio programs.  All you really need is some audio cables, and a PTT interface.  Luckily, a serial PTT interface is really easy to build.

I did some testing yesterday morning with packet radio using a manual switch.  To make the switch, I just cut the ground wire of a 1/8″ stereo audio cable and soldered a momentary pushbutton switch to it.  When I want to send audio out of my radio from the computer, I just hold the switch down manually and set the packet radio software to transmit.  It took some fiddling, but I finally got it working.  I transmitted from my PC running a virtual Ubuntu install through one radio.  The signal went through the air and out the microphone jack of a second radio plugged into my laptop running Windows XP.  I sent the message “KE7SAL testing testing” and the laptop recieved it.  There were some extra garbage characters before and after the message, but none in between.  It was pretty awesome.  The software I was running is called fldigi.  It runs on both Windows and Linux and worked really well.  At first I tried transmitting with fldigi and receiving with digipan, but digipan could not pick up the signal properly.  I tried tweaking settings but to no avail.  Once I switched to using the same program for send and receive it worked flawlessly.

Now that I can send data to myself, I want to send data to somewhere else!  Although it’s cool knowing that I can do it, it’s just not as cool as actually using it for a purpose.  Until some friends of mine get set up with packet radio gear I will need to find another way to go about this.  That’s where a BBS comes in.  A radio BBS is kind of like an old telephone computer BBS.  You can “dial” into it with your radio and a computer and send/receive messages.  Most BBS’s now adays are also connected to the Internet so you can send and receive emails via radio.  There are a few in my city, although I’m not sure I will be able to hit them from my apartment.  Maybe if I use an external antenna and place it in a good location outside.  Regardless, I’m going to start getting my computer’s set up to deal with BBS’s this week.  Here is a breakdown of the steps as far as I can tell:

1) Make sure the AX.25 protocol is enabled in my kernel.  This is the network protocol that packet radio uses to communicate with BBS’s among other things.

2) Make sure KISS mode TNC’s are enabled in the kernel.

3) Install soundmodem.

4) Configure soundmodem using the soundmodemconfig utility.

5) Install jnos2.  jnos2 is a packet BBS software.  I believe it can be used as a BBS client or as a BBS server.  This could prove to be fun to experiment with later on.

6) Configure jnos2 to use the soundmodem interface

7) Build the PTT interface.

That should be about it… I think.  After all of that is done I believe I should be ready to communicate with BBS’s , or just communicate using the AX.25 protocol in general.  If I get a friend to run jnos2 we should be able to send/receive email with each other via radio whenever.  I’m getting pretty psyched about this for some reason.  There’s just something cool about knowing you can transmit any data you want all over the place wirelessly without requiring any type of phone, cable, internet, etc service.  All you need is electricity!

Pneumatic Cannon

August 26th, 2008

I’ve wanted to build a potato cannon since I was in high school, but never really got around too it. About two years ago I finally built one with my friend Drake. It was a very basic cannon. It had about a four foot long barrel and two inches wide, with a three inch wide chamber at the bottom. We would spray some hairspray in there and ignite it with a grill igniter. It worked pretty well but I was always a bit concerned with the fire element. Since then I have read up on using compressed air to build more powerful pneumatic potato cannons. I finally decided to spend some money and get the parts to build one.

I was able to find most of what I needed at Lowe’s.  I bought all the PVC there, a PSI gauge, and a hacksaw.  I ended up having to go to Ace Hardware to get the Schrader valve.  One of the guys that works at Ace Hardware instantly knew what I was building when I told him I was attaching the valve to PVC.  It turns out he has built a few of these himself.  He advised me to use caulking to seal up the valve and PSI gauge to the PVC.

Once I returned home to my apartment I laid out all the pieces to make sure I hadn’t forgotten anything.  It was at this time that I had realized the 3″ Sch40 PVC I had purchased to function as the pressure tank says right on the side of it “FOR NONPRESSURE USE ONLY”.  Great.  It turns out this Lowe’s doesn’t carry PVC pressure pipe in any sizes over 2″.  I also tried the nearest Home Depot with no success.  I will be stopping by a bigger Home Depot today to try and find some.  Once I have the pipe, I can start chopping, drilling, and gluing everything into place.  I’m hoping to have this thing ready by Sunday so I can launch some potatoes into orbit.

I’m adding a page for this to the Works in Progress section.  I’m also adding a page to the ideas section for a coil gun.  While doing research for the air cannon I found many resources on building a small coil gun out of used disposable cameras.  I’ll most likely be stopping by a CVS soon to try and score some.  There are actually several ideas I have for the disposable cameras but the coil gun sounds the most intriguing to me right now.  I’ll be posting updates as things roll along.

New Project Completed

May 22nd, 2008

It’s been a while since I posted on here. There are several reasons for that. The main reason is that my latest project has been taking all my spare time and it was a secret. I didn’t log any of it until just a few minutes ago because I didn’t want the secret to get out. It is an anniversary present for my girlfriend. You can check out the project page for more details on that.

The second reason is that my web server has been down and I haven’t fixed it until recently. My server rebooted one day when I lost power and Apache refused to start for some reason. Rather than sitting down to fix that, I just spent all my time working on the anniversary project. It turns out there was some other instance of httpd running in the background hogging port 81. I have no idea why this was. I’ll have to reboot the system again to see if the problem occurs again. At least I’ll know what the problem is.

In other news, I have started the Near Space class at school last week. I am really excited for this class. We will be sending a balloon equipped with computer, science experiments, and a camera into near space in just a few months. Ryan is splitting the class into teams and should have them posted on the e-shell this weekend at some point. Hopefully I’ll have access to the shell soon. I just e-mailed a local enthusiast to see if he wants to come to class to share his experiences and offer some words of wisdom. Hopefully that will go over well.

My dad should be sending me another radio, antenna and a Tiny Trak 3 module next week. I can’t wait to get that stuff. I want to start messing with APRS tracking as soon as possible to get a feel for it before we actually do a launch. I’m hoping to be on the tracking and telemetry team for the near space class.

I suppose that’s enough updating for now. I have to take some photos of the anniversary lamp to stick on that page, as well as get a schematic up. Man, I still need to get a schematic up on the graduation hacks page… I’ll get on that soon. I’ll also post a video of the lamp in action. Until then.

Please, God let me finish this tonight

May 5th, 2008

I really want to get this radio playlist finished tonight. I’m sick of fighting with it. I was thinking earlier that the best way to deal with the e-mail deletion option might just be to open up the mail again at the end of the program, cycle through all of the e-mails again, and delete them at that time. It’s not elegant but it will do… hopefully.

That didn’t work either. I could not figure out for the life of me what the problem was when it dawned on me. I had placed a print statement towards the end of the program that should always be called, but it wasn’t. That’s when I discovered where the problem was. The Perl script just halted at the exec command. It would run the shell command and then just freeze for some reason. I tried changing it to the system() function and it worked!. I got rid of all the extra stuff I just added and tested out the delete function. It works! Now that means all I should have to do is change the delay time back to 30 minutes and test this baby out.

I turned on the radio at 9:14 and the song Never There by Cake was playing. I love that band. I send a text message to my e-mail towards the beginning of the song. I checked my at jobs a few minutes later and lo and behold, a job was set for 30 minutes from now. So far, all is going well. I just need to check back in 30 minutes to see if the song shows up. If it does, I’ll consider this project finished until I find some problems that need to be fixed.

Thirty minutes pass, I check for the playlist file and it exists.  It’s the moment of truth.  Does it give me the Aritst Cake and the Song Never There?  Yes!  Finally I have this thing working.  I can start something new now.  I’m going to go update the project page right now.

OK let’s do this

May 4th, 2008

If watching Iron Man last night has made me realize anything, it’s that I need to finish this radio playlist project so I can start building my own arc reactor. I’ve been spending all my time as of late destroying GTA4 and it’s time to get back to work.  So let’s DO THIS!

When I last left off, there was something weird happening with the bash script. For some reason the at job would start it, but it would have problems. Since it’s running in the background and there’s no logging going on, I have no idea what the problem is. The first thing I’m doing today is adding some logging features. I’ll just have it echo some stuff out to a file in the same directory so I can see what’s going on.

So I’ve added some logging lines and I’ve also altered the Perl script to add the at job immediately instead of waiting 30 minutes. This change is only temporary. I don’t want to have to wait 30 minutes for each test. Now I just wait 3 minutes for the cron job to start and we’ll see what happens.

Well that didn’t work. No log was created. This means that either none of the logging lines ran at all, or I have to give the full path to where the log is. I’m not sure. I’ll try adding the full path and see what happens after that.

That didn’t work either. It seems like nothing is happening at all. I’m going to try to set the at job manually and see what happens.

That seems to have started working… but not completed. AHA! The log shows that the the script is not getting the input arguments. It seems like at has no way of providing arguments. I tried using double quotes and single quotes around the entire command to no avail. I’m not really sure how to handle this problem. I need a way of sending the time to the bash script but I’m not sure if ‘at’ can do it. Also, it’s really hard to do a Google search for ‘at’ so I’m not sure how to find that out… Perhaps my handy dandy ginormous Unix manual has something to say about this… Not really.

I figured it out! Thanks to this page I found. I can send arguments to the at command by calling it like this:

echo “command arg1 arg2 arg3″ | at -t time

I tested it out and it works. Now I can just change the Perl script to reflect this and we shall see what happens.

That fixed THAT problem. Finally. Although now there are more problems. Luckily, the at command sends mail to my user when something goes wrong. Based on the error messages I received, I forgot to include the full path to the downloaded html code. I changed that and everything finally works! Now I have to figure out why the emails are not being deleted when I set them to be.

I’m having trouble figuring this out.  It only deletes the e-mails if I put the Delete command before the IF statements.  However this makes it so the IF statements do not work at all.  I’m taking a break from this for the rest of the night.  Until tomorrow…

The Radio Playist is starting to piss me off

April 30th, 2008

Now that I have the main script working, I need to be able to receive email notifications. This is where I am stuck. I cannot for the life of me find a console based mail client that will allow this. All of the clients require a user to control them. This is driving me nuts. I have tried a perl script that I found and that didn’t seem to work. I thought about trying to use links with a defined command set but I don’t think that will work either.

I’m starting to think maybe I can set up my own e-mail server. Maybe I can have the mail server perform an action (run a script) when it receives certain e-mails. One problem I have with this is that my ISP (Cox) seems to block access to port 25. There seems to be a way around it if you have a valid MX record. I went to my Domain Name provider and added an MX record for myself. Now I think I should be able to route e-mail through Cox’s mail servers. I’m not sure that I really need this, though. They might only be blocking outbound e-mail and not inbound. I can’t figure it out.

Screw it. I’m just going to go for it and see what happens. I’m installing postfix on my server and I’m going to try to get all of this working.

I started out following this guide, but it includes a lot of other programs I don’t need. I don’t want anti virus or spam protection or anything like that. I just want a really basic mail server.

OK. So I’ve spent several hours trying to figure out how the heck to solve this problem and I think I’ve finally found my answer. It is a Perl script written specifically to retrieve e-mail messages from GMail accounts. Before it would run on my system I had to install two Perl modules using cpan:

install Mail::POP3Client
install IO::Socket::SSL

I saved the script to my computer, added execute permissions (chmod +x) and changed the variables to match my own g-mail account. I ran the script and presto, I had my e-mail… finally. I wish I had found this a lot sooner. I also wish I was a better programmer. So now, I have a way to use a script to retrieve my e-mail. I just have to modify this script to locate any messages with only the word “Radio” as the subject, and then call my other script.

I have added a few lines of code to the script in order to test for the occurrence of the word “Radio” as well as to get the time that the e-mail message was sent. The time is crucial in order to tell when the song was actually played. The code I changed looks as follows:

————————-CODE———————-

# loop over msgs

for($i = 1; $i <= $pop->Count(); $i++) {

print $pop->Head($i) . “\n”; #Print out header info
#print $pop->Body($i) . “\n”;
if($pop->Head($i) =~ m/Radio/) { # If the header contaisn the word Radio anwhere
print “Found Radio!\n”; # Tell me if found it
if($pop->Head($i) =~ m/(\d\d):(\d\d):\d\d/) { # If the header contains a date
print “Message Time: $1:$2\n”; # Print out the hour and minute
}
}
print “\n”;
}

———————–END CODE——————————

So far, this works perfectly. I can tell that the word radio is present. If it finds it, it will grab the time and print it. Now I just need to convert the time into a 12 hour format and save the hour, minute, and AM/PM setting in three variables. Once they are saved, I can set a cron job to run 30 minutes from the current time to call my bash script with the variables as arguments. The 30 minute delay is required because the playlist on the website does not get updated for anywhere between 10-30 minutes typically. I’m off to work on a time conversion function.

So here’s my quick and dirty (probably terribly innefficient) time conversion program.

—————————–CODE———————————

# Function to convert time to 12 hour time.
sub timeConvert {
($hour1) = @_; # set these variables to = inputs
print “timeConvert: $hour1\n”;
if ($hour1 =~ “00″) { # if it’s midnight
$hour2=”12″; # change from 00 to 12
$ap2=”AM”; # set to AM
return;
}
$hour1 += 0; # convert the string to int
if ($hour1 < 12) { # if its morning
$ap2=”AM”; # set to am
return;
}
if ($hour1 =~ 12) { # if its noon
$ap2=”PM”; # set to PM
return;
}
if ($hour1 > 12) { # if it’s after noon
print “Greater than 12\n”;
$ap2=”PM”; # set to pm

$hour2=$hour1 – 12; # convert to 12 hour format
return;
}
return;
}

———————-END CODE———————

The function takes in an hour value and performs all of those checks on it. Based on the time it will convert the hour value as necessary, and then set the AM/PM variable. My next step will be to get this Perl script to call my bash script as soon as it sees a Radio message. I will deal with setting the cron job later.

Well, that was easy. One simple command did the trick.

exec “playlist.bash $hour2 $2 $ap2″;

I added this into the if condition that checks for a time in the email header. This way, it won’t call the playlist script unless it finds a time and converts it first. I sent a new text message to my g-mail account and ran the script. Everything worked perfectly. The only problem is that the time now is 4:35PM and the song it returned to me was played at 4:16 PM. That’s the delay problem I was referring too before. Now I have to get the script to wait 30 minutes before checking for the song.

First I had to fix my system to have the correct time and date:

date 04301637

That did the trick, but I just remembered my bash script has a fatal flaw that I should fix now before I forget about it. If the song is played at the very beginning or end of the hour, the script does not take into consideration the fact that there are only 60 minutes in an hour. It could potentially try to look for a song played at 2:60, which obviously would not exist. I need to write a conversion function for the minute and hour for the bash script in this event. I’ll do that next.

I had to add a whole bunch of nested if statements in order to get the job done. When I say “had too” I really mean I’m not really sure the best way to do this and that’s the only way I could think of right now. It looks really messy but it gets the job done. The whole section looks like this:

————————–CODE——————————-

if [[ $artist = "" ]] #–If we didn’t find an artist
then #–Then increment the minute
m=`expr $m + 1`
if [[ $m = 60 ]] #–Obviously we can’t check 2:60 oclock
then
h=`expr $h + 1` #increment the hour and
m=00 # change the minute
if [[ $h = 12 ]] # might need to toggle AM PM
then
if [[ $a = "AM" ]]
then
a=”PM”
else
a=”AM”
fi
c=1
fi
fi
if [[ $h = 13 ]] # Now we also need to check for the hour being too high
then
h=1
fi
echo “time: $h:$m $a”
artist=`cat savedPage.html | grep “>$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`
if [[ $artist = "" ]] # if the artist still has not been found
then # then set the time back to normal
if [[ $c = 1 ]] # if we changed the am/pm setting
then # then reset it
a=$3
c=0
fi
h=$1
m=$2
fi
fi

while [[ $artist = "" ]] #–If we didn’t find an artist
do # then continue to subtract a minute till we find one
m=`expr $m – 1`
if [[ $m = -1 ]] #–Obviously we can’t check 2:-1 oclock
then
h=`expr $h – 1` #decrement the hour and
m=59 # change the minute
if [[ $h = 0 ]] # Need to change from 0:00 to 12:00
then
h=11
fi
if [[ $h = 11 ]] # this is where we might need to toggle AM/PM
then
if [[ $m = 59 ]]
then
if [[ $a = "AM" ]]
then
a=”PM”
else
a=”AM”
fi
fi
fi
fi
if [[ $h = 13 ]] # Now we also need to check for the hour being too high
then
h=1
fi
fi
if [[ $m -lt 10 ]] # the earlier correction stuff makes anything<0 look like: 12:1 instead of 12:01. We have to add that zero
then
m=”0$m”
fi
echo “time: $h:$m $a”
artist=`cat savedPage.html | grep “>$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`
done

————————-END CODE—————————-

Ah, god it hurts to look at. I really should switch this whole thing over to Perl. Why I don’t just quit now and do that I don’t know. At least it’s all set now.

OK, I think I’ve fixed that problem but I just discovered another one I had not even thought of before. If I try to search for the time 12:03, it MAY return a song that was actually played at 2:03 because it doesn’t check the value before that number. I’m going to have to fix my grep search string.

That turned out to be WAY easier than I expected. The way the playlist html code is set up, there is always a ‘>’ character right before the time value. All I had to do was add that to the beginning of my grep search. It now looks like this:

artist=`cat savedPage.html | grep “>$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`

Piece of cake. Now what’s left from here? The time delay. What is the best way for me to do this? I suppose I’ll have the perl script constantly running in the background, checking my e-mail every five minutes for a new message. If it finds the message, I’ll have it set a cron job to run 30 minutes from the current time. The cron job will call the bash script, which will then save the artist and song title to a file on my system. I also have to delete the messages after I’ve read them to prevent me from reading them over and over again.

I’m going to start with having the script set the cron job. According to this reference article cron takes 24 hour time. This should be easier for me considering the g-mail time comes in as a 24-hour time.

And apparently cron is used when you want a job to run more than once. That is, every day, week, month, etc. What I want to use is the “At”command. I found this nice reference for the at command. at also takes a 24 hour time, so I need to set up a new function in the perl script to come up with the future time, and set the at job.

Holy crap. So I just did some googling for the heck of it and discovered that I can actually just use the date command to get the date in the future. In my case I can just run:

date –date=’30 minutes”

This tells me the time 30 minutes from now. The at command wants to take a date in the form “mmddhhmm”. I should be able to just output the date command in that format and pipe it to the at command. This is going to be so much easier than I thought. I love Linux!

So I added a new perl function:

———————-CODE————————–

# Function to get the curent time and set an at job in 30 minutes
sub setJob {
my $future = `date –date=’30 minutes’ -t +%m%d%H%M`;
print “future= $future”;
exec “at -f ./playlist.bash -t $future”;
return;
}

——————-END CODE————————–

And I set it to be called where I used to have the perl script directly call the bash script. Now I just have to set the bash script to save all songs saved into a file. To do this, I deleted the end of the bash script where it printed the song to the screen and replaced it with this:

#——Save the artist and song——
date=`date +%m-%d-%Y` # det todays date so we can start a list for all songs found today
echo “$artist – $song” >> /media/radioLists/$date # save song in file

This creates a text file with the name being equal to todays date. That way I can have a list for every day I find a song. It saves the lists in my media directory which is shared via Samba.

I guess all that is left is to get the Perl script to check my mail every five minutes and set up GMail to delete the messages I have already read. I think the easiest way to do this would be to just set cron jobs. I can set up one cron job for every five minutes of an hour. Those jobs will run every hour, daily.

I also noticed that although I set up GMail to delete my messages once they are accessed with POP, it isn’t working. I added a line into my Perl script to set each Radio message to be deleted.

Now it’s testing time. I switched on the Radio and hear “Linkin Park’s – What I’ve done”. That was at 6:30. I sent a text message to my e-mail address while the song was playing. Now I have to wait until 6:35 to see if the cron job will actually start and if my Perl script will actually set the at job correctly. If all goes according to plan, I should basically be finished with this project.

The time has come. My Radio e-mail has dissapeared so that’s a good sign. The cron jobs are working. Now did the at job get set? No. There was a problem or two with the script. For one, it took me about 20 minutes to figure out that the message was actually being deleting before I got the time from it. I had to move the Delete command to a later spot in the Perl script. Second, the Perl script needed an absolute path to the bash script for the AT command. Once I fixed that, it all seems to work.

I turned on the radio at 7:37 and the song Psycho was playing by Puddle of Mudd. The script was delayed due to the problems I mentioned in the last paragraph, but it will run. I’m waiting for 7:19PM to find out if this thing will actually work. If yes, then I am done until I find a problem. Or if I decide to port the entire thing to Perl, which would be a really good idea. We’ll see…

Nope.  It didn’t work.  I can’t figure this out.  It look like cron actually runs the Perl script and the Perl script sets an at job.  I noticed that since I moved the pop->Delete(); command to a different place it’s stopped working, but the real problem is that the bash script doesn’t seem to be functioning properly.  The AT job starts, but then never finishes.  I can see all the Grep, Sed, Tail, etc processes using “ps -A” but they all say <Defunct>.  They keep disappearing and re-appearing.  I’m starting to wonder if the Perl script isn’t creating the AT job correctly for some reason.  Although, both scripts work fine if I run them myself…  MY friend Brian suggested that I give all commands in my bash script absolute paths.  I’ve tried that and it does the same thing.  The script works fine when I call it myself but not when the at process calls it.  I can’t figure this out.  I was thinking maybe I have to give Perl all of the absolute paths as well, but if it’s actually setting an at job than that couldn’t be the problem.  I’m really at a loss here.

Tomorrow I’ll probably have to start adding some kind of logging features.  Also, since Perl seems to be working fine maybe I’ll just start porting the Bash code over to Perl.  It couldn’t hurt I suppose…

Radio Playlist Work

April 29th, 2008

So, I was going to try to get the VU Meter Tie project going again, only I decided to put the LEDs on the buttons of my shirt instead of in a tie. Unfortunately I destroyed four surface mount LEDs while trying to solder wires or leads to them. I’m either going to need slightly bigger LEDs or some tiny wire. Out of frustration I’ve decided to come work on the Radio Playlist project instead.

Where I last left off, I now have a script that can retrieve the Artist and Song title of a song played on the Edge at a specified time. I would run the script like this:

./script2.bash 6:28\ PM

Running this exact command right now returns the result:

FINGER ELEVEN
Paralyzer

So, all is well in the script so far. Now everything gets trickier. For one, the website doesn’t update the songs in real time. When a song is played on the radio, it won’t show up on the list for around 10-30 minutes. 30 minutes is the maximum delay that I have noticed so far. This means that when the script receives a notice to check for the song, it will have to wait about 30 minutes before actually checking. My first reaction is to schedule a cron job to call the script 30 minutes after it receives the message from me. This means that I will actually need two scripts. One to check for the e-mails and set the cron job, and another to actually look up the song and save it in a list.

The next trick lies in the fact that my computer time is not synchronized with the radio station’s time. Therefore, my computer will see that I want a song that was being played at say, 3:14. The radio station’s playlist might say it was played at 3:12 or 3:16. My script has to have some way of finding the right time, without going too far and getting the wrong song. I think if the script first looks up the exact time on my computer and finds nothing, then it can increment by one minute and check again. It will go up a maximum of two minutes. If it finds nothing, than it will start subtracting one minute from my computer’s time until it finds a song. The first song it encounters will be considered the correct song. This is the first thing I will work on today.

So I have added a bit to the script and I’m beginning to think a different programming language would have been a better choice. Perl maybe? I’m not sure. I’m just going to keep on moving with bash until I feel i have to change environments. Here is the newer, revised version of the script:

———————CODE————————–

#Next 3 lines are temp for testing the script
#They take the input values and store them in variables (h=hour, m=minute, a=am/pm)

h=$1
m=$2
a=$3

echo “time: $h:$m”

#—–Get the page and save it to a file
curl http://www.mediabase.com/whatsong/whatsong.asp?var_s=075069068074045070077 > savedPage.html

#—–Get the Artist——-
artist=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`

if [[ $artist = "" ]] #–If we didn’t find an artist
then #–Then increment the minute
m=`expr $m + 1`
echo “time: $h:$m $a”
artist=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`
fi

while [[ $artist = "" ]] #–If we didn’t find an artist
do
m=`expr $m – 1`
echo “time: $h:$m $a”
artist=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/<td nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1`
done

#—–Get the Song———
song=`cat savedPage.html | grep “$h:$m $a” -A 1 | tail -n1 | sed ’s/D><td nowrap><span class=blackMain11px>/\n/’ | tail -n1 | sed ’s/<\/td>/\n/’ | head -n1`

#——Print the artist and song——
echo $artist
echo $song

——————–END CODE———————-

The first change I did was to take the input time and save the values into variables. I then use those variables to search for the time within the html code instead of the raw input values. This allows me to change the values easily if I need too.

The next addition is the “if” condition. This says that if the $artist variable is empty (we didn’t find anything for that time) then add one minute to the time and check THAT time.

The next change is the “while” loop. This loop checks to see if the $artist variable is blank again. If it is still blank, then it decrements the time by one minute and checks THAT time. It will continue to decrement and check the time until it finds a song. This works, but I think it MIGHT end up working better if I check one minute up, then one minute down, then two minutes up, then two minutes down, etc. This would allow me to find the closest song to the current time.

The last change I made was to have it save the artist and the song title into two variables. This was to make it easier to check them. At the end of the loop I just echo the variable values.

Now that this part of the script is working, it’s time to write a new script to check for an e-mail message. I’m thinking I’ll probably use pine, since it seems to be a popular console-based e-mail client.

So after trying to get pine installed for a while I had no luck. I kept needed dependencies and not knowing where to get them or how to install them. I ended up finding this little guide on installing alpine by using yum and rpmforge. I followed it and it seems to have worked. Now I just need to figure out how to set it up to check my GMail account.

I found this tutorial on how to set up alpine to read messages from Gmail. Not very difficult at all. Now that I can view my e-mail with alpine, I still have no idea how to use this in a script. I’m beginning to think that I can’t. It looks like there must be a user issuing commands to Alpine in such a way that doesn’t allow for scripting.

I found this perl script for checking an email account for unwanted messages and then deleting them. The script checks the subject line. If all it contains is the word “Document” then it attempts to delete the message. This is similar to what I want to do, only instead of just deleting the message, I want to trigger an event and then delete it.

OK it’s late and I suppose I’m done for the night.  I have tomorrow off, except for the last final exam I’ll ever have to take… ever.  So hopefully I can get this working tomorrow.

Radio Playlist is coming along famously

April 26th, 2008

I found some more time to add in the details of my progress on the radio playlist project. I’ve decided once again that the original information I included on that page should probably have been a blog post. I have removed it and replaced it with something more suitable. The text that used to be there is included at the end of this blog post just so I’ll have it, and also so I do not have to explain the whole thing over again. For now, here is the progress I have made so far:

The first thing I had to do was find a way to download the web page to my computer from the command line. Having used cURL before, it seemed like an obvious answer. The problem I ran into first, was that I didn’t know the URL of the actual playlist page. The way I normally got to it was by visiting the Edge’s website, clicking on the “Edge Music” button, and then selecting the “What song was that?” option. The URL for the page supposedly showed up in my address bar, but it was incorrect. I could not for the life of me figure out the URL to the playlist page. Then my friend gave me a great idea. Use Wireshark to see what exactly happens when I click on the link. That was the answer to this problem. Wireshark revealed that the URL to the playlist is:

http://www.mediabase.com/whatsong/whatsong.asp?var_s=
075069068074045070077

It appears that The Edge uses another company called MediaBase to handle the playlist for them. Regardless, I had the URL. Now it was time to get cURL to download the page. This is a piece of cake. The command is simply:

curl http://www.mediabase.com/whatsong/whatsong.asp?var_s=
075069068074045070077

Done. The code to the page is dumped to the screen. Now that I had a way to get the page locally, I needed a way to get rid of all the html code and just get the Artist and Title that corresponds to a specific time. I figured the best way to start was to use good old grep. I chose a random time that was available on the playlist and tried piping the curl command to it:

curl http://www.mediabase.com/whatsong/whatsong.asp?var_s=
075069068074045070077 | grep “11:30 AM”

It worked… almost. The line containing the time was returned to me, although it appears that the time for one song is actually included on the previous line. This means that when I find the line that contains the time for the song I want, I actually need to get the next line to see the artist and song title. The only way I could figure out how to get the line returned with grep was to run the command:

curl http://www.mediabase.com/whatsong/whatsong.asp?var_s=
075069068074045070077 | grep “11:30 AM” -A 1

The -A 1 argument tells grep to return the matching line, and one extra line after that. So now I had the artist and the title, but all this other html garbage as well. Since I didn’t need the first line that contained the time, I figured the next logical step was to get rid of it. I only wanted the second line returned by grep. How did I do it? Like this:

curl http://www.mediabase.com/whatsong/whatsong.asp?var_s=
075069068074045070077 | grep “11:30 AM” -A 1 | tail -n1

This command gets the web page, pipes the output to grep which returns the line containing the time, and the next line that contains the artist name and song title. Then it pipes those two lines of output to the tail command. The “tail -n1″ command shows the last “n” lines of whatever I pipe to it. In my case, n=1. Therefore, tail will cut out everything but the last line.

Now that I have only the line I need, I decided to use sed to parse out the information I wanted. I found this awesome tutorial for sed during an earlier project and it proved to be a huge help with this project as well. This part took me a while. I’m not very good with sed but eventually a found a solution that worked for me. I piped all the output from the first commands to this one:

sed ’s/<td nowrap><span class=blackMain11px>//’

This command finds the text “<td nowrap><span class=blackMain11px>” (which is just some code in the page) and deletes it. I then pipe that output to the command:

sed ’s/<\/td>/\n/’

This command finds the first part in the text that matches “</td>” and replaces it with the newline character (/n). Why did I do this? The first sed command removes everything before the artist name. The second sed command splits the one line into two lines. The first line will contain the artist name only. The second line contains all of the html code that I don’t care about. Now I can simply pipe that output to:

head -n1

The head command works opposite the tail command. “head -n1″ outputs the first ‘n’ lines of the input. In this case, n=1 so head outputs one line only. The end result is the artist name and nothing else! Now I just had to do something similar to retrieve the song name. The full command I used for this was:

cat savedPage.html | grep “$1″ -A 1 | tail -n1 | sed ’s/D><td
nowrap><span class=blackMain11px>/\n/’ | tail -n1 |
sed ’s/<\/td>/\n/’ | head -n1

I won’t bother explaining this whole thing because it is the exact same idea as the last command. The only difference is that I removed all of the text before the song title, and split the one line right after the song title. I now have a way to retrieve the Artist name and Song title of any song as long as I know the time the song was played. I then created a bash script to do all of this for me. The script code looks like this:

#!/bin/bash

#—-The next three lines are to be used for checking the time in a later revision—-
h=`date +%l`
m=`date +%M`
echo “time: $h:$m”

#–Get the web page and save in a file called “savedPage.html”
curl http://www.mediabase.com/whatsong/whatsong.asp?var_s=075069068074045070077 > savedPage.html

#—–Get the Artist——-
cat savedPage.html | grep “$1″ -A 1 | tail -n1 | sed ’s/<td
nowrap><span class=blackMain11px>//’ | sed ’s/<\/td>/\n/’ | head -n1

#—–Get the Song———
cat savedPage.html | grep “$1″ -A 1 | tail -n1 | sed ’s/D><td
nowrap><span class=blackMain11px>/\n/’ | tail -n1 | sed ’s/<\/td>/\n/’ | head -n1

This script will simply output the artist name on one line, and the song title on the other line. I saved the webpage to a file first to avoid having to retrieve it twice. This lowers the run time of the script.

That’s all I have for now. The next step should probably be to set up the script with a fudge factor. It is very unlikely that my computer is set to the exact same time as the playlist computer’s clock. This means that I will never have the exact time the song is played. The script must look at the time I listened, and find song with the closest time. Also, it takes about 20 minutes for the playlist to update after each song, so the script will have to hear my request, and then wait to retrieve the song. After that, I will have to set up the e-mail portion of the script. This is the part that will actually allow me to send a text to my computer.

So far I’m really happy with how fast this is coming along. I expect it to be finished very soon.

—————–Previously found in the project section——————-

I thought of this project a few days ago. I was driving in my car and they played this new song on the radio (The Edge 103.9). I loved it and wanted to find it on my computer when I got home to listen again, but I had missed the names of the song and band. This happens a lot while driving. Normally in that situation I will have to send a text message to myself of some lyrics that I remember so I can Google them later and try to find the song. Well this project aims to prevent this problem.

I want to write a script that will sit on my server and look for incoming e-mails with a specific subject line coming from my cell phone. When it finds one of these e-mails it will go to The Edge’s web site and look at what song is playing at, or around, that time. When it finds the artist and song title it will save that to a “playlist” of sorts. I can then come home later and look at the list to see all of the songs I want to find.

The reason this should work is because The Edge keeps a list of all the songs that they play. Every time they play a song, the list gets updated. I’m thinking I can use Curl to check the website and Bash to do most of the other scripting. Maybe I should use Perl instead to make it more portable? We’ll see. I’ve already started this project but I’ll post up my progress in a blog post later.