Batch Scripts and Sixty Four Bits of Architecture

Written by

I was recently writing a batch script to auto-install service packs onto windows, detecting the OS version was relatively simple and there’s plenty of scripts on the internets to do this; on the otherhand, detecting between 32 and 64bit architectures is much more complicated.

Microsoft has a KB artictle with batch script code to do this which is great apart from it doesn’t work under a standard user account and might not even work on an admin account (it didn’t work on my domain admin account), so if MS can’t supply code that works is there even a detection method?

Some people suggest that you should be searching for the %programfiles(x86)% variable which is only on 64bit systems, this is great apart from it didn’t work either.

I then stumbled by chance on a great solution which is to check the %PROCESSOR_ARCHITECTURE% variable. This worked great! I have been told that if the machine has WMI disabled then this wouldn’t work but most systems have WMI enabled.

     echo x64
     "X:\Windows 7 x64 SP1.exe" /passive /norestart
 ) ELSE (
     echo x86
     "X:\Windows 7 x86 SP1.exe" /passive /norestart

That’s the code that I’m now using to detect the architecture of the systema nd install the correct SP, although it will attempt to install the 32bit Service pack onto 64Itinium machines; an extra check can be included to test for that but I’m unlikely to stumble upon an Itinium based system in my 20 PC office…

Variable System Result
64bit AMD64
WOW64 x86
Itinium IA64
%PROCESSOR_ARCHITEW6432% 32bit null
64bit null

Hope this is useful to others.

Migrating MySQL to a Different System the Easy Way

Written by

Well this week I’ve replaced my personal home server and reinstalled my work’s development server; both these systems have a MySQL server running which contain databases (duh) that would be either too large to import/export or too complex to rebuild from scratch.

So I decided to find a way of copying the database files directly between systems and it’s actually dead easy and strangely not very well documented! So here’s the steps to do it fairly painlessly.

This guide assumes you have a root account and root database access.

Step 1:

Log into MySQL and lock the databases from being edited by typing the following:


Step 2:

In your favorite shell copy the databases like so:

cp -R /var/lib/mysql /mnt/mounted

Step 3:

On your new system copy the databases into the correct location:

cp -R /mnt/mounted/mysql /var/lib

Step 4:

Fix the permissions on the databases

chmod -R 700 /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql

Step 5:

Reboot MySQL server

service mysql restart


/etc/init.d/mysql restart

Step 6:

Unlock the databases by typing this into MySQL


All your databases and previous permissions should now be copied accross and working!

Linux WiFi: Deauthenticated Reason Codes

Written by

So I’ve been having a fiddle with Linux and WiFi this evening in an attempt to make a wifi bridge (Windows can do it so why can’t Linux?)…

Anyway the long and short of it is that I was getting disconnected and de-authenticated from the access point. Having a quick look at dmesg said I was disconnected for “Reason 2” and “Reason 6”; these on their own are not very helpful and so in a quest to find out what these codes meant, I found that there isn’t a list that’s published anywhere easy to find (a single forum post and in an IEEE pdf). The codes in italics are the most common.

Code Reason Explanation
0 Reserved Normal working operation
1 Unspecific Reason We don’t know what’s wrong
2 Previous authentication no longer valid Client has associated but is not authorised.
3 Deauthenticated because sending STA is leaving (or has left) IBSS or ESS The access point went offline, deauthenticating the client.
4 Disassociated due to inactivity Client session timeout exceeded.
5 Disassociated because AP is unable to handle all currently associated STAs The access point is busy, performing load balancing, for example.
6 Class 2 frame received from nonauthenticated STA Client attempted to transfer data before it was authenticated.
7 Class 3 frame received from nonassociated STA Client attempted to transfer data before it was associated.
8 Disassociated because sending STA is leaving (or has left) BSS Operating System moved the client to another access point using non-aggressive load balancing.
9 STA requesting (re)association is not authenticated with responding STA Client not authorized yet, still attempting to associate with an access point.
10 Disassociated because the information in the Power Capability element is unacceptable
11 Disassociated because the information in the Supported Channels element is unacceptable
12 Reserved Not Used or Special Purpose
13 Invalid information element.
14 Message integrity code (MIC) failure
15 4-Way Handshake timeout
16 Group Key Handshake timeout
17 Information element in 4-Way Handshake different from (Re)Association Request/Probe
Response/Beacon frame
18 Invalid group cipher
Association denied due to requesting STA not supporting all of the data rates in the BSSBasicRateSet parameter
The link speed requested by the client or AP is incompatible. (i,e. trying to operate N only speeds on a G AP)
19 Invalid pairwise cipher
20 Invalid AKMP
21 Unsupported RSN information element version
22 Invalid RSN information element capabilities
23 IEEE 802.1X authentication failed
24 Cipher suite rejected because of the security policy
25-31 Reserved Not Used or Special Purpose
32 Disassociated for unspecified, QoS-related reason Quality of Service has denied the action.
33 Disassociated because QoS AP lacks sufficient bandwidth for this QoS STA
34 Disassociated because excessive number of frames need to be acknowledged, but are not
acknowledged due to AP transmissions and/or poor channel conditions
35 Disassociated because STA is transmitting outside the limits of its TXOPs
36 Requested from peer STA as the STA is leaving the BSS (or resetting)
37 Requested from peer STA as it does not want to use the mechanism
38 Requested from peer STA as the STA received frames using the mechanism for which a
setup is required
39 Requested from peer STA due to timeout
45 Peer STA does not support the requested cipher suite
45-65 Reserved Not Used or Special Purpose
99 Typically “No Reason Code” / Unknown State
535 Reserved Not Used or Special Purpose

The first few reason codes where helpful while debugging my wifi related issues.

I’ve added a few more codes and descriptions; they conflict with a previous list from IEEE but this one is from Cisco and was discovered after an N wifi card refused to connect to an N router at N speeds.

Linux Wifi: Belkin F5D7050 Wireless G USB Adapter

Written by

I was testing my Belkin USB WiFi dongle today and Debian wouldn’t use it and I was getting errors/issues to do with a faulty firmware.

Even after following a few guides online and installing different packages etc, I still couldn’t get it to work but I finally managed using the following method:

Add Contrib & Non-Free Repo’s:

nano /etc/apt/sources.list
deb http://YOUR.CHOSEN.MIRROR squeeze main non-free contrib
deb-src http://YOUR.CHOSEN.MIRROR squeeze main non-free contrib
apt-get update

Install Latest Working Firmware (and tools):

apt-get install firmware-ralink wireless-tools

– OR –
Download manually: Wireless-Tools and ralink-Firmware

dpkg -i package.deb

Plug the WiFi Dongle in and test:

ifconfig wlan0 up

iwlist scan

All your local AP’s should wizz past on screen now.

Some useful WMI Filters

Written by

WMI Filters are filters used via Group Policy Managment in Active Directory to control what GPO’s are applied to what machine. They are rather simple to understand as they are structured like SQL all you need to know is the data stored in WMI.

Here’s a couple of filters I have found useful.

Detect OS Version:

Windows 7

SELECT * FROM Win32_OperatingSystem WHERE Version like "6.1%" AND ProductType = "1"

Widows 2008 R2

SELECT * FROM Win32_OperatingSystem WHERE Version like "6.1%" AND ProductType = "2"

Windows Vista

SELECT * FROM Win32_OperatingSystem WHERE Version like "6.0%" AND ProductType = "1"

Windows 2008

SELECT * FROM Win32_OperatingSystem WHERE Version like "6.0%" AND ProductType = "2"

Windows XP

SELECT * FROM Win32_OperatingSystem WHERE Version like "5.1%"

Windows 2003

SELECT * FROM Win32_OperatingSystem WHERE Version like "5.2%"

Windows 2000

SELECT * FROM Win32_OperatingSystem WHERE Version like "5.0%"

Detect Arcitecture:

Windows x64 (64bit)

SELECT * FROM Win32_Processor WHERE AddressWidth="64"

Windows x86 (32bit)

SELECT * FROM Win32_Processor WHERE AddressWidth="32"

Detect Service Pack:

Service Pack 3 or above

SELECT * FROM Win32_OperatingSystem WHERE ServicePackMajorVersion>=3

Pre-Service Pack 3

SELECT * FROM Win32_OperatingSystem WHERE ServicePackMajorVersion<3

The filters can be included together to create more precise detection such as Windows XP SP2 or Windows 7 x64 SP1.

Hope these help others as much as myself.