Archive for the ‘linux’ Category

Batch conversion of images

Friday, December 15th, 2006

For batch image conversion, file renaming, etc, I usually use these kind of one-liners:

$ for i in `ls *.bmp` ; do echo convert $i ${i%.*}.png ;done
convert screendump_01.bmp screendump_01.png
convert screendump_02.bmp screendump_02.png
convert screendump_03.bmp screendump_03.png

When you’re satisfied with the result, have it executed by the shell:

$ for i in `ls *.bmp` ; do echo convert $i ${i%.*}.png ;done | sh

Scripting the Unscriptable

Wednesday, October 11th, 2006

I’ve been looking for different ways to send keystrokes, mouse movements and clicks to GUI applications. Under Windows, this is straight forward with AutoIt. The BASIC-like language allows you to read files, start applications, send keystrokes move the mouse etc. A nice feature is to wait for certain dialogs, for example:

; wait for tightvnc window
WinWaitActive("New TightVNC Connection")
; send hostname and port number
Send($hostname & ":" & $displaynumber)

This makes sure that the keystrokes actually end up in the right window. Mouse coordinates can be specified relative to the application window. Using these features allows you to write ‘reasonably’ robust code. But don’t expect your AutoIt script to run flawlessly on other PC’s. There are a lot of dependencies, like dialogs that look different in another version. Internationalization might cause the window title to read “Nieuwe TightVNC Verbinding” (Dutch) and the above code snippet will fail. Even user preferences like font sizes, window themes and screen resolution might result in unexpected behavior.

Anyway, it’s just fun to figure out which sequences to send to an application and let it process a batch of documents!
Ok, this works pretty nice under windows. But what about my favorite platform?
(more…)

Seder’s grab bag

Tuesday, October 3rd, 2006

I was already very pleased with the sed one-liners, but today I found grab bag. A fully loaded bag of sed scripts, tutorials, etc.

Why didn’t I check http://sed.sourceforge.net before???

addsshuser

Wednesday, September 27th, 2006

When creating user accounts, it is tempting to use a default password and ask the user to change it right after logging in the first time. Unless the user is forced to do this by means of a password policy, most users are just happy with the supplied password.
I know about a company (a hospital!) that uses one and the same password for new email accounts, allowing (ex)colleagues to read each others’ email!

A nice utility to generate random passwords is pwgen. The passwords are semi-random: random enough to be safe, but not too difficult to remember. By default, pwgen presents you a list from which you can choose:

$ pwgen
Vahvoi5u Da5ahrah Ogo0voh0 Fo3rieji aiw5zeuR qui1aiYo
thae5Chu Ha8bohSu oa2Eu3go JooQuia8 Aroh7Ohc UCoo4Hah
Woocee1i Ahnein6s di6aGeph fi5Ahngu ooRe8eeW Aish0sha
aikei3Fa Abohhai3 aeNgoo6o yiw2keiS eiyo5geH To5we6ey

… and 16 more lines. The man page explains that this is done so that you can pick a password without being afraid of someone ‘shoulder surfing’ for the chosen password.
Used from within a script (non-interactively), pwgen will generate a single password:

$ pwgen | wc
      1       1       9

Using pwgen to create a new user would involve a number of steps:

  1. run pwgen and write down the generated password
  2. create user account
  3. hand over the note with the password
  4. instruct the user how to log in and refer to the documentation about the service

You can argue about weather it is safe to write down the password, that’s another discussion.

I’ve written a little script that combines these steps.
The most interesting part of the script is the way that the generated password is fed in to the useradd command. The useradd option -p allows you to supply the encrypted password. “…, as returned by crypt(3)”, the man-page says.
Well, that brings up a problem. Isn’t there a binary or built-in equivalent? I would rather see crypt(1) being referenced… but that tool doesn’t exist.
This is where openssl comes to help! It can generate a passwd style hash for a given password. For example, to generate a hash for ‘mypassword’, execute:

$ openssl passwd -1 mypassword
$1$xBN626Gk$vsK88ODo4CfQ3pBx9Z1vD0

This looks like something that we can feed to useradd!!

The minimalist version of the script now looks like:

#!/bin/sh

# first argument is the username
USER=$1

# generate password
PASSWORD=`pwgen -cn -1`

# generate password hash
PW_HASH=`openssl passwd -1 ${PASSWORD}`

# create the user account
useradd -M -p ${PW_HASH} ${USER}

# display username and password
echo Account created:
echo Username: ${USER}
echo Password: ${PASSWORD}

The full script adds some ‘fancy features’ like checking the input argument and printing a receipt with the username and password and a link to the documentation.
I’m using the script to add accounts to a server that acts as a SSH gateway. There’s no need for home directories on this computer. By using the -M option for useradd, no home directory will be created.
Maybe I should write my next Nerdnote(tm) about setting up an SSH server to use as a secure gateway to a private network.

Vncshare

Wednesday, August 23rd, 2006

I just wrote vncshare, a script that allows you to temporary share your vnc session. It changes the vnc password to ‘helpme’ for 30 seconds. Then it gets reverted to the original one, giving your colleague a time window to login to your vnc session. The script makes use of the ‘trap’ command to make sure the password is always reverted when the script is aborted.

Combine PDFs

Tuesday, June 27th, 2006

Try the following Gostscript one-liner:

$ gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=combined.pdf article1.pdf article2.pdf notes.pdf

Typo3update

Sunday, June 25th, 2006

Typo3update is designed to directly update content to a Typo3 website from the command line. It works as a network service and can be used from any machine that can connect to the server running the Typo3 website. For example, quota overviews and cluster news information that is available from command line programs or text files can be updated to Typo3 using simple bash scripts. Typo3update takes the actual content from standard input. Additionally, it requies two command line options: uid and the header.
Have a look at the following example:

$ cat textfile | typo3update 9533 "About"

This will insert the contents of textfile into content element 9533 that has the heading “About”. The heading parameter is to double check the content element id. If the id doesn’t match the supplied heading, typo3update refuses to update (overwrite) the content. Instead, it writes a line to an error log file.
The content element should be created first from the Typo3 backend. To identify the uid of the content element in a certain page, just hover your mouse pointer above the content element icon. The uid will show up in a tool-tip:
Typo3 uid
(more…)

Redirecting stdout & stderr notes

Sunday, June 25th, 2006

Redirect stdout to stderr:

$ echo Oops, something went wrong >&2

Redirect stderr to /dev/null:

$ ls /home /qwerty 2> /dev/null
home:joe john

To get rid off all output:

$ ls /home /qwerty >/dev/null 2>/dev/null
# or
$ ls /home /qwerty >/dev/null 2>&1

Tar over ssh

Saturday, June 24th, 2006

Just a very short note. It’s not so difficult, but I keep on forgetting the syntax. Here it is:

me@localhost$ tar cf - . | \
    ssh user@otherhost 'cd dir; tar xf -'

Alienscan

Wednesday, June 14th, 2006

Today, I wrote a little bash scripts that scans the network for computers that are not registered in DNS. Reverse DNS to be more precise. It uses nmap to do a reverse DNS lookup for each computer it pings:

$ nmap -sPR -oG - 192.168.0.*

The result is filtered for failed DNS lookups:

$ nmap -sPR -oG - 192.168.0.* | grep "()"

Since all computers are contacted their mac address is known in the ARP (Address Resolution Protocol) table:

$ /sbin/arp | grep -w 192.168.0.35 | awk '{ print $3 }'

After looking up the MAC address, a table is presented with ip addresses and their mac addresses. This is the complete script.