An AppleScript to look up NetBIOS names

Mar 28, '03 09:59:00AM

Contributed by: xeroply

When pointing the Finder or Print Center at an SMB (Windows) file share or printer (see this hint), NetBIOS names don't seem to do the trick. Mac OS X wants DNS names or IP addresses. So, a share that you "map" on MS Windows clients as MYSERVERMYSHARE has to get translated to something like smb://myserver.company.com/myshare in order for the Finder to understand it in the Connect to Server box. The problem I've found is that these mappings aren't always obvious.

Fortunately, Jaguar comes with some Samba UNIX tools that can help out with this problem, but they're kind of obscure. After much headache and stumbling through web tutorials, I wrote the following AppleScript to make them easy to use for name/address translation.

Read the rest of the hint for the script...

This script relies on the presence of a WINS server (the NetBIOS equivalent of a DNS server) entry in /private/etc/smb.conf. Mine already had such an entry, but your mileage may vary. When run, the script asks for a NetBIOS name and then attempts to get an associated IP address and DNS name. You can also click "More Info" in the results box to open two stacked Terminal windows with more detailed information (other NetBIOS names associated with the same device, and shares available on the device. Note that the latter may ask for a server password in the Terminal). From there you can copy and paste as needed to construct an SMB pathname that the Finder or the Print Center will understand.


--attempt to read the WINS server from the smb.conf file
set myConfig to do shell script "grep wins /private/etc/smb.conf"
if myConfig = "" then
  display dialog "Couldn't find a WINS server entry in smb.conf" buttons {"Cancel"}
else
  -- grab the IP address of the WINS server
  set AppleScript's text item delimiters to " = "
  set myWINS to get last text item of myConfig
  
  --get the NetBIOs name to lookup
  set theInput to display dialog ¬
    "Enter the NetBIOS name to look up (in uppercase):" default answer ¬
    "SERVER" buttons {"Cancel", "OK"} default button "OK"
  
  if button returned of theInput is "OK" then
    
    set theServer to text returned of theInput
    
    -- do a basic lookup and display the result in a dialog box
    set theOutput to do shell script "nmblookup -T -U " & myWINS & " -R " & theServer
    set moreOutput to display dialog theOutput buttons {"More Info", "OK"}
    
    -- open the Terminal to provide a more detailed display if desired
    if button returned of moreOutput is "More Info" then
      tell application "Terminal"
        activate
        -- make room for a second window
        set the position of the front window to {100, 22}
        -- do a detailed lookup of the server's NetBIOS properties
        do script "nmblookup -T -S -U " & myWINS & " -R " & theServer ¬
          in the front window
        set winHeight to the last item of (get the size of the front window)
        --look up active shares on the server in a new window positioned below the first
        do script "smbclient -L " & theServer
        set the position of the front window to {100, (22 + winHeight)}
      end tell
    end if
  end if
end if
If you don't want to bother with Script Editor, there's a version saved as an application that you can download here.

[robg adds: I haven't tested this one...]

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030327154357426