[ #235 ] Converting ssh2 public keys to openssh Permalink

SSH, Shell Added about two months ago

Sometimes you will have to convert a public key generated by PuttyGen for use on *nix. Just use ssh-keygen:

ssh-keygen -i -f sshv2.pub > id_dsa.pub



[ #234 ] Vim comment hints Permalink

Vim, Python Added about two months ago

If you need vim to interpret a file in a specific way (or set some values for a specific file only), you can put comment hints in the file.

For example, if you have a Python file that doesn't have a .py extension and also doesn't have a #! line indicating that it is indeed Python, then vim will not turn on the correct syntax highlighting for your file (because it thinks it is just plain text.) To force Python syntax highlighting, place a comment hint at the bottom of your file like this:

def eggs():
    pass
# vim: syntax=python



[ #233 ] Context managers in Perl Permalink

Perl Added about two months ago

Let's try to implement something like Python's with statement in Perl.

First, let's look at Python. Python lets you do something like this:

class controlled_execution:
    def __enter__(self):
        set things up
        return thing
    def __exit__(self, type, value, traceback):
        tear things down

with controlled_execution() as thing:
     some code

(code example stolen from effbot)

The idea is that when the with block exits, the __exit__ cleanup is automatically called. This is nice for dealing with files, for example:

 with f = open("somefile"):
    f.read()

... where the implicit __exit__ code closes the file for us.

Can we do this in Perl? Sure. We just don't have the luxury of using nicely named __enter__ and __exit__ functions. Here is an example:

package Dir;

use strict;
use warnings;

use Cwd;

sub cd {
    my $dir = shift;
    my $code = shift;
    my $origdir = getcwd();
    chdir($dir);
    $code->();
    chdir($origdir);
}

1;

Now the script that utilises it:

#!/usr/bin/env perl

use strict;
use warnings;

use Dir;

Dir::cd "..", sub {
    my @files = glob("*");
    print join("\n", @files);
};

This context manager is for "cd", which chages directory, runs all the code in the block, and as it exits it simply changes back to the original directory it was in. There you go! Simple context managers in Perl.




[ #232 ] Dish rotation Permalink

Python Added about two months ago

James over at Prog21 has an article on calculating the shortest rotation (in degrees) for a satellite dish - his point being that there is a case or two that he didn't immediately think about (i.e. that it's harder than it sounds.)

Here is my Python version of his function (with doc tests):

def angle_diff(begin, end):
    """
    >>> angle_diff(0, 50)
    50
    >>> angle_diff(37, 38)
    1
    >>> angle_diff(200, 10)
    170
    >>> angle_diff(50, 20)
    -30
    >>> angle_diff(0, 270)
    -90
    >>> angle_diff(270, 0)
    90
    >>> angle_diff(100, 100)
    0
    >>> angle_diff(0, 0)
    0
    """
    easyway = end - begin
    if abs(easyway) > 180:
        return (360 - abs(easyway)) * cmp(0, easyway)
        # get the shorter way ^
        #           in the opposite direction ^
    return easyway

Testing:

$ nosetests --with-doctest ad.py 
.
----------------------------------------------------------------------
Ran 1 test in 0.003s

OK



[ #231 ] Git - fixing commit user Permalink

Git Added about two months ago

If you just committed a change as the incorrect user, as long as you haven't pushed your branch you can do this:

$ git config user.name "Correct name here"
$ git config user.email "Correct email here"
$ git commit --amend --reset-user



[ #230 ] apt stuff Permalink

apt, Linux Added about two months ago

Sometimes I find yourself in a situation where I need to have specific versions of packages installed - perhaps from upstream source errors, untested experimental sources in your sources.list or some kind of local corruption. Here are some apt tricks help (this is not a step-by-step tutorial, just a grab-bag of commands that I sometimes forget about:)

$ apt-show-versions -p vim-common
$ apt-cache policy vim-common
$ sudo apt-get install vim-common=2:7.2.330-1ubuntu3



[ #229 ] Using shell variables in AWK Permalink

Shell Added about two months ago and last edited about two months ago

If you have a shell variable that has been exported (i.e. an environment variable), you can access it in awk like so:

awk 'BEGIN { print ENVIRON["HOME"] }'

However, say you have a non-exported variable like so:

X=wassup

... and you want to use your X variable in an awk script, you need to do some fancy quoting like so:

awk 'BEGIN { print "'$X'" }'

Also, here is an alternative that doesn't involve crazy quoting:

awk -v X=$X 'BEGIN { print X }'



[ #228 ] Linux - Too many open files Permalink

Linux, Shell Added about two months ago

If you get the error:

Too many open files

Then your file-max limit is probably set too low. Edit /etc/sysctl.conf to ensure:

fs.file-max = 102400

... (or whatever larger number you want) and then run:

# sysctl -p

to apply the changes.




[ #227 ] Tell gvim to save and quit... remotely Permalink

Vim Added less than a year ago

If you have a gvim session open on your desktop, and you later have to ssh in to your desktop to edit the same file, you'll get the familiar message that the file you are editing is open elsewhere and you can save, abort, etc etc.

Do this to safely save and quit that session first:

$ vim --serverlist
GVIM
$ vim --servername GVIM --remote-send '<C-\><C-N>:wqa<CR>'

... and now you can open the file up for editing safely from your ssh session.




[ #226 ] Vim - automatically remove whitespace at EOL Permalink

Vim Added less than a year ago

To automatically remove whitespace at line end when you save:

autocmd BufWritePre * :%s/\s\+$//e



Older Posts ... Newer Posts

Colophon

Django Python 960.gs Git Vim NetBSD Nginx

The Author

This is the blog of Brad Willis, a software engineer living in Brisbane.

Meta

Help
Latest entries

*BSD Agile Apache Apple apt Athletics Best-Practice Censorship Chrome Comedy Cool Crosswords Deployment Django English Exim Firefox Git Hardcore Health irssi Javascript Jira Languages Linux Makefile Mathematics Mobile Broadband Mutt MySQL NetBSD nginx Nokia OpenVZ OSX Perl Postfix Privacy Python Rant Requirements rsync Ruby Shell Slackware SQL SQLite SSH Standards Subversion Television Testing ThisBlog Vim VMWare (Fusion) VPN X zsh

Recent Entries

gvim - Always open new files as new tabs
crontab - escape % (percentage)
OSX Google Chrome - start in incognito mode
SQLite date arithmetic
Postfix - delete message in mailq
Checking for exceptions in doctests
Homer's Curling Speech
retry in Python
Vim Makefile tabs
Centos (or RH) IPTables
Converting ssh2 public keys to openssh
Vim comment hints
Context managers in Perl
Dish rotation
Git - fixing commit user
apt stuff
Using shell variables in AWK
Linux - Too many open files
Tell gvim to save and quit... remotely
Vim - automatically remove whitespace at EOL
Python - relative paths from within modules
TV Aspect Ratios
Git - Which commits are in your branch only?
Subversion setup cheat sheet
Force detach a screen session
Modify sudo's use of environment variables
Install all Perl modules
Mutt - delete old messages
OpenVZ VPS and swap space
fail2ban on NetBSD for ssh
NetBSD - Using sup
Python - testing for a sys.exit
Python Best Practice Link Dump
Python script names
Perl - Using an expensive module
Speed of git clone
Perl Modules with Custom Prefix
Perl: tr vs. s
Brilliant sysadmin Reference
Why is GRUB better than LILO?

Links

ChoppingBoard, DaveMisc, Project365, RageQuit