How to check if a string is valid utf-8

Every day (at least) I’m facing a problem: how to check if a string is valid in utf-8 ?
So I wrote a little C program, that I put on my github. Just be aware that pure ASCII is valid UTF-8 and that’s not a bug: my program is checking if a string is valid utf-8, not if the string is in utf-8.

Enjoy :-)

Posted in Code | 3 Comments

emacs: standard input is not a tty

Did you ever tried something like :

$ find -name '*.c' | xargs emacs
$ grep -rl snmp . | xargs emacs

and got the error “emacs: standard input is not a tty” ?

That’s normal, as the stdin for emacs is here the pipe, not your tty, you need a workaround to leave the normal stdin to your emacs.

There are different approches, the one I used for a long time was a command substitution :

$ emacs $(find -name '*.c')

But other approches exists, redirecting /dev/tty to emacs’ stdin :

$ find -name '*.c' | xargs sh -c 'emacs "[email protected]" < /dev/tty' emacs

And if you are searching for a specific pattern with grep, you should want to jump directly to the right line using the ‘+line file’ syntax of emacs, and the shell substitution :

$ emacs $(grep -rn snmp services/ | sed -r 's/([^:]*):([0-9]*):.*/+\2 \1/g')

It’s good while having only one occurence of the pattern in each file, but if many occurences exists, the file is oppened only once, with a while you can open one emacs for each occurence of the searched pattern and the /dev/tty redirection :

$ grep -rn snmp services/ | sed -r 's/([^:]*):([0-9]*):.*/+\2 \1/g' | while read line file; do emacs $line $file < /dev/tty; done
Posted in Code | 2 Comments

Automating GNU screen startup

At work I use GNU screen with one window per server (ssh connection), and when I loose my screen, it takes minutes to rebuild the naming and the ssh connections …

So I searched and found a PHP version on Jon’s blog but I don’t like PHP and don’t want a PHP cli on my machine (even to start screen !)

So I rewrote it in bash (not sh, bash, to have brace expansion !)

Enjoy :

#!/bin/bash -f
SERVERS=$(echo {julien,root}@dev root@{www,sql}{1,2} root@media{1,2,3,4,5,6}
screen -dmS "$SCREEN_NAME"
    NUM=$((NUM + 1))
    screen -S "$SCREEN_NAME" -X screen
    if [ z"$SERVER" != z"" ]
        screen -S "$SCREEN_NAME" -p $NUM -X title "$SERVER"
        screen -S "$SCREEN_NAME" -p $NUM -X stuff "ssh $SERVER $(printf \\r)"
printf "Done,  now you can join your screen with :\n"
printf "$ screen -dr -S $SCREEN_NAME\n"

Don’t forgot to start your ssh-agent just before starting this script !

PS : If you don’t have a ssh-agent, you may want to remove the $(printf \\r) and press enter yourself.

Posted in Code | 2 Comments

ashttp: vt100 screen scraping exported over HTTP

Originally wrote for logtop I just wrote a vt100 screen scraper that listen to a port and serve the screen over HTTP.

Basically, you want a `top` (or logtop ;-) ) to be displayed in your website back office ? But top outputs in your terminal and you don’t know how to capture it ? Use ashttp, like this :

$ ashttp -p 8080 top

And then just open the port 8080 with an HTTP client and enjoy (typing some F5…) :

You can find the code on my github.

Posted in Code, Python, Sysadmin, unix | 3 Comments

squid: (101) Network is unreachable when DNS returns IPv6

If you have a Squid proxy configured on a machine that have no IPv6 connectivity, and try to reach an IPv6 compatible site, you squid will try to reach the site using IPv6 (I use squid 3.1.6 from Debian Squeeze) and will fail without even trying IPv4, displaying a beautiful : (101) Network is unreachable.

The only trick I found to force squid to use IPv4 (After disabling IPv6 on the interface, after disabling IPv6 in the kernel that leads to nothing better…) is :

tcp_outgoing_address [YOUR PUBLIC IP HERE]

And can enjoy again your plain old IPv4 network !

Posted in Sysadmin | 24 Comments