Pages

Thursday, June 24, 2010

Create a bridge (software switch)

To expedite the setup of a vmhost, where vmguests can be installed and initialized, I created two projects to facilitate the creation of bridges on vmhosts:

git clone git://github.com/Buggaboo/slackware-kvm-scripts.git
git clone git://github.com/Buggaboo/ubuntu-kvm-scripts.git

The ubuntu one works on ubuntu and all debian-based systems. The slackware one works on all slackware -based systems. The can be fetched by git cloning as described above. To setup both, you need the binaries brctl and tunctl. Brctl and tunctl.

Read the README file and have fun!

Tuesday, June 22, 2010

Make ubuntu's screen play nice with nano shortcuts

I use nano a lot either for programming or editing configuration files on linux. At work we use both ubuntu servers for vmguests and vmhosts.

The main reason I use nano is because they have a preconfigured shortcut-key on your keyboard to save your current document: F3.

You will not catch me hitting esc,colon,w; escape to non-insert mode; :w to write from edits on memory to file in vim. Nor will I do whatever they do in emacs.

I'd rather gain mastery of the language, than mastery of an editor, with my limited time.

Since I have to do stuff simultaneously, I use screen a lot to switch between shells and tasks.

Anyways, in ubuntu the maintainers accidentally hijacked the F3  shortcut with a conflicting shortcut F3 in screen; So, what happened is this, I edit a file with nano, I attempt to save, it but find myself in another screen. You could imagine why that would piss me off. Context-switching craziness.


What you do next to fix the unintentional context-switching is to attack screen. Namely, the configuration files.


In ubuntu they put this in your home dir.

You want to find change your keybindings file.


find ~/ -name "keybindings"


When you crack the file open you'll find something like this:


source /usr/share/screen-profiles/keybindings/common


All I needed to do was to copy that common file up there to /home/user/.screen-profiles/user-common-keys and adapt keybindings to reflect the change.

All I wanted was to regain the ability to save my files with F3 function key. So I smote away this line from the file:


bindkey -k k3 prev ...


and change the keybindings file.


source /home/user/.screen-profiles/user-common-keys


Then they lived happily ever after.

Arithmetic mind hacking, part 1: addition

I have a bad memory for numbers. Really bad. It has improved since I started studying arithmetic strategies. I wish I started earlier, but we didn't have the internets when I was younger, o, also my social standing did not depend on my number-crunching abilities then.

Let's talk about addition. I think this is the easiest concept to absorb for most people.

You have one cookie, then you get another cookie from a loving spoiling parent/guardian, then you have how many cookies?

Zero; because before you considered the metaphysical abstract notion of numbers you already ate the cookies.

To crunch numbers quickly we need to get practical. Brutally practical, if it doesn't work for you, practice. If it still doesn't work for you, simplify, adapt, remould, evolve; stir and shake; rinse and repeat.

Say I have... 2512 cookies and I find 3467 cookies stored in a jar. How many cookies do I have in total?

First strategy proposal:

I round off the trickiest looking number, to me that would be 3467, upwards to 3500; that would be a difference of 33.

I add 3500 to 2500 and add 12 later.

3500 + 2500 = 6000

Then I take 33, I round that off to 40.

6000 - 40 = 5960

I took too much so I give back 7.

5960 + 7 = 5967

I almost forgot I suspended 12 from the game; so I gotta get it back.

5967 + 12 = 5979.

2nd strategy proposal:

That previous strategy took 4 steps. It could get simplified.

How about starting by subtracting 12 from the rounded up difference of 33?
Conceptually it's like this: I borrowed 33 to get a nice round number, but instead of paying back in full, I pay back some right now (12).

33 - 12 = 21 (I still owe)

So I add as we did previously.

3500 + 2500 = 6000

We pay back the 21 by subtracting.

6000 - 21 = 5979

The difference

The first is more explicit, but it requires more memory, because of the 12.

The 2nd requires a wee less memory for numbers but it requires some practice because it requires the use concepts of borrowing and repayment in the rounding-of-the-numbers phase; but the payoff is in the number of steps.

It's one of those memory vs. time trade-offs.

The third and final strategy proposal

Go into heuristic mode. Check whether 10 and 60 make a hundred, 2 and 7 make a ten. No?

Now just add those up

(10 + 60) + (2+7) = 79

3400 + 2500 = 6000


This one requires a heuristic, in contrast with the previous proposals, that checks if a sum of decimal numbers go over 9.

Final note

The previous strategies were overkill for this problem.

For instance 789 + 837 and the previous strategies would work out great with this example.

Round off the number which contains a combination which add up to more than 9.  The one closest to 9; which would be 789. We have a debt of 11; in other words, we borrowed 11.

800 + 837 = 1637
1637 - 11 = 1626

During addition, check for sums larger than nine. Round off or just add the numbers. It's quite simple really. Some people have the innate ability to figure out these strategies, but other people need a tiny push in the right direction.

Portscanner: nmap

Recently, I wanted to list all the mac addresses of all our voip telephones (not to be confused with softphones) and reserve a block of addresses from our ip-address space and map each macaddy to an ip addy.

I already knew of nmap and I wondered how one could get all the mac addresses.

So I scanned with nmap.

I dug around on the net and bumped into this mindmap. Since I was scanning locally and not sniffing around someone else's stuff, I could get aggressive (-T5).


sudo nmap -T5 ... ip_addr_range


I wanted output I could grep, so I picked a typical output format (-oG).


sudo nmap ... -oG grepme.txt ...


I found out from the nmap man page, that nmap can do fingerprinting with the -O switch.


sudo nmap -O ip_addr_range -T5 -oG grepme.txt ...


Then I found out that the -oG option did not output the mac address. I was bummed out.

Some monkeying around I found out one could sed using multiple patterns, but that would take effort.

Update 23-07-2010: Some googling led to this command (from this blog)

First try:

nmap -T5 -sP -n ip_addr_range | tee log.txt | sed -n '1!H;${;g;s/Host \([0-9.]\+\) is up.*MAC Address: \([0-9A-F:]\+\)/\1 \2/g;p;}'

Way too greedy.

2nd try:
... still working on it...

Sunday, June 6, 2010

Alternative input devices and udev

If you have or own:
  1. an alternative character input device, e.g. typematrix keyboard (why);
  2. a keyboard with its own unique layout e.g. dvorak;
  3. a family where most of the members use the same computer;
  4. a gnu/linux computer with udev support, where you have root permission;
  5. a desire to automate changing the keyboard layout when plugging your alternative device in;
Udev is this nice system in linux where you can use certain attributes of devices to write rules so you could run certain scripts, mount devices etc. i.e. give behaviour to certain devices, when they are introduced to your operating system (Linux).

There might be predefined rules already in your /etc/udev/rules.d, check it out.

To gather info about your device, hit lsusb for usb devices and hit lspci for more info about any other non-usb devices.

You need the following command to figure out the attributes required to create the conditions for a certain rule:

$ udevadm info -a -p $(udevadm info -q path -n /dev/input/eventX) --walk-attributes

# NOTE: /dev/input is where your mouse or keyboard is usually put.

You can create general rules or device-specific rules for certain devices; For instance you can figure out what the ATTRS{idVendor} of a certain device and it would match with every device made by this vendor (in theory).
 You could also match with ATTRS{serial} and only that device containing that serial number would make the rule fire (in theory).

In udev rules file, only a rule per line is allowed. You can test the validity of a rule by writing a dummy function, i.e. echo to a file in /tmp/udevtest.txt. While plugging and unplugging the device (in my case a keyboard), check if the 'add' and 'remove' ACTION(s) are actually firing, by checking 'udevadm trigger'.

Next step is to write decide what to do with the rule. Recall I mentioned dvorak (pronounced as: dvor-zh-ak, it's czech), if you want to load a certain keyboard layout try writing a udev rule like this in /etc/udev/rules.d/111-typematrix-kb.rules:

## via Xorg
ACTION=="add", KERNEL=="event?", SUBSYSTEMS=="usb",ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="8081",RUN+="/usr/bin/setxkbmap -model 102 -layout dvorak"

ACTION=="remove", KERNEL=="event?", SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="8081", RUN+="/usr/bin/setxkbmap -model 102 -layout us_intl"


If your rule fires repeatedly for no apparent reason, try writing less general rules and change the wildcard * to ? so it matches with only a single character. Hit man udev when you're clueless about how your rule should look like. Also use ATTR instead of ATTRS where appropriate, hit the --walk-attributes in udevadm I just mentioned.

Next post: How to setup udev and OCZ NIA to control your computer with your mind. (TODO)