Τετάρτη 16 Ιουλίου 2008

Linux Media Server

Nowadays, many people may have an old PC around that they don't really use. One of the things that you can do with it is to convert it into a "media server". Doing this, may provide you with a platform from where you can listen to your music collection remotely, download staff etc.

In this article I will first list what I believe that we can add to our media server. I really want to see your comments regarding other things that you would like to see on our media server. Then I will go on to describe how we can actually set one up.

The basics:
The OS that we will use on our media server will be "Linux". More specifically I will use Ubuntu 8.04LTS as this currently my favorite distro. However, note that I will be using the Server edition (meaning no GUI).

The initial content of the media server (please add to this list using the comments):
  1. Ability to stream media files over the network - Jinzora
  2. Ability to download torrents with a nice web interface - rTorrent & rtgui
  3. Ability to share files over the network between Linux and Windows machines - SAMBA
  4. Ability to remotely manage every aspect of our media server - Webmin
  5. Manage Remotely databases - phpAdmin
  6. ...
  7. ...

Τρίτη 15 Ιουλίου 2008

How to get line by line read of "ls" in bash

The problem:

I wanted to write a bash script that will read the output of 'ls' in a directory and rename each file that matched a specific pattern. However I soon found out that using

for line in ls

was no good as it returned the output word by word.

The solution:

The problem was solved after a bit of googling. The code that I needed was:
ls | while read lineVar
do
...
...
done
What this does:

1. ls pipes its output into "while read" which read line by line and stores each line into $lineVar (you don't need the dollar initially).
2. In the while/do loop I can do the pattern matching using sed and renaming.

The full script (currently with no terminal input - it's all hardcoded):
#!/bin/bash

ls | while read lineVar
do
echo OLD: $lineVar
new=`echo $lineVar | sed -e 's/^.....//g'`
echo Replacing $lineVar with $new
mv "$lineVar" "$new"
done
Hope this helps

Παρασκευή 11 Ιουλίου 2008

How to block a specific IP or Hostname from accessing your web-site using htaccess

Recently I figured out that someone was accessing repeatedly a specific url of my web-site causing a huge amount of traffic which finally resulted to a warning from my provider for exceeding the available to me bandwidth. So, I had to block that someone.

There were two steps involved in this process:
  1. Identify the offender
  2. Block the offender
Identifying the offender was simple using the server logs which are being analyzed by webalizer (but any other similar tool would do).

Having identified the offender (I had the offender's host name) I had to ban it from the site.

My provider does not give me access to a httpd.conf (I am using shared hosting) so I had to use .htaccess file in the webroot of my site. Below are the steps I followed:

  1. Create a .htaccess file in the webroot of your site (usually in /httpdocs)
  2. In the .htaccess file add the following lines:
order allow,deny
deny from hostname.com
deny from xxx.xxx.xxx.xxx
allow from all
In the above replace the values in italics with the actual that you want to ban (tip: to get the ip address you can just ping the hostname and will resolve to the ip address)

Finally save the file and you should be ready.