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
  1. #1 by zodman on January 22, 2009 - 5:37 pm

    im do a ping from x.x.x.0 to x.x.x.254 for seen the machines up

  2. #2 by tacone on January 22, 2009 - 5:47 pm

    I get:
    No command: view

  3. #3 by bbechdol on January 22, 2009 - 5:53 pm

    I have been looking for something like this! Good show old chap! 🙂

  4. #4 by Jeff Schroeder on January 22, 2009 - 6:05 pm

    Here is a 1 liner to replace your script assuming this is from a ‘nix box:
    net view | awk ‘/^\\/{print $1}’ | sed -e ‘s:^\\\\::g’ > pyNetView.txt

  5. #5 by Jeff Schroeder on January 22, 2009 - 6:09 pm

    Sorry but you’ll need to manually fix the quoting. WordPress breaks it. You can prevent wordpress from doing that by following the advice here:
    http://blog.ninedays.org/2008/05/16/problem-with-curly-smar-quotes-in-code-with-wordpress-and-markdown/

  6. #6 by stgraber on January 22, 2009 - 7:40 pm

    stgraber@sahal:~$ cat test | cut -d ” ” -f1 | sed “s/\\\//g”
    comp1
    comp2
    comp3

    You can then use: net view | cut -d ” ” -f1 | sed “s/\\\//g”

  7. #7 by etank on January 22, 2009 - 8:55 pm

    I agree will all of the comments so far but I was doing this on a Windows XP box. On a Linux box it would be a different deal all together 🙂

  8. #8 by shantanu goel on January 22, 2009 - 11:42 pm

    net view | cut -d ” ” -f1 | cut -d “\\” -f3
    this is another way to go about it.
    And you can do this is on windows as well (thanks to gnuwin32 (native) or u may also use cygwin)

  9. #9 by Jeff Schroeder on January 23, 2009 - 12:07 am

    http://unxutils.sourceforge.net/ install these and put them in somewhere like C:\windows\system32 and my comment still should work

  10. #10 by JCabillot on January 23, 2009 - 3:20 am

    Filed under: Programming, Python, __Ubuntu__

  11. #11 by PhE on January 23, 2009 - 8:19 am

    Your script in a full function Python style :
    #!/usr/bin/env python
    import os
    open(r’pyNetView.txt’, ‘w’).write(‘\n’.join([line.split()[0][2:] for line in os.popen(‘net view’) if line.startswith(‘\\’)]))

  12. #12 by Tormod on January 23, 2009 - 10:11 am

    awk (use gawk for windows) can do it all in one command 🙂
    awk ‘/\\\\/{ print substr($1,3)}’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: