The shortest distance between skew lines r=a1+tb1 and r=a2+sb2 is: SD = |[a2-a1, b1, b2]| / |b1 x b2|.
The numerator is the absolute value of the STP of (a2-a1), b1, b2. The denominator is the magnitude of the cross product of the direction vectors.
Derivation: The common perpendicular has direction b1 x b2. The shortest distance is the projection of (a2-a1) onto this direction: SD = |(a2-a1).(b1 x b2)| / |b1 x b2| = |[a2-a1, b1, b2]| / |b1 x b2|.
If [a2-a1, b1, b2] = 0, the lines are coplanar (either intersecting or parallel). If b1 x b2 = 0, the lines are parallel, and the formula doesn't apply; use SD = |((a2-a1) x b)| / |b| instead.