Wget an entire FTP folder from it’s index (RegExp Introduction)

4 12 2009

Hi, folks!

Just a couple of hours ago I was trying to download all the files in a folder on the OSUOSL FTP Slackware mirror with wget, and all I kept getting was the index.html file from the page, so I decided to write a little script to download any file linked in the index. I’m sure there are tools which can do this far more succinctly, but I thought this would be a good way to begin to explain the incredibly useful nature of regular expressions. Here’s how my script turned out…

for i in $(wget ftp://ftp.mirrorservice.org/sites/ftp.slackware.com/pub/slackware/slackware-13.0/source/a/tar/ -O - | grep "ftp://" | sed 's/^.*href=\"//g' | sed 's/\".*$//g'); do wget $i; done

Now let me break it down… The first command I wanted was one to download the index.html file and extract the necessary link data from it’s content. To download a file and then stream it’s contents to another command, use the wget syntax:

wget {URL} -O - | {COMMAND}

First of all I piped the file’s contents to grep, which ignores any line which does not contain the phrase “ftp://”. This will ensures that we are only working with lines which contain a hyperlink to a file, ignoring all the extraneous HTML tags. The next process was to remove any of the surrounding HTML from the links. A link in an HTML document will always be preceeded by <a href=". To remove this part, I used sed. There are other tools which would work in a similar manner, but I find sed to be a great way of learning regular expressions, and I find it's syntax to be very easy to understand. The command to remove anything up to and including the href=" is as follows:

sed 's/^.*href=\"//g'

To anybody who doesn’t understand regular expression syntax, this looks like a jumble of characters. I’ll explain briefly how it works… Sed’s syntax for basic search and replace is as follows:

sed 's/{REG EXP OR TEXT TO SEARCH FOR}/{TEXT TO REPLACE WITH/g'

The regexp to match in our example is ^.*href=\”

^ means “From the beginning of the line”.
.* is a wildcard, denoting absolutely any sequence of characters.
href=\” describes the exact text string we want to have as the final characters in the match. The \ is an escape character to force the ” to be treated as a character.

In our command, the second part of the sed command is empty. This means that any text which matches the regexp will just be removed. The regexp will match from the beginning of any line which contains href=” up to the “.

Now each line processed will read something along the lines of:

ftp://ftp.mirrorservice.org:21/sites/ftp.slackware.com/pub/slackware/slackware-13.0/source/a/tar/rmt.8.gz">rmt.8.gz
(2429
bytes)

The second use of sed will remove anything occurring after the filename. It reads like so:

sed 's/\".*$//g'

This is used in the same way as the previous use of sed. The regexp to match is \”.*$.

$ means “End of line”, so this matches everything from the first occurrence of ” up to the end of each line. The output should now be nothing but a list of links. The final part is to wrap the output in a loop, and hand each line to wget.

Anyway, I hope this has been informative, and I’ll no doubt post some more soon!

n00b





Grab a whole folder with wget

27 11 2009

Hey folks,

When needing to download a whole folder with wget, I’ve always run into problems with the mirror option, which is what everybody points towards when somebody asks how to achieve this. It creates massive unnecessary directory structures, leaving actually what you want hidden somewhere within. I found a command today, which circumvents this nonsense, and just downloads the contents of the folder you point it at…

wget -nd -nH -r http://www.site.dir/name/goes/here

The -nd can also be entered as –no-directories in verbose form. This stops the command creating the folder hierarchy on your local machine.

The -nH (–no-host-directories) stops the command from creating a folder labelled with the domain name at the top of the hierarchy (in the example above – www.site.dir).

Works a charm!

n00b





KDE & Firefox – Open Containing Folder?

26 11 2009

Hi all…

After having Slackware installed on my servers for a while, it began to get really annoying that I couldn’t open the containing folder of any downloads I’d done, and had to manually navigate with Konqueror, which can be quite time consuming with a complex directory structure. As with anything which doesn’t feel quite right with Linux… There’s a fix!

First of all make a script called something along the lines of “kfmexec.sh” in /usr/local/bin. Into it, type the following…

#!/bin/bash
/usr/bin/kfmclient exec $1
exit 0

Now go into Firefox’s about:config. If you don’t know what this is, I advise you to read up on it before going hacking around with it, as you could really badly break things by changing the wrong values.

You will need to add the following values of the stated type, with their values as shown:

network.protocol-handler.expose.file (boolean) true
network.protocol-handler.external.file (boolean) true
network.protocol-handler.app.file (string) /usr/local/bin/kfmexec.sh

Restart Firefox, and now when you press “Open Containing Folder” or try to open any item from the Downloads window, Firefox will pop up a little window which will ask you which program you want to use and whether this should be the default action.

Hope this saves you guys as much time as it’s saved me!

n00b





A milestone has been achieved!!!

20 11 2009

Good morning again!

0×0000 has reached 10,000 visitors! Whoohoo! Thank you to anybody who has popped past, and I can only hope that I’ve succeeded in helping you fix whatever problem it was you came here with :-) If anybody has any constructive criticism to supply regarding any of my work, feel absolutely free to leave me comments. It’s always good to know how I can improve what I am doing :-D

n00b





Bootsplash patch repository!

20 11 2009

Hi guys, just a quick, but hopefully very useful message… I’ve found a repository which holds Bootsplash patches for every 2.6 kernel currently in existence! No longer will you have to trawl the Internet for hours trying to find a suitable patch!

n00b





Disable Gnome-Panel on Ubuntu Karmic Koala

8 11 2009

Hey all. I’ve recently upgraded my home desktop to Ubuntu’s latest release, version 9.10 Karmic Koala. I’ve also spent some time messing with my desktop, and am now using AWN (Avant Window Navigator) as my main launcher/menu, so needed to stop Gnome Panel from starting on bootup.

This is done by running gconf-editor (either from a run dialog or from CLI), and navigating to Desktop –> Gnome –> Session –> Required-Components. Now you will have three values in the panel on the right. Change the value of “Panel” from “gnome-panel” to “” (blank). Note that in order for this to work, you’ll need to have another panel application (e.g. AWN) running and in your startup programs folder.

Just a quick one. Took me bloody ages to find this out, so hope this has made things easier for some of you!

n00b





Sending & Recieving SMS with a 3G device in linux

26 08 2009

Hi all!

Got something quite interesting (or at least I thought so!) for you today: Sending and recieving SMS with a 3G USB dongle and I also assume this will work with a PCMCIA/Express card, if possibly in a slightly different manner. If I get the chance to try it out I’ll update this post with info for them too.

It’s actually pretty simple. The only prerequisites are that you have the correct driver for your 3G device loaded, ie you have access to your 3G dongle via /dev entries, and that you have the ppp package installed, as this contains the chat program. I’d imagine if you’re already successfully using the 3G dongle, then you’ve already got this installed, though.

When you plug your 3G device into a USB slot, use the command dmesg to find out which /dev/ttyUSB slots have been used for the device. This information can be seen on lines which will read something along the lines of:

usb 3-2: GSM modem (1-port) converter now attached to ttyUSB2

Then you need to work out which of the ports is the command port. You can do this by using the command cat /dev/tts/USBn, where n is the number of the port you want to check. On the command port, but not the other(s), you will see messages coming through on regular intervals. They will read something like:

^BOOT:28385139,0,0,0,72

You may as well leave the terminal open watching the USB device as you attempt communication, as you will be able to see the data scroll past as you pass it to the device this way.

Something important to at least know, if not understand regarding SMS itself, is the difference between PDU and text mode, as this article will not go into the depth of all the encoding issues you’ll come across trying to deal with SMS in PDU mode.

The messages you will send to the device will all be in the same format. They are just basically a modem chat script all filled out into one command line. They will follow the format:

chat TIMEOUT x "" "Info to send to device" "OK" > /dev/tts/USBn

The USB device is referred to as /dev/tts/USBn, as /dev/ttyUSBn is just a symlink to /dev/tts/USBn.

For the ins and outs of the chat command, check out the man page, as it’s not particularly relevant to this article’s sphere of interest. I’m concentrating more on the specific AT commands used to control the device, as that’s what’s important, not the method of communication. You could just as well use a terminal emulator like minicom to achieve exactly the same result.

The only difference may be replacing x with a timeout value, n with the USB device number and the info to send to the device. For this example, I will use a standard TIMEOUT of 1 and I will assume your USB device is USB2.

Sending SMS

First of all, the simplest option is to put the device into text mode. This is done with the following command:
chat TIMEOUT 1 "" "AT+CMGF=1" "OK" > /dev/tts/USB2
The terminal on which you are running cat should come back wtih the result:

AT+CMGF=1
OK

If it doesn’t, then either you are not watching the correct device, or your USB dongle’s firmware does not support SMS.

The next step is to tell the modem what number to send the text message to. Note the escaped quotes in the following command. When nesting similar quotes in such a manner, we must tell the terminal to treat the inner quotes as plain text to avoid the command trying to process them as part of the command line. Replace the xxxxxxxxxxx with the telephone number you wish to send the SMS to.
chat TIMEOUT 1 "" "AT+CMGS=\"xxxxxxxxxxx\"" "OK" > /dev/tts/USB2

On your second terminal, you should see the response:

AT+CMGS="xxxxxxxxxxx"
>

This is a prompt to enter the SMS’s text in plaintext format. The command is simply:

chat TIMEOUT 1 "" "Message text goes here" "OK" > /dev/tts/USB2

To insert a new line of text, just repeat this command.

To finalise the message, normally you would press . To emulate this, we use the ^ character. This will cause the terminal to treat it as if you have pressed the CTRL key followed by the next character. The command reads as follows:

chat TIMEOUT 1 "" "^Z" "OK" > /dev/tts/USB2

After a couple of seconds, your watching terminal should respond with:

+CMGS: n

n will be a number corresponding to the number of sent messages from the device.

You should now have the text message arrive at the destination telephone number.

Simples.

Receiving SMS

First step, as with sending SMS, is to put the device into text mode. This is exactly the same command used before:

chat TIMEOUT 1 "" "AT+CMGF=1" "OK" > /dev/tts/USB2

As an optional step, you can list all of the SMS stored on the SIM like so:

chat TIMEOUT 1 "" "AT+CMGL" "OK" > /dev/tts/USB2

The only real requirement for reading SMS, however, is to choose the index number of the SMS you wish to read. This is done with this command:

chat TIMEOUT 1 "" "AT+CMGR=n" "OK" > /dev/tts/USB2

In order to delete a SMS, should you so wish, it is done via a very similar command:

chat TIMEOUT 1 "" "AT+CMGD=n" "OK" > /dev/tts/USB2

That’s all I’m gonna talk you through, but there are countless resources online for finding out what some other AT commands are, and it’s quite interesting just firing them at the device to see what the response is, and I encourage you to have a mess about… Be careful when messing around with the SIM PIN codes, though, and it’s not my fault if you lock yourself out of your kit!

Hope I’ve been of use!

As always… Any questions/mistakes I’ve made, just leave a comment.

n00b





Resizing a VirtualBox HDD

28 04 2009

Today I came up with a problem involving a VirtualBox VM which had run out of space. “Damn” I thought, as I read so many postings by so many nay-sayers telling me that it “Just Can’t Be Done”.

C’mon guys… There’s almost no such thing as can’t with the plethora of useful gadgetry that the world of FOSS has brought us.

A slightly more extensive search turned up this site. Exactly what I was looking for.

What I’m interested in finding out is if this will work on a real HDD… This could fix all of my pain-in-my-ass HDD upgrade problems if I can just clone a disk into a larger partition! Infinitely expandable storage!

I’ll post here when I get a chance to try it.

n00b

UPDATE 08/05/09

Nick Heppleston, the author of the original article, has confirmed that using this method to clone data from a smaller partition to a larger partition on either the same disk or a separate disk works! This is going to make my Windows administration a lot easier, that’s for certain.

Thanks Nick!





Nifty RAM Dump/Sort script

24 04 2009

Hello again! Second post in one day! Whooo! Just a quick one though because I want to go home! It’s 5 o’clock!

I’ve been charged recently with helping to track down somebody else’s (grr!) memory leak in a piece of software. The program was eating about 30% of our server’s 2Gb of RAM. It seemed to make sense to do some kind of RAM dump.

As with most things linux oriented, as soon as you know how to access the relevant doohickies, the entire thing’s a piece of cake, so I wrote a little script so I could do the entire process again. It really is simple, but it’s such a useful little doobrie that I had to post it.

WARNING: I have not written any error checking into this, so you may want to write some break points or something into it before running.

Stage 1: The script frees any unused memory by synchronising any loose data then passing a number to /proc/sys/vm/drop_caches.

Stage 2: It then uses dd as root to take a byte-for-byte copy of the current contents of the RAM from /dev/mem.

Stage 3: It then parses any ascii characters from it and passes it through various sorting procedures to come up with a list of strings found in the RAM sorted from least found to most commonly found.

Anyway… Here’s the script (special thanks to WordPress for utterly destroying any semblance of formatting):

#!/bin/bash
IMGFILE="ramdump-`date +%d%m%y`.img"
TXTFILE="ramdump-`date +%d%m%y`.txt"
echo "************************************************"
echo "* n00bsys0p's RAM dumper and sorter *"
echo "* written 04/2009 *"
echo "* Use it for whatever the hell you want *"
echo "* and don't blame me if you ruin your *"
echo "* computer by being a retard *"
echo "************************************************"
echo "Stage 1 (Freeing unused RAM). Requires root privileges."
if [ $UID == 0 ]; then
echo "Already root, continuing"
fi
su -c "sync; echo 3 > /proc/sys/vm/drop_caches"
echo "Stage 2 (RAM dump). Also requires root privileges..."
if [ $UID == 0 ]; then
echo "Already root, continuing"
fi
su -c "dd if=/dev/mem of=$IMGFILE && chmod a+rw $IMGFILE"
echo -e "\n\
RAM Dumped to $IMGFILE.\n\
Stage 3 (Sort $IMGFILE). This may take a very long time..."
strings $IMGFILE | sort -fd | uniq -c | sort -n > $TXTFILE
echo -e "\n\
RAM dump sorted in $TXTFILE.\n\
Items are listed and numbered by order of occurrence."

Hope it’s useful to someone.

n00b





Pirate Bay Justice Scandal

24 04 2009

Hi all!

I’m not usually one for speculation, but this is just disgusting… Check this link.

It was bound to happen, though. Don’t know why they even bothered with a trial. May as well have sent them straight to Guantanamo.

n00b