I don't have the code for you, but the utility is XNET which come with KiXtart v3.63

This is free, or the Windows NT/2000 Resource Kit has utilities for this also.

You can write code to automate them.