Archive

Archive for the ‘Computer’ Category

Remap keys in Ubuntu

August 13, 2013 Leave a comment

A comfortable way to re-assign keyboard keys is by creating the file ~/.Xmodmap in the home directory, and putting line by line one re-assignment after the other. The typical format is ‘keycode <number> = <symbol>‘, but there are more possibilities (see ‘man xmodmap‘ or search for ‘Xmodmap’ for details). An example is:


keycode 112 = Home
keycode 110 = Prior
keycode 117 = End
keycode 115 = Next
keycode 134 = Delete

The number to the left identifies the pressed key itself. It can be determined by running ‘xev‘. The symbol to the right is the new action when the specified key is pressed without any modifier. You can define up to four space-separated symbols, where the second is applied if SHIFT is additionally pressed, the third if ALT, and the fourth if ALT+SHIFT. Use ‘NoSymbol’ if you do not want to let any action happen at all. For example:


keycode 16 = 7 slash NoSymbol backslash

This defines the mapping for key with keycode 16, which is usually the key labeled with ‘7’. The mapping defines to act like symbol ‘7’ when pressed without any modifier (note that the ‘7’ is the symbol for the number 7, but not a keycode as it would on the left). Further, SHIFT+7 gives slash, ALT+7 is ignored, and ALT+SHIFT+7 gives the backslash.

Changes are applied after logging out and in again.

Advertisements
Categories: Computer, System & Shell

Howto synchronize Tomboy with custom SSH server

January 9, 2012 8 comments

This Howto describes a way to set up client and server to let Tomboy synchronize its notes with your SSH-server ‘myserver.com’, and optionally to share these notes across multiple clients. It is written for Linux clients (ubuntu 11.04) and a Linux server (ubuntu 10.04) with root access via ‘ssh’. However, it should work out similarly for other Linux derivates.

The seven easy steps I present here are:

  1. Preliminaries
  2. Create remote user ‘tomboy’
  3. Test new user ‘tomboy’ and create data directory
  4. Create and install private/public key
  5. Configure Tomboy
  6. Disable password-login for user ‘tomboy’ at server
  7. Configure Tomboy on additional clients

And here we go with the details (for user ‘peter’)…

  1. (Preliminaries)
    At the client, the package ‘sshfs’ must be installed to allow Tomboy to connect via ssh at all:

    peter@myclient:~$sudo apt-get install sshfs
  2. (Create remote user ‘tomboy’)
    At the server, create a new user ‘tomboy’ including some home-directory.

    peter@myserver:~$sudo adduser tomboy

    In the interactive dialog, you might choose some silly password (we will disable it later anyway) and use defaults for all other values.

  3. (Test new user ‘tomboy’ and create data directory)
    Test the new user login by logging into the server as ‘tomboy’. Create some directory ‘data’ as root directory for all Tomboy-data:

    peter@myclient:~$ssh tomboy@myserver.com
    tomboy@myserver:~$mkdir data
  4. (Create and install private/public key)
    At the client, create a new private/public key pair by:

    peter@myclient:~$ssh-keygen

    In the interactive dialog, choose as file ‘/home/peter/tomboy@myserver.com’. The password can be left empty, if it is ok that anyone who has the private key file can access the server as tomboy.

    This generates two files: ‘/home/peter/.ssh/tomboy@myserver.com’ and ‘/home/peter/.ssh/tomboy@myserver.com.pub’, corresponding to the private and the public key, respectively. We have to install the public key on the server. We might do this manually (by copy&paste into remote’s ‘~/.ssh/authorized_keys’ or by using the following tool at the client:

    peter@myclient:~$ssh-copy-id -i ~/.ssh/tomboy@myserver.com.pub tomboy@myserver.com

    We use the silly password from above for this step.

  5. (Configure Tomboy)
    Now we can already configure Tomboy (Properties…) to use ssh:

    1. if necessary, activate Add-Ins -> Synchronization -> SSH Sync Server Add-In
    2. at the sync-tab, type the servername (‘myserver.com’), the user (‘tomboy’) and the directory (‘data’)
    3. save the settings. The first sync process should quit successfully
  6. (Disable password-login for user ‘tomboy’ at server)
    Finally (in fact, optionally), we might drop the ability to access ‘tomboy@myserver.com’ via ssh by just a password at all, and require to have the private key installed. Therefore, on the server, edit ‘/etc/ssh/sshd_config’ as root and add to its end (!) the following lines:

    Match User tomboy
        PasswordAuthentication no

    This disables the password authentication for the tomboy user only. Note that this should be placed at the end, because *all lines* started from ‘Match User tomboy’ are ignored for any other user than tomboy.
    Remotely load the changes to the ssh-server by:

    peter@myserver:~$sudo /etc/init.d/ssh reload

    On the client-side, you might have to re-login to let the changes take effect. You can check if a login is still possible, by:

    peter@myclient:~$ssh tomboy@myserver.com

    This should login as tomboy at the server without prompting for a password (if none was set for the private key). Or, just try to sync again with tomboy.

  7. (Configure Tomboy on additional clients)
    If you did not disable the password-login, you can enable Tomboy on additional clients to access your notes by performing the steps 1, 4 and 5 exactly as above. Otherwise, step 4 (creating a new private/public key pair on the additional client and adding its public key to the server for user ‘tomboy’) is no longer possible, since you can no longer login as ‘tomboy’ by password on the server to add the new public key.
    However, instead of step 4, you can just re-use the same private key on all additional clients by placing a copy of both the private key and the public key files ‘~/.ssh/tomboy@myserver.com[.pub]’ in the ‘~/.ssh’ directory of the additional client. You should make sure that the file permissons of the private key copy is still restricted to user-visibility (‘chmod 600 ~/.ssh/tomboy@myserver.com’), since otherwise it is not accepted as a private key. The copy of the public key beside the private key is needed to auto-detect this key on ssh-login.
    If you run into trouble, try manually adding this private key to the ssh-cache by ‘ssh-add ~/.ssh/tomboy@myserver.com’, and check the verbose output of ssh by ‘ssh tomboy@myserver.com -v’.

Final note:
Further, it might be a good idea to backup the ‘/home/tomboy/data’ directory on the server-side once in a while (to a place where user ‘tomboy’ has no privileges) by some cronjob.

Categories: Computer, Howto Tags: , , ,

Send Mail with Attachment from the Commandline

November 23, 2011 Leave a comment

Under Linux, you can send some file “test.pdf” attached to an email by typing:

uuencode test.pdf test.pdf | mailx -s "Title for my email" myname@mydomain.com

So what’s going on there? uuencode takes two input parameters: the file to read and the filename how it should appear at the mail. The file is converted to an ASCII-string and piped to mailx. The email is created with the given title (after the -subject flag) and then send to the given email address. The text-encoded file as piped from uuencode is attached to the body. Any modern email client is able to recognize this attachement.

The following script can be used to send a file including a subject to some fixed email. Some help is printed if it is called with the wrong number of parameters.

#!/bin/sh
if [ $# -ne 2 ]; then
        echo "sendfile <filename> \"title\""
        exit 1
fi
uuencode $1 $1 | mailx -s "$2" mymail@mydomain.com

Just save these lines in some file (e.g., sendfile) and make it executable (e.g., with chmod 755 sendfile).

Mounting an .iso file

October 30, 2011 Leave a comment

In Linux it is easy to access the content of some .iso-file directly without having to burn it to a CD/DVD. Just mount the .iso-file to some directory of your choice and you can browse it like any directory:

sudo mount <iso-file> <existing directory> -t iso9660 -o loop

The following script also prints some help if called with the wrong number of parameters and creates the directory if not yet existing:

#!/bin/sh

# print help if called with wrong parameter count
if [ $# -ne 2 ]; then
        echo "USAGE:   isomount <isofile> <location>"
        echo "INFO:    Mounts the specified .iso-file to the given"
        echo "         directory location (if it is not existing then"
        echo "         it is created with user-rights)"
        echo "EXAMPLE: isomount MyFile.iso /tmp/fooooo"
        exit 1
fi

# create directory if not existing and exit on error
if [ ! -d "$2" ]; then
        mkdir $2
        if [ $? -ne 0 ]; then
                exit 1
        fi
fi

# mount the iso-file
sudo mount $1 $2 -t iso9660 -o loop

Just save these lines in some file (e.g., isomount) and make it executable (e.g., with chmod 755 isomount).

Categories: Computer, Shell Scripts Tags: ,

Passing Values from the Commandline to PHP by GET/POST Method

May 16, 2011 6 comments

When using the commandline tool php-cli to execute a php-script, it is not possible to pass values to the script in the same way as if they were passed by get or post via http. However, I just found some easy solution to solve this problem. By this method, you can test your script from the commandline with just a very small change to it. The second solution even allows you to use the script without any change at all.

Note that with php-cli it is possible to pass arguments to the global php array $argv using the syntax:

$ php myscript.php arg1 arg2 ...

From within the script you can access these arguments using:

$arg1 = $argv[1];
$arg2 = $argv[2];
...

You can make use of this feature in one of the following two ways.

1.) If you can modify the script you want to call:
Begin the script with:

<?php
  if (!isset($_SERVER["HTTP_HOST"])) {
    // script is not interpreted due to some call via http, so it must be called from the commandline
    parse_str($argv[1], $_GET); // use $_POST instead if you want to
  }

  // the rest of your script remains unchanged here...

Call it from the commandline by:

$ php myscript.php 'name1=value2&name2=value2&...'

2.) If you cannot modify the script you want to call:
Create the following wrapper-script and save it as ‘wrap.php’:

<?php
	$parts = explode("?", $argv[1], 2);
	if (count($parts) == 2)
		parse_str($parts[1], $_GET); // use $_POST instead if you want to

	include($parts[0]);
?>

Perform the following call from the commandline to start ‘myscript.php’ with custom parameters:

$ php wrap.php 'myscript.php?name1=value2&name2=value2&...'

Both alternatives do also support url-encoded values, e.g. using ‘name=one%20two’ instead of ‘name=one two’.

Categories: Computer, PHP Tags: , , ,

How to restore crontab entries from scratch after accidentally typing ‘crontab -r’

February 9, 2011 10 comments

What happens if you type crontab -r instead of crontab -e ? Well, your crontab gets immediately deleted, without any further confirmation request. That is sub-optimal, especially because ‘r’ lays next to ‘e’, which is frequently used for editing the crontab (I really would appreciate it if crontab -r would ask the user, while crontab -R would not ask).

The first thing to try to undo the mistake, is to check whether there are some backups of the previously installed crontab (e.g. in /etc/cron* or some system-dependent backup) and then reinstall these.

If this is not the case (as usual), then there is no other way than creating a new crontab. But how do we know which entries were defined in it before deleting? Here, the system logs in /var/logs/ help us a lot, since it stores all crontab-calls of the long past and makes them easily to extract, since “/usr/sbin/cron” is part of the message (possibly in upper-case!). So, depending on your system, cat and grep all crontab lines from the messages* files into some output file, e.g.,

cat /var/logs/messages | grep -i "`which cron`" > /tmp/cron

where `which cron` is used to automatically be replaced by for example /usr/sbin/cron, depending on your system. You should also append all data from older messages, as usually stored as .gz or .bz archive. For .gz you might use:

gzip -d /var/logs/messages*.gz -c | grep -i "`which cron`" >> /tmp/cron

Now /tmp/cron is a file that contains all crontab-calls. You might use the less command to display its entries. Choose any line to handle at first, and Copy&Paste its <command> call (where <command> denotes the stuff in the bracktes after ‘CMD’) to a new textfile in which you rebuild your new crontab. To get the time interval for this command call, use the search function and have a look at the date/time when this call is repeated. Using less, just type /<command>, this will highlight all occurences. So, once you have extracted this call and recreated it using the appropriate * * * * * <command> syntax, you might delete all its occurences from /tmp/cron. You might do this by

cat /tmp/cron | grep -v "<command>" > /tmp/cron.out; mv /tmp/cron.out /tmp/cron

By repeating these steps, the file /tmp/cron gets smaller and smaller and you finally can be sure to have all crontab-calls handled that were made in the past since the first log entry.

Furthermore, since the file /tmp/cron decreases quite rapidly over time (there might be thousands of lines for just 10 crontab commands or so), you get the feeling to make progress, so its not that awful anymore that you called crontab -r before 🙂

After installing the newly created crontab, you should call the following command to manually backup your crontab:

crontab -l > ~/crontab.bak

Hopefully, this helps some of you out there that are in the same problematic situation…