Sunday 31 August 2008

Install and use ClamAV for linux anti virus

Install Clamav with either Synaptic or apt-get/aptitude.

To update the definitions use:

sudo freshclam

scanning files is as simple then as calling clamscan with the -r or --recursive flag and a path, if the path is omitted then it will scan at the current path:

clamscan -r /home/subbass
or clamscan -r

If you would rather only see infected files then use the -i or --infected flag:

clamscan -ri /home/subbass
or clamscan -ri

If you wish to scan every file on the system you may have to run clamscan with sudo, as running without it clamscan can only read files the user running it can access.

sudo clamscan -ri /

This would check every file on the system recursively and report only infected files.

You can schedule clamscan using cron or for the odd one off scans using the "at" command, such as:
at 3:30 tomorrow
at>clamscan -i /home/user > mail user@example.com
at>
job 3 at 2005-04-28 03:30
This would perform the scan at 3:30am later that night and mail the results to the defined address. To add a regular cron job try the following:

nano crontab -e

Then enter the following line at the bottom of the file

00 00 * * * sudo clamscan -r /location_of_files_or_folders
Save the file and exit.

Use exiftran to transform jpeg images

Exiftran is a command line utility to transform digital image jpeg images. It can do lossless rotations like jpegtran, but unlike jpegtran it cares about the EXIF data: It can rotate images automatically by checking the exif orientation tag, it updates the exif informaton if needed (image dimension, orientation), it also rotates the exif thumbnail. It can process multiple images at once.

exiftran -ai *

This will transform all the images in a folder, rotating them to the correct orientation as defined by the exif data from the camera and replace the original, assuming your camera can sense and write the orientation into exif data.

exiftran -i9 *

Transform the image through 90degrees (to the right) and overwrite the original.

exiftran -i2

Transforms the image through 270degrees (to the left) and overwrite the original.

I have adapted this into two scripts in my nautilus-scripts folder "/home/subbass/.gnome2/nautilus-scripts/" you can make two scripts in this place using the following code and just changing the exiftran command to rotate left or right in each of the scripts.

#!/bin/sh
for arg; do
filetype=`file -i "$arg"`
if [ -n "`echo $filetype | grep -i '.jpg' `" ]; then

exiftran -i9 "$arg"

else
convert -rotate 90 "$arg" "$arg"
fi
done

The convert at the end is to convert other image formats without me requiring another set of scripts, feel free to omit this if desired. Always do a small test before setting any script to work on large numbers of images, I don't want to be held responsible if your exif data gets nuked.

Saturday 30 August 2008

Use rsync to and from copy files from a remote machine

Assuming the remote machine has SSH successfully installed and working then it is a simple case:

rsync -v -u -a --delete --rsh=ssh --stats username@192.168.0.100:/home/username/remotefile.txt

This will copy files from /home/username/remotefile.txt on the machine at 192.168.0.100

to copy files to that same machine then the command syntax is as follows:

rsync -v -u -a --delete --rsh=ssh --stats localfile.txt username@192.168.0.100:/home/username/

I will do a more in depth backup write up using rsync soon.

Tuesday 26 August 2008

rTorrent, light and fast bittorrent client.

I use two torrent clients almost every day, Transmission on my own box with a gnome desktop, and for a long time now rTorrent in a shell via SSH to a spare machine I have use of on a separate connection. Setting up rTorrent on an Ubuntu machine is what I am going to discuss here.

First step is to install the program, open a terminal and enter:

sudo apt-get install rtorrent

Or use Synaptic and search for 'rtorrent', install it. A basic config file is located /usr/share/doc/rtorrent/examples for you to tweak to your own liking, at its simplest you may want to change the download folder in it and the 'watch' folder. The watch folder is a place that rTorrent will monitor for *.torrent files coming into so it can automatically start, and when the download has completed and achieved a ratio you are happy with, you can just delete the torrent file from this folder to remove it from rTorrent. Dead simple stuff :]

cp /usr/share/doc/rtorrent/examples/rtorrent.rc ~/.rtorrent.rc

then edit this file with your prefered text editor, mine is usually nano, use gedit if you prefer a GUI editor:.

cd ~/
nano .rtorrent.rc

Now we can make the most basic of changes, first up is the download folder, look for the line:

# Default directory to save the downloaded torrents.

change the line after it to the folder you want to be your download folder and remove the # so you have something like this:

# Default directory to save the downloaded torrents.
directory = ~/torrents

This makes my download folder /torrents/ in my home, the default without editting is simply your home folder which you may be happy with.

Next lets tell it what folder to watch for torrent files. I have defined mine to be this same ~/torrents/ folder, you may prefer to leave it as your home folder. Either way find these lines just slightly down from the previous:

# Watch a directory for new torrents, and stop those that have been
# deleted.
schedule = watch_directory,5,5,load_start=~/torrents/*.torrent
schedule = untied_directory,5,5,stop_untied=

Yours will look very slightly different as I have shown how I changed mine to watch the ~/torrents/ folder. You should now save the file and close the editor, ctrl+o to save in nano if you used my prefered editor from above and don't know it, press enter to save wit hthe same name, then ctrl+x to close it.

You should now be free to start rTorrent with the command:

rtorrent

Not very impressive at first look is it, but download a torrent file to the watch folder and see rtorrent leap into action all by itself. You can adjust the upload and download speeds using the keys

a/s/dIncrease the upload throttle by 1/5/50 KB.
z/x/cDecrease the upload throttle by 1/5/50 KB.
A/S/DIncrease the download throttle by 1/5/50 KB.
Z/X/CDecrease the download throttle by 1/5/50 KB.


There are other keys to pause and resume torrents and many other facilities that you may require, for that I would point you at the User Guide page at http://libtorrent.rakshasa.no/wiki/RTorrentUserGuide


As I use this remotely I also run it inside a screen so I can safely detach from it and break the connection leaving it running on the remote machine, to do this enter in a terminal:

screen

then you will just see another command prompt on a clear terminal at which point enter:

rtorrent

rTorrent will start as normal but if you use the keys ctrl+a then d it will detach, that is it will drop you back at your original shell, leaving rtorrent running in the backgroun still. You can attach to it again with:

screen -r

You can then see rTorrent again to check your downloads or ratios. Check 'man screen' for more help on using screen, there are a *lot* of things you can do with it, and that might be a worthwhile post for me some time soon.

Monday 25 August 2008

Automated document scanning script

This article is taken from http://www.thelinuxblog.com/automated-scanning-with-the-shell/ as I need to scan several hundred photographs very soon. Thanks to Owen for correcting my link, I had linked to the wrong page.

I recently needed to scan a lot of images on my desktop PC. Unfortunately I am not the owner of an automatic document feed printer, and if I were it wouldn’t have helped this time because the documents I needed to scan were not feed able. XSANE is a great way to scan documents visually in Linux. Its not the easiest to use, but it has plenty of options. Part of the SANE package is scanimage, scanimage can be used from the shell.

The first thing that I did was a few test images with scanimage. I quickly found out that scanimage outputs in pnm format, and at a high resolution if the correct options are used. Once I found out the good options for my scanner (scanimage –resolution 400 > file.pnm) I wrote a quick shell script to scan up to 1000 times or until I don’t give the script any input. To do this, I used a combination of snippets that can be found in this blog column.

Here is a direct link to the script, and the shell script source below

#!/bin/bash
for i in `seq 1 1000`; do

#get input line
read inputline;

if [ $inputline ]; then

#Process Scanned Image in BG
echo Scanning Pg$i;
scanimage –resolution 400 > Pg$i.pnm;
echo Next;
else
exit
fi

done;

To use it all I do is execute the script, and I get to scan up to 1000 documents providing I type something after it prompts “Next”, and then hit enter. Once I was done scanning, I just hit enter to stop the script execution and then moved on to manipulating the images with the shell.

Hope this shell script scanning script is useful, if it is then drop me a comment, or if you have any suggestions or it was not at all helpful still drop me a comment.

Friday 22 August 2008

Recurse to find files and move them to a location

I just had to move a bunch of files out of individual sub-folders into the parent folder, time consuming to do that 50 or so times so a quick check of the "find" command and the solution is here:

find . -iname '*.avi' -exec mv {} /home/subbass \;

Command breakdown looks like this:

find .
find "here"
-iname '*.avi'
case insensitive name match on *.avi
-exec mv
execute the move command on the matches
{}
the match result from find
/home/subbass/
path to move the files to
\;
end of the -exec, each match runs as a new command

A quick command line, fairly simple syntax and it should prove a great time saver. You could easier of course cp or rm files instead of mv or many other possibilities.

Friday 15 August 2008

How to install an SSH server on your Ubuntu box

One of the nicer features of Linux is the ability to use Secure Shell (SSH) for encrypted remote access. Using SSH, you can control a remote computer without having physical access to the machine. The traffic generated by SSH is encrypted, and assuming you configure SSH correctly, quite secure as well. Here’s how to install the OpenSSH Server software with a basic security configuration on Ubuntu 8.04 Hardy Heron. (Bear in mind, of course, that you follow all advice at your own risk.)

First, open up a Terminal window, and use apt-get to download and install OpenSSH Server:

sudo apt-get install openssh-server

This will take a few moments, depending upon the speed of your computer and your Internet connection. Once the installation is finished, you’ll return to the Terminal. We’ll need to make a few changes to your sshd_config file in order to increase SSH’s security. (Note that it is always best practice to make a backup copy of a configuration file before editing it.) To edit your your sshd_config file, use the following command:

sudo gedit /etc/ssh/sshd_config

(Note that you can use your editor of choice instead; vi or emacs or whatever.)

Once you’re editing the file, we’ll need to change the following directives:

PermitRootLogin no

Never, ever give root access to SSH unless you have an extremely good reason for doing so. If an attacker happens to discern your root password, he or she will have total remote control over your system. By denying root login permission through SSH, you can provide an effective block to that danger.

The Port directive also specifies which TCP/IP port the SSH daemon uses to listen for SSH requests. The default port is 22, and you may want to change that, especially if your machine is accesible on the Internet. Many malware bots use port 22 for default SSH attack attempts, and by changing your port number you can stop at least some of the automated cracking attempts.

You may also want to change the AllowUsers directive, which specifies which users can access SSH. This can provide an additional layer of security by granting SSH access only to a few users. For instance, to restrict access to only the users joetest and billtest:

AllowUsers joetest billtest

Once you’ve finished changing your settings, save your changes to the sshd_config file, and restart the SSH daemon:

sudo /etc/init.d/ssh restart

You should now be able to SSH into your Ubuntu machine. You can do so from the Terminal on a Mac or another Linux machine, or by using Putty on a Windows machine.

Thursday 14 August 2008

X server gives error and won't load.

I got asked to look at one my parents PC's a couple days ago as it had 'locked up'. Upon investigation it hadn't locked up but the motherboard had fatally broken with the PS2 and USB ports left lifeless. I was able to SSH into it and the machine was just fine.

I wasn't able to easily get any data off the HD while in the case so popped it out and took it home, plugged it into another of my own PC's and booted it up. GDM failed to come up and I was greeted with the typical "X has failed to start" message and dumped to a prompt. Not a problem, I've been here before and we can fix that in a jiffy.

sudo dpkg-reconfigure xserver-xorg

Go through the various questions, most of which are pretty simple and you can pretty much take the defaults in my experience.

A quick reboot:

sudo shutdown -r now

or a faster restart of gdm (the login manager)

/etc/init.d/gdm restart

Everything should be fine when it comes back up.

Enable DVD playback and other 'restricted' extras.

This information is taken from the Ubuntu Documentation (here) and any copyright remains with them. This is here for my own use.

Playing Restricted Formats in Ubuntu.

Follow these steps to play most common multimedia formats, including MP3, DVD, Flash, Quicktime, WMA and WMV, including both standalone files and content embedded in web pages.

Using the GUI (Synaptic)

  • Go to ApplicationsAdd/Remove...

  • Set Show: to All available applications

  • Search for ubuntu-restricted-extras and install it. Note that there is also xubuntu-restricted-extras (for Xubuntu) and kubuntu-restricted-extras (for Kubuntu.)

Or open the Terminal (much faster), and execute the following command:

sudo apt-get install ubuntu-restricted-extras

Use convert to add a border to multiple images in the command line

Ok, so you you have taken a bunch of photos with your new camera and what do you know, some of them are half decent. I use this after I have copied photo's I want to print or photo's that I am sending to flickr or deviant art to a seperate folder as it really adds a finishing touch, I copy them to a seperate location so I know exactly which ones I am printing/uploading and don't omit any.

Now you could easily do this in The Gimp, load each image and enlarge the canvas followed by a fill and then resave the image. This is linux though and we can do things a heap simpler than that!
First install Imagemagick:

sudo apt-get install imagemagick


Or install it via Synaptic or your package manager.

Then use this simple script,

be aware that you should operate on copies as this does overwrite the original, as noted above I only do this on photo's I have copied for printing/uploading.


for img in `ls *.jpg`

do
convert -bordercolor white -border 50x50 $img $img
done

Easiest to pop it all into a bash script, or just close it up separating the commands with semicolons into a one liner eg.

for img in `ls *.jpg` ; do ; convert -bordercolor white -border 50x50 $img $img ; done

If your camera uses a different extension then adjust the .jpg accordingly.

Adjust "50x50" to whatever you feel adds a nicely sized border, and feel free to change the border colour (black for instance). If you want to get creative stack up the converts to add a thin black border before a larger white border or whatever you fancy.

Wednesday 13 August 2008

Unpack multiple zip files easily

So you have just completed a download and it is in multiple zip files, rather than unpack them in the gui one at a time try this.

Open a terminal and 'cd' to the folder containing the zips, then enter this command:

for FILE in *.zip; do unzip -jo $FILE; done

I would suggest having the zip files in a separate folder to anything else to avoid confusion over the files that get unpacked.