My. Debian.

The diary about a journey which has started with debian.org
  • rss
  • Home
  • About me
  • $/#
  • Report bad English
    • Changelog

Lock the screen and save the environment

June 20, 2009 | 12:13

If you lock the screen with Ctrl+Alt+L (in gnome), a screensaver starts and you’ll be asked a password next time you press a key. That’s a pretty nice thing if you getting a pause from a public PC but from the environment point of view it doesn’t change anything because your machine is going to consume the same power if you are sitting in front of it or not.
You can change the things by using a command to turn your monitor off :

$ sleep 10 && xset dpms force off

then you have 10 seconds for locking the screen in the usual way.

A more friendly way to accomplish the same task is to use a keybinding and a little script:

#!/bin/bash
gnome-screensaver-command -l
xset dpms force off

Now just run gconf-editor and edit /apps/metacity/keybinding_commands/command_1 and /apps/metacity/global_keybindings/run_command_1 or, if you are using compiz, run ccsm and edit the respective values in General Options. No, you can’t use Ctrl+Alt+L but you may choose Ctrl+Alt+K (which I happily use)

The above script is very simple but it’s far from being perfect. Here is something more complex:

#!/bin/bash

# lock the screen and start screensaver
gnome-screensaver-command -l

# store the string returned by "gnome-screensaver-command -q"
# We have to do this because the string is in your language
# which is not predictable :S
activestring=$(gnome-screensaver-command -q)

if [ -z "$activestring" ]; then
	echo "Error: null active string!" >&2;
	exit -1;
fi

sleep 1;

# if either the screensaver is active AND the user is not entering the password for unlocking the screen then we
# force the screen to remaing turned off.
# In addition, if the the paswword form is active the user has 30 seconds to type in the password. After this period the form
# is killed and we return to the screensaver + monitor switched off situation
while [ "$(gnome-screensaver-command -q)" = "$activestring" ]; do
	if ps aux | grep -q gnome-screensaver-[d]ialog; then
		sleep 30
		if [ "$(gnome-screensaver-command -q)" = "$activestring" ] && ps aux | grep -q gnome-screensaver-[d]ialog; then
			killall gnome-screensaver-dialog;
		else
			exit;
		fi
	fi
	xset dpms force off
	sleep 10;
done

The above script comes with two major features:

  • the monitor is turned off every 10 seconds if the screen is still locked (sometimes it just tune itself on :O )
  • to unlock the screen, the user has 30 seconds to insert a valid password. After that, the monitor is turned off again and the password prompt is killed
  • On my home PC, the above script allows me to save about 30W (~30% of the total power consumption) while I am away. Well, hibernation (suspend to disk) would be a lot better but often you simply can’t do that.

Comments
No Comments »
Categories
Gnome, bash, commands
Comments rss Comments rss
Trackback Trackback

Handling not-installed debian packages

June 15, 2009 | 16:05

If a package is installed, it’s very easy to get the list of its files:

$ dpkg -L $package

But how can you do the same if the package is not installed yet?
Well, as far as I know apt-cache & Co. can’t help you in this case. You can either download the whole package and list its content or get a browser and search in http://packages.debian.org

Fortunately, you can do it via console too by just using wget and sed:

$ wget -q http://packages.debian.org/lenny/all/$package/filelist -O- | sed -n '/<pre>/,/<\/pre>/ {s/^[^/]*//;/\/pre>/!p}'

If you are not using Lenny just substitute “lenny” in the above URL with the name of your suite (e.g. testing, unstable, squeeze, sid, etc.).

Another interesting task that I you may have to accomplish is: given a filename, show the package which contains it.

Again, if the package is installed, you just need dpkg:

$ dpkg -S /full/path/file

You can avoid the full path, but in most cases it will reduce the results list. For example, try

$ dpkg -S bash

and

$ dpkg -S /bin/bash

and see the difference.

As previously, however, if the package isn’t installed you can’t use apt-cache/aptitude.
Then, you have at least two choices: you can open up a browser (even console based like lynx or w3m) and search for the package, or you can run this simple command which just uses wget and sed:

$ wget -q "http://packages.debian.org/search?suite=stable&searchon=contents&keywords=$file" -O- | sed -n '/<table>/,/<\/table>/ { s/[[:space:]]*<a href="[^>]*>\([^<]*\)<\/a>/\1/p}'

where $file is the file whose package you are looking for. If you are not on a Debian Stable release, just change the word “stable” with one more suitable.

We can create a simple script that will execute the above commands in a more user friendly way:

#!/bin/bash

if [ $# -lt 2 ]; then
	echo -e "Usage:\t$0 searchfile file \n\t$0 list package"
	exit 1
fi

if [ "$1" = "list" ]; then
	wget -q http://packages.debian.org/lenny/all/$2/filelist -O- \
                | sed -n '/<pre>/,/<\/pre>/ {s/^[^/]*//;/\/pre>/!p}'
elif [ "$1" = "searchfile" ]; then
	wget -q "http://packages.debian.org/search?suite=stable&searchon=contents&keywords=$2" -O- \
		| sed -n '/<table>/,/<\/table>/ { s/[[:space:]]*<a href="[^>]*>\([^<]*\)<\/a>/\1/p}'
else
	echo "Error: invalid argument \"$1\"";
	exit 2
fi

Now, you can do a search with an easy command:

$ ./script.sh searchfile /bin/bash

or

$ ./script.sh list bash

Hope this helps!

Comments
No Comments »
Categories
Debian, commands
Comments rss Comments rss
Trackback Trackback

Know your system

December 2, 2008 | 12:52

What are the executables you daily use made of? Are they scripts or binaries?
What do developers use? Python? Java? Or perhaps Ruby?

It’s easy and funny to find it out, you just need a couple of bash lines with a pinch of awk:

#!/bin/bash

datafile=$(mktemp /tmp/bintypes.logXXX)

# scan /bin /sbin,/usr/bin,/usr/sbin
for file in /{,s}bin/*  /usr/{,s}bin/*; do
	rfile=$(readlink -f $file); 

# file is a tool which tells what a file contains
	[ -f $rfile ] && file "$rfile" >>"$datafile";
done

# count all occurrences with awk
# in some cases we have an 'a ' article in front of the description (e.g. "a python script text executable")
# we cut it away with sub()
awk -F "[:,]" '
{
	sub(/ a /," ",$2);
	a[$2]++
}
END{
	for (el in a)
		print a[el] "\t" el
}' "$datafile" | sort -n
rm "$datafile";

Here is my output:

1	 ASCII English text
1	 awk script text executable
1	 /bin/loadkeys script text executable
2	 setgid ELF 32-bit LSB shared object
4	 /usr/bin/ruby1.8 script text executable
5	 setuid setgid ELF 32-bit LSB executable
13	 ELF 32-bit LSB shared object
14	 setgid ELF 32-bit LSB executable
35	 setuid ELF 32-bit LSB executable
61	 Bourne-Again shell script text executable
119	 python script text executable
225	 perl script text executable
377	 POSIX shell script text executable
1748	 ELF 32-bit LSB executable

Hence, except for the binaries, the most common language used is Posix Shell scripting, followed by Perl and then Python. That lonely awk script is /usr/sbin/mksmbpasswd while the plain text file is just an error: it’s a shell script without the shebang (to whom it may concern, the file is /usr/bin/gnome-power-bugreport.sh). Do these results surprise you?

It must be said that of all the above tools, only 96 belong to Gnu coreutils, the others come from a really huge variety of programs (i.e. TeX, java, console.tools, etc.). In order to know which file belong to which package you have to modify the above script by using “dpkg -S $rfile” instead of file and change the field being counted by awk.
That’s all.

Comments
1 Comment »
Categories
awk, bash, system
Tags
coreutils, file, GNU
Comments rss Comments rss
Trackback Trackback

Recover one or more files from a usb external device

December 1, 2008 | 2:40

Let’s say that you unintentionally deleted a file from a block device (i.e. any common hard disk or USB flash pen). This issue is very nasty and, unfortunately, is something that you will probably face at least a tenth of times during your life. Hence, it’s advisable to be well prepared for when it comes.
How could you get back your data?
Read the rest of this entry »

Comments
1 Comment »
Categories
desktop, security
Tags
images, pictures, recover, usb device
Comments rss Comments rss
Trackback Trackback

Even to Odd converter

November 14, 2008 | 17:31

Here is an interesting problem: write a sed script to substitute every even number with the nearest odd number. I got this one from the list of the latest searches which bring the users to this site.
Let’s see how we can tackle this issue.
Read the rest of this entry »

Comments
No Comments »
Categories
awk, sed
Tags
arithmetics, even numbers, odd numbers
Comments rss Comments rss
Trackback Trackback

How to know which process is going to be the “next”

October 27, 2008 | 19:03

If the memory runs out the kernel just use an agent which will try to free some memory by killing those processes which are both old and fat (i.e. use a lot of memory for too long). Every process comes with a score which is constantly updated by the kernel. You can view the score by lurking in /proc/$PID/oom_score.

This article briefly describes how you can find out which process has the biggest change to being killed in a “out of memory” event and how to protect those which are required (e.g. ssh daemon for a remote machine) .
Read the rest of this entry »

Comments
No Comments »
Categories
bash, multimedia, networking, sed
Tags
kernel, memory, oom killer, proc filesystem
Comments rss Comments rss
Trackback Trackback

Simple and secure password protected archives

| 0:45

Many popular commercial applications offer the possibility to protect an archive with a password. Is there something similar for Gnu/Linux?
Well, first of all one could use one of the those commecial apps, but it’s not advisable for at least three reasons:

  • they come with restrictive licenses;
  • they use weak algorithms (which the use can’t change);
  • you have to install them by enabling unfree/multiverse repositories (at least rar);

So, what should one do? The answer is simple: just use tar + gpg which are respectively the best for archiving and the best for encrypting? This article briefly explains how you can put them together to create a password protected compressed archive.
Read the rest of this entry »

Comments
3 Comments »
Categories
desktop, scripting, security
Tags
asymmetric cryptography, gpg, rar, tar, zip
Comments rss Comments rss
Trackback Trackback

Keep track of the times a script is being run

October 16, 2008 | 16:47

The fact: you need to know how many times a given script has been run lately. This task is quite trivial if you keep the number in a given temporary file.

What I am going to show you instead, is how to store the number just directly in the same script.
Oh well, not that this is too hard to do but it’s a nice trick that may give you an idea of how self-extracting script do work (e.g. nvidia official drivers and common linux games are shipped with this type of container).

Let’s go to the core of the problem: in order to distinguish the code from the data, we need to put the first on top of the file and then tell the shell interpreter when it has to stop reading. How we can do this? With exit, of course :)

Now, the problem is reduced to a read-a-file-and-increment-a-number which is quite easy if you have familiarity with sed/awk et similia:

awk '/^[0-9]+$/{$0++} 1' $0

The above command will search in $0 (which is the current script in bash) for a line containing only a number (/^[0-9]+$/) and replaces it with its value increment by 1 unit ($0 in awk is the current line). The one (1) before the single quote, is there just to tell awk to print the current line.
With a temporary file we can write the changes in a safe place and then move it over the current script:

#!/bin/bash

tmpfile=$(mktemp)

awk '/^[0-9]+$/{$0++} 1' $0 >$tmpfile

mv $tmpfile $0
chmod +x $0
exit

0

Every time you run the above script, the number at the bottom line will be increased by one unit.

Hope this helps!

Comments
3 Comments »
Categories
awk, bash
Tags
awk, mktemp, self-extraction, self-modifying code, track
Comments rss Comments rss
Trackback Trackback

Check for duplicates

October 9, 2008 | 16:29

How to find those files that have different names but exactly the same content?

You could install the good fdupes or you could just reinvent the wheel with bash, md5sum and awk:

find path/ -type f | xargs md5sum | awk '{
	sub("[^/]*/","",$2);
	if (cache[$1])
		print "Found: "cache[$1],$2;
	else
		cache[$1]=$2
}'

path is where you want to search for duplicates. You can limit the search with the find maxdepth option.

Comments
1 Comment »
Categories
awk, bash
Tags
duplicate, files, md5, search
Comments rss Comments rss
Trackback Trackback

Reverse Dependence of a package

September 26, 2008 | 11:30

In Debian every package depends on others and thus every package has generally at least another one which depends on it. Every once in a while you could need to know why a given package is present in your Debian machine. Here is how:

Method 1: apt-cache

$ apt-cache rdepends package

Shows all the packages, no matter whether they are installed or not, which depends on package.

Method 2: aptitude

If you, like me, don’t use aptitude very often (i.e. never) you should first update its package db:

# aptitude update

Then:

$ aptitude search '~i~Dpackage'

This command shows all the installed packages which depend on package.

Method 3: hand-made bash script

#!/bin/bash

# usage: ./irdeps.sh package [,package]
# show the packages which depend on a package and are installed

if [ $# -lt 1 ]; then
	echo "Usage: $0 package"
	exit -1;
fi
while [ "x$1" != "x" ];
do
	echo "reverse dependencies for $1..."
	while read pack;
	do
		if grep -q "^Package: ${pack/|/}$" /var/lib/dpkg/status;
		then
			awk -v pack=${pack/|/} '
				/^Package: / && $2 == pack && flag==0{
					flag=1;next
				}
				flag==1 && /^Status: /{
					if ($4 == "installed")
						print pack;
					else
						exit;
				}' /var/lib/dpkg/status;
		fi
	done < <(apt-cache rdepends $1 | grep ^[[:space:]])
	shift;
done

This script does the same of aptitude in about the same time, but it relies upon dpkg only (and bash+awk of course).

References:

  • aptitudeSearch Term list
Comments
2 Comments »
Categories
package manager
Tags
apt-cache, apt-get, aptitude, awk, bash, dependencies, dpkg
Comments rss Comments rss
Trackback Trackback

« Previous Entries

Month activity

July 2009
M T W T F S S
« Jun    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Tags cloud

awk backup bash commands console dar database Debian desktop Gnome hardware hints Java kernel multimedia Mysql networking other php problemsolving programming samba scripting scripts security sed server ssh system trick Ubuntu unix Virtualization wordpress X11

Archives

  • June 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006

Recent Posts

  • Lock the screen and save the environment
  • Handling not-installed debian packages
  • Know your system
  • Recover one or more files from a usb external device
  • Even to Odd converter

Antipixels

  • bash
  • Debian Homepage
  • gimp
  • Play Ogg!
  • Rosetta@home antipixel

Spam Blocked

8,961 spam comments
blocked by
Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox
In order to display this page to you 48 queries were executed in about 1.318 seconds; what a waste of time!