469,311 Members | 2,483 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,311 developers. It's quick & easy.

Parsing output of `nbtstat -a`

I have been given a text file that contains 'nbtstat -a' output for various computers in our network. The script needs extract the name of the computer and if there is a user name that nbtstat reads; extract that as well. If there is no user name then the script needs to say something like 'no user found'. does anyone have an idea how i would do that?
Aug 1 '07 #1
11 5010
numberwhun
3,503 Expert Mod 2GB
First, I have never heard of nbstat and would not have the foggiest clue as to what the format of the output is. Could you possibly provide a sample of the data so we can see it?

Also, we will be happy to help you, but must first see what you have tried thus far. Please post the code that you have tried and we will assist you from there.

Regards,

Jeff
Aug 1 '07 #2
Here is a sample of the output

Expand|Select|Wrap|Line Numbers
  1. C:\WINDOWS\system32>nbtstat -a BOB#######
  2.  
  3. Local Area Connection:
  4. Node IpAddress: [] Scope Id: []
  5.  
  6. NetBIOS Remote Machine Name Table
  7.  
  8. Name Type Status
  9. ---------------------------------------------
  10. BOB####### <00> UNIQUE Registered
  11. NT <00> GROUP Registered
  12. BOB####### <03> UNIQUE Registered
  13. BOB####### <20> UNIQUE Registered
  14. TOM_LK <03> UNIQUE Registered
  15.  
  16. MAC Address =
  17.  
I had to change the computer name and user name b/c of work. But the scipt has to read this out put and print to a file the computer name and the username associated with the computer if it has one. So far the code I have written will print the line which contains computer name. Here is what I got so far

Expand|Select|Wrap|Line Numbers
  1. #! C:\Perl\bin\perl
  2.  
  3. $data_file = "nbtstat-output.txt";
  4. open(DAT, $data_file) || die("Could not open file!");
  5. @raw_data = <DAT>;
  6. close(DAT);
  7.  
  8. @barcode = grep(m/\b\w{2,5}\d{6,9}\b/, @raw_data);
  9.  
  10. foreach $barcode(@barcode) {
  11.     print "$barcode\n";
  12. }
  13.  
This is my first time writing in perl. I am doing this project on the side to learn the language. So any help will be apprciated.
Aug 2 '07 #3
numberwhun
3,503 Expert Mod 2GB
Well, we can certainly help you, that is what we are here for. Thank you for providing the output as that helped me to understand the data's structure. I fully understand changing the data, work is work and there will always be information we have to safeguard.

As for your script, I can assume from your code you are only wanting to output the lines that list the Name/Type/Status. So, what about the following:

Expand|Select|Wrap|Line Numbers
  1. #! C:\Perl\bin\perl
  2.  
  3. $data_file = "nbtstat-output.txt";
  4. open(DAT, $data_file) || die("Could not open file!");
  5. @raw_data = <DAT>;
  6. close(DAT);
  7.  
  8. foreach $line (@raw_data)
  9. {
  10.     if ($line =~ m/\w+\s+\<\d{2}\>\s+\w+\s+\w+/)
  11.     {
  12.         print("$line");
  13.     }
  14. }
  15.  
I tested this on the data you provided, with the # signs removed and it matched the following lines:

BOB####### <00> UNIQUE Registered
NT <00> GROUP Registered
BOB####### <03> UNIQUE Registered
BOB####### <20> UNIQUE Registered
TOM_LK <03> UNIQUE Registered

Is that the result you wanted?

Also, when putting code into your postings, be sure to add the CODE tags around your script so the moderator(s) won't have to follow up behind you and do it. You will see examples of how to do this in the forum editor, off to the right the next time you post.

As for learning Perl, good choice of languages. Its awesome. Be sure to get a copy of Learning Perl as it is one of the best books IMHO. Also, be sure to read perldoc.perl.org.

Regards,

Jeff
Aug 2 '07 #4
KevinADC
4,059 Expert 2GB
did you want all the lines between "Name Type Status" and "MAC Address = "?
Aug 2 '07 #5
numberwhun
3,503 Expert Mod 2GB
did you want all the lines between "Name Type Status" and "MAC Address = "?
That is what is sounded like to me. I tested the code I put up and it does just that. :-) Let's see what he comes back with.

Regards,

Jeff
Aug 2 '07 #6
KevinADC
4,059 Expert 2GB
Well, I am confused because the OP posted this:

@barcode = grep(m/\b\w{2,5}\d{6,9}\b/, @raw_data);


but there are no places where there are six to nine digits in the lines he posted unless they are the ###### stuff we can't see.
Aug 2 '07 #7
Well, we can certainly help you, that is what we are here for. Thank you for providing the output as that helped me to understand the data's structure. I fully understand changing the data, work is work and there will always be information we have to safeguard.

As for your script, I can assume from your code you are only wanting to output the lines that list the Name/Type/Status. So, what about the following:

Expand|Select|Wrap|Line Numbers
  1. #! C:\Perl\bin\perl
  2.  
  3. $data_file = "nbtstat-output.txt";
  4. open(DAT, $data_file) || die("Could not open file!");
  5. @raw_data = <DAT>;
  6. close(DAT);
  7.  
  8. foreach $line (@raw_data)
  9. {
  10.     if ($line =~ m/\w+\s+\<\d{2}\>\s+\w+\s+\w+/)
  11.     {
  12.         print("$line");
  13.     }
  14. }
  15.  
I tested this on the data you provided, with the # signs removed and it matched the following lines:

BOB####### <00> UNIQUE Registered
NT <00> GROUP Registered
BOB####### <03> UNIQUE Registered
BOB####### <20> UNIQUE Registered
TOM_LK <03> UNIQUE Registered

Is that the result you wanted?

Also, when putting code into your postings, be sure to add the CODE tags around your script so the moderator(s) won't have to follow up behind you and do it. You will see examples of how to do this in the forum editor, off to the right the next time you post.

As for learning Perl, good choice of languages. Its awesome. Be sure to get a copy of Learning Perl as it is one of the best books IMHO. Also, be sure to read perldoc.perl.org.

Regards,

Jeff
I bought Programming Perl, it has helped somewhat but at there is just alot of information there. I also visit cpan.org as well. as far as the output I am look for,
the people who I am writing the script for want something like this
BOB####### |---tab deliminated--| Tom_LK
computer name user name

they want to eventually import this output into excel to see which users are accessing these machines.
Aug 2 '07 #8
numberwhun
3,503 Expert Mod 2GB
I bought Programming Perl, it has helped somewhat but at there is just alot of information there. I also visit cpan.org as well. as far as the output I am look for,
the people who I am writing the script for want something like this
BOB####### |---tab deliminated--| Tom_LK
computer name user name

they want to eventually import this output into excel to see which users are accessing these machines.
Programming Perl is a good reference. They call it the bible, I call it a function reference. I typically stick to Learning Perl as it does a lot more in the way of explaining things. CPAN contains all the modules and some documentation, but definitely visit perldoc.perl.org as it is filled with tons of information.

As for your issue, I am now even more confused. The output you are looking for:

BOB####### |---tab deliminated--| Tom_LK

looks to contain data from two seperate lines in your output. Is this correct? If so, we need to be able to determine what is a machine name and what is a user name. Can you describe the file further?

Regards,

Jeff
Aug 2 '07 #9
Programming Perl is a good reference. They call it the bible, I call it a function reference. I typically stick to Learning Perl as it does a lot more in the way of explaining things. CPAN contains all the modules and some documentation, but definitely visit perldoc.perl.org as it is filled with tons of information.

As for your issue, I am now even more confused. The output you are looking for:

BOB####### |---tab deliminated--| Tom_LK

looks to contain data from two seperate lines in your output. Is this correct? If so, we need to be able to determine what is a machine name and what is a user name. Can you describe the file further?

Regards,

Jeff
The output is on 2 seperate lines.
BOB#### is the computer name
TOM_LK is the user name
Basically nbtstat checks the NetBIOS information on windows machine. if a user is logged on to that machine it prints that out as well. Someone took that nbtstat information for a number of machines and redirected the output to a text file. I am not sure if they realized all the extra information they will get along with the computer and and user name; there are 2 other wrinkles. Some of the machines they ran this command against did not have a user name associated with it (basically that means that the line with TOM_LK . is not there) and there some machines the command could not find, so it printed out the phrase "host not found." Here is an idea of how all of this looks on the text file:

C:\WINDOWS\system32>nbtstat -a BOB#######

Local Area Connection:
Node IpAddress: [] Scope Id: []

NetBIOS Remote Machine Name Table

Name Type Status
---------------------------------------------
BOB####### <00> UNIQUE Registered
DL <00> GROUP Registered
BOB####### <03> UNIQUE Registered
BOB####### <20> UNIQUE Registered
TOM_LK <03> UNIQUE Registered

MAC Address =


C:\WINDOWS\system32>nbtstat -a MOE#######

Local Area Connection:
Node IpAddress: [] Scope Id: []

NetBIOS Remote Machine Name Table

Name Type Status
---------------------------------------------
MOE####### <00> UNIQUE Registered
DL <00> GROUP Registered
MOE####### <20> UNIQUE Registered
MOE####### <03> UNIQUE Registered

MAC Address =


C:\WINDOWS\system32>nbtstat -a BILL#######

Local Area Connection:
Node IpAddress: [] Scope Id: []

Host not found.

for the first output the script needs to printout a line like this

BOB####### TOM_LK

for the other 2 outputs the scripts needs to print

MOE####### No user found
BILL####### No user found

I hope that helps.
Aug 2 '07 #10
numberwhun
3,503 Expert Mod 2GB
The output is on 2 seperate lines.
BOB#### is the computer name
TOM_LK is the user name
Basically nbtstat checks the NetBIOS information on windows machine. if a user is logged on to that machine it prints that out as well. Someone took that nbtstat information for a number of machines and redirected the output to a text file. I am not sure if they realized all the extra information they will get along with the computer and and user name; there are 2 other wrinkles. Some of the machines they ran this command against did not have a user name associated with it (basically that means that the line with TOM_LK . is not there) and there some machines the command could not find, so it printed out the phrase "host not found." Here is an idea of how all of this looks on the text file:

C:\WINDOWS\system32>nbtstat -a BOB#######

Local Area Connection:
Node IpAddress: [] Scope Id: []

NetBIOS Remote Machine Name Table

Name Type Status
---------------------------------------------
BOB####### <00> UNIQUE Registered
DL <00> GROUP Registered
BOB####### <03> UNIQUE Registered
BOB####### <20> UNIQUE Registered
TOM_LK <03> UNIQUE Registered

MAC Address =


C:\WINDOWS\system32>nbtstat -a MOE#######

Local Area Connection:
Node IpAddress: [] Scope Id: []

NetBIOS Remote Machine Name Table

Name Type Status
---------------------------------------------
MOE####### <00> UNIQUE Registered
DL <00> GROUP Registered
MOE####### <20> UNIQUE Registered
MOE####### <03> UNIQUE Registered

MAC Address =


C:\WINDOWS\system32>nbtstat -a BILL#######

Local Area Connection:
Node IpAddress: [] Scope Id: []

Host not found.

for the first output the script needs to printout a line like this

BOB####### TOM_LK

for the other 2 outputs the scripts needs to print

MOE####### No user found
BILL####### No user found

I hope that helps.

Ok, one thing we really need to know. Are the ####### representative of numbers? If so, are they the same number of numbers each time? (ie: a string of 7 digits)

Regards,

Jeff
Aug 2 '07 #11
Ok, one thing we really need to know. Are the ####### representative of numbers? If so, are they the same number of numbers each time? (ie: a string of 7 digits)

Regards,

Jeff
they are a string of 7 or 9 digits. Mostly 7 but we do have some PCs wth 9 digits. each PC name is unique; this is rg string i used /\s\b\w{2,5}\d{7,9}\b/
w(2,5} = name of the PC (name letters range between 2-5 letters) the names are associated /w departments so there are a few MOE,'s BILL's etc..
d{7,9} = unique number either 7 or 9 digits long.
Aug 2 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Fraser Gordon | last post: by
8 posts views Thread by Gerrit Holl | last post: by
reply views Thread by burn_hall | last post: by
3 posts views Thread by siddharth jain | last post: by
9 posts views Thread by ankitdesai | last post: by
reply views Thread by =?Utf-8?B?VWxmIFRob3JzZW4=?= | last post: by
1 post views Thread by avpkills2002 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.