Archive for January, 2009

pyNetView

Have you ever wanted to just have a list of the machines on your network? One that you could easily pass to a script / application? Well I sure have.  So I do the net view > outfile.txt then scrub the text file dance. All I really want is a list of the machine names. No \\ in front of the names. No description of the systems either. I want to go from this:

\\comp1                my computer
\\comp2
\\comp3                test machine

to this:

comp1
comp2
comp3

So today I decided to write a little crap script to do this for me in one step. I know it isn’t pretty but I don’t really care. I just want it to work. The good thing is that I can now use the same kind of code in some of my other scripts and I don’t even need it to write to a file at all.

#!/usr/bin/env python
import os

outfile = open(r'pyNetView.txt', 'w')

indata = os.popen('net view')

for line in indata:
    if line.startswith('\\'):
        line = line.split()
        clean = line[0][2:]
        outfile.write(clean + '\n')

outfile.close()

Comments are welcome. Use it if you want.

Advertisements

12 Comments