Wednesday, February 18, 2009

Thought for the day...

People that call it V I instead of "vie"...

...probably don't need a "vi quick reference guide/cheatsheet" anyway.

:o)

Tuesday, February 17, 2009

Add correct host key in /home/john/.ssh/known_hosts to get rid of this message.


john@hostname:~/.ssh$ ssh user@hostname
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for hostname has changed,
and the key for the according IP address 123.45.67.89
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10.
Please contact your system administrator.
Add correct host key in /home/john/.ssh/known_hosts to get rid of this message.
Offending key in /home/john/.ssh/known_hosts:9
RSA host key for hostname has changed and you have requested strict checking.
Host key verification failed.


The known_hosts file is now encrypted, which makes it harder to find the bad line...
but you will note the line that says:

Offending key in /home/john/.ssh/known_hosts:9

(where :9 means line 9)

If you comment out or remove the line that the error indicates, this should fix the problem.

Alternatively, you can remove an entry for a given host using:


$ ssh-keygen -R hostname


Or find it with:


$ ssh-keygent -F hostname

Thursday, February 12, 2009

find grep hack to show name and content...

For a long time I've done things like:


for FILE in $(find * -type f -exec grep -l "$STRING" {} \;)
do
echo -n "$FILE"
grep -n "$STRING" $FILE
done


to get both the filename and the string it's matching on because there's no "name plus match" flag for grep... you either have -l or -n but not together...

I had a brainwave just now...

If you give grep more than one file to grep in - it tells you which file it found the match in... so how about:


$ find * -type f -exec grep -n $STRING {} /dev/null \;
path/to/the/FILE:12:I found $STRING on line 12!


If you can't be bothered typing, it can be any valid filename instead of /dev/null - i.e.:


grep -n $STRING {} /
grep -n $STRING {} .
grep -n $STRING {} *


I just figured /dev/null should hopefully have the least overhead to stat.

Be careful you don't give it an invalid filename or you'll get an error for each file the find finds!


$ find * -type f -exec grep STRING {} no_such_file \;
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory

Tuesday, October 14, 2008

Holding Debian versions

Hold is a status flag which tells apt (or aptitude) not to automatically upgrade a package.
To hold a package, 'echo pkgname +hold|dpkg --set-selections'
or 'aptitude hold package'
or use = in aptitude's curses interface.
You can ignore a hold by using apt-get install foopkg; or by using ++ in aptitude's curses interface.
[Note that this is *NOT* the same as packages which have been "held back" for dependency reasons.]

Snapshots is an archive that contains all Debian packages uploaded since 2002,
including those removed from the official archives because they were very buggy, unusable, broken, vulnerable or in some way undistributable.
Much of 2004 was lost because of harddisk problems.
See http://snapshot.debian.net/ for more information.

e.g.
dpkg -i /var/cache/apt/archives/sun-java5-bin_1.5.0-14-3_i386.deb /var/cache/apt/archives/sun-java5-jre_1.5.0-14-3_all.deb
aptitude hold sun-java5-bin
aptitude hold sun-java5-jre

Thanks to #debian

Wednesday, May 28, 2008

Stitching images with ImageMagick

From: http://studio.imagemagick.org/pipermail/magick-users/2002-July/003925.html

Simplified:

If you want to join images side by side:


[ AA.jpg ][ AB.jpg ][ AC.jpg ]


use:


convert +append AA.jpg AB.jpg AC.jpg row_A.png


NB: If they're not photos, you'll want the output to be PNG so it's not lossy.

When you want to join the rows the +append becomes -append like so:


[ row_A.png ]
[ row_B.png ]
[ row_C.png ]



convert -append row_A.png row_B.png row_C.png all_rows.png

Tuesday, April 29, 2008

Converting seconds to Hours, Minutes & Seconds

It's amazing how many different approaches to doing this you can find on the web.

Some of them are really tortured and inefficient!

These are the tightest two I've found:


#!/usr/bin/perl
#
#
#
use warnings;
use strict;

my $time = time % 86400;

#
# http://www.perlmonks.org/?node_id=101511
#
printf "%02d:%02d:%02d\n",(gmtime $time)[2,1,0];

#
# http://www.perlmonks.org/?node_id=101548
#
printf "%02d:%02d:%02d\n", ($time/3600)%24, ($time/60)%60, $time%60;


To convert the other way, an easy way is to use POSIX mktime:


#!/usr/bin/perl

use warnings;
use strict;

use POSIX qw(mktime);

my $day = "2008-05-28";
my ($d_year, $d_mon, $d_mday) = split /-/, $day;

my $secs = mktime(0, 0, 0, $d_mday, $d_mon-1, $d_year-1900);

Wednesday, April 02, 2008

Showing a function in ksh

If you need to see the contents of a function in ksh...


$ typeset -f [FUNCTION_NAME]


if you do typeset -f by itself it will list all of the defined functions.

Monday, March 24, 2008

Sharing your X desktop via VNC

When getting set up to share my Linux X desktop, I found to my temporary confusion, that unlike vncserver on Windows, which of course shares your existing desktop, the vncserver program for X is designed to start a new instance of an X server (possibly even blind to the person on the machine) and share *that* rather sharing your existing desktop, which is probably what you usually need to do!

To share your existing X desktop via VNC requires another tool called x0vncserver.

As far as I know this is only available with vnc4 (realvnc) and not with tightvnc.

To share my desktop, the first thing I needed to do was to create a password file for the incoming user to authenticate against:


$ vncpasswd .vnc/passwd
Password:
Verify:


The .vnc directory didn't need to exist already, it creates it for you.


$ ll .vnc/
total 12
drwxr-xr-x 2 john john 4096 Mar 20 15:28 .
drwxr-xr-x 52 john john 4096 Mar 20 15:28 ..
-rw------- 1 john john 8 Mar 20 15:28 passwd


It only seems to use the first 8 characters, and then saves that as obfuscated binary data.

Once you have the password file you can launch x0vncserver against it:


$ x0vncserver PasswordFile=.vnc/passwd

Thu Mar 20 15:33:32 2008
main: XTest extension present - version 2.2
main: Listening on port 5900


and it is ready to accept incoming connections :o)

If the incoming user is using tightvnc or another client that does not support the vnc4 protocol extensions, then you need to tell it to run in vnc3.3 compatibility mode:


$ x0vncserver Protocol3.3 PasswordFile=.vnc/passwd


You can also get usage help from x0vncserver by running it with any args it doesn't understand:


$ x0vncserver .

Friday, March 07, 2008

suspend to disk on low battery...

I found out why my machine was shutting down at low battery and why I couldn't get it to suspend to disk instead...

Fixed now :o)

You need to look at these files:


/etc/powersave/battery


I changed these values:


BATTERY_WARNING="10"
BATTERY_LOW="5"
BATTERY_CRITICAL="0"


And in:


/etc/powersave/events


I now have these:


EVENT_BATTERY_LOW="notify"
EVENT_BATTERY_CRITICAL="notify john_suspend_to_disk"


NB: I put john in the name so it would stick out like a sore thumb when I do a diff if dpkg ever asks me about changed config files on an upgrade and I'll know it's my own modification, and so it didn't conflict with or get overwritten by a provided script.

I created my own script to handle this:


/usr/lib/powersave/scripts/john_suspend_to_disk


which just has:


#!/bin/sh
#
#
#

sleep 10

/usr/sbin/s2disk

#
# You could use:
# echo -n disk > /sys/power/state
#


And I installed uswsusp which provides /usr/sbin/s2disk

Oh... I also set up a shell function to protect it, because I found I accidentally scrolled back through my shell history and hit enter on s2disk a couple of times ;o)


# type s2disk
s2disk is a function


I put this in my .bashrc:


s2disk ()
{
echo -e "Confirm you want to suspend: \c";
read OK;
if [ "$OK" = "y" ]; then
/usr/sbin/s2disk;
fi
}


What I *would* also like to add is a notify when the battery reaches 100% so I know to unplug my charger - I understand it's much better for the longevity of your battery if you don't leave it plugged in on full charge all the time and instead allow it to do full flat-charged-flat power cycles. I have a couple of laptops with useless batteries due to them having been left plugged in and running 24x7 for months or years on end - my Linux boxes rarely get shut down.

In an ideal world I'd like some device that plugged in between the charger plug and the laptop which I could drive from the OS to physically break the power connection when the battery gets to 100% - and not to reconnect it again until it's back to 0% (yes, I find with my reasonably new battery I can actually keep running it quite a while after it reaches "0%" - obviously 0% isn't dead flat - it's an arbitrary level set to give you a safety margin and still work when your battery is getting a bit knackered due to age.)

Wednesday, March 05, 2008

Problems installing vmplayer on Debian

I downloaded VMware-player-2.0.2-59824.i386.rpm and converted it with alien.


# alien VMware-player-2.0.2-59824.i386.rpm --scripts


I had installed it before, then removed it to install vmware-server (because vmware-server complained that installing it caused file conflicts with vmplayer) and later removed vmware-server again and reinstalled vmplayer.

The bits left behind by vmware-server seem to break it...
The nice thing would be to find them and remove them, but I found a workaround.

When I first tried to install the player again I got error messages from some of the install scripts:


dpkg: error processing vmwareplayer_2.0.2-59825_i386.deb (--install):
subprocess pre-installation script returned error exit status 1
/var/lib/dpkg/tmp.ci/postrm: line 25: [: abort-install: integer expression expected
Errors were encountered while processing:
vmwareplayer_2.0.2-59825_i386.deb


So I backed up the deb file and built a copy of it with no scripts using:


# alien VMware-player-2.0.2-59824.i386.rpm


(and renamed that vmwareplayer_2.0.2-59825_i386.deb.no_scripts)

I was able to install it, but when I tried to run the player I found more hangovers of having had vmware-server installed:


$ vmplayer
Unable to load image-loading module: /build/mts/release/bora-59824/bora/build/release/ws/vmui/../libdir/libconf/lib/gtk-2.0/2.10.0/loaders/libpixbufloader-png.so: /build/mts/release/bora-59824/bora/build/release/ws/vmui/../libdir/libconf/lib/gtk-2.0/2.10.0/loaders/libpixbufloader-png.so: cannot open shared object file: No such file or directory


I made sure it was there:


$ locate libpixbufloader-png.so
/usr/lib/gtk-2.0/2.10.0/loaders/libpixbufloader-png.so


Eventually I found the solution was to run:


# /usr/bin/vmware-config.pl


but... that failed:


# /usr/bin/vmware-config.pl
Making sure services for VMware Player are stopped.

sh: /etc/init.d/vmware: No such file or directory
sh: /etc/init.d/vmware: No such file or directory
Unable to stop services for VMware Player

Execution aborted.



OK, so fake it:

Make a copy of /etc/init.d/vmware containing:


#!/bin/bash

echo $1


then:


# chmod +x /etc/init.d/vmware


Now run the config again:


# /usr/bin/vmware-config.pl
Making sure services for VMware Player are stopped.

status
stop

Configuring fallback GTK+ 2.4 libraries.

In which directory do you want to install the theme icons?
[/usr/share/icons]
...


and go through the rest of the config.

After this (if you have your correct compiler etc installed, you should find that vmplayer will start happily)

Once that works you will find you can now go back and over-install the copy of vmwareplayer_2.0.2-59825_i386.deb that DOES have scripts without it erroring, so you get your correct startup scripts etc...

Once you've reinstalled it you will again need to run


# /usr/bin/vmware-config.pl


one more time otherwise trying to run vmplayer will give you:


$ vmplayer
vmware is installed, but it has not been (correctly) configured
for this system. To (re-)configure it, invoke the following command:
/usr/bin/vmware-config.pl.