LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   NAS Discovery - how? (https://www.linuxquestions.org/questions/linux-networking-3/nas-discovery-how-4175477817/)

displace 09-20-2013 12:35 AM

NAS Discovery - how?
 
Greetings

How can I search and discover NAS devices attached to my LAN from a shell/bash? I'm trying to write a bash script for NAS discovery, but I am unsure what commands to use. It is supposed to enumerate the local network shares.

lleb 09-20-2013 09:11 AM

what exactly are you looking for? are you looking for a brand new NAS plugged fresh into a DHCP network to identify its IP?

are you looking for an already configured NAS with a static IP?

what?

displace 09-20-2013 09:24 AM

Either one. I need a list of local shares where a user can select a share in a web-gui (possibly provide authentication), and have the server connect/mount that share to a specific folder. I don't own a NAS, so I have no idea how to discover them. I mean what protocols do they use to announce their presence? CIFS? NFS? UPNP?

lleb 09-20-2013 09:58 AM

it all depends on the brand and how they are configured. could be cifs, could be nfs, could be afs or even ftp.

you would detect a share like you would any other kind of share detection. each type has its own method of detection. nfs for example uses exports -a.

so yes what you are wanting to do perform is possible, but you need to narrow down, or expand exactly what you are looking for.

sounds like you need to start writing the script then when you need additional help post back to these forums with detailed questions. there are plenty of great script masters around here willing to help.

displace 09-20-2013 11:53 AM

I'm looking to implement support for the most common consumer-grade NAS' - the ones you can get from shelves or internet stores? Most average users have Windows on their machines, so I assume the protocol of choice would be Samba/CIFS.

EDIT: According to some websites the two most common protocols are NFS and CIFS. Good, this is one part of information I was looking for. Now how to discover which machines on the network offer these services. I could use nmap, but that would be an overkill.

EDIT2: You mentioned exports for NFS and samba seems to have a tool called smbtree. I'll play with these a bit.

ronlau9 09-20-2013 01:47 PM

I do not know what you exactly like to do .
It does not matter if use the linux box , Mac BOx or the windows box .
Typing in firefox find.synology.com it connects to my NAS and it give two options sign as a user or as admin
Signing in as admin I can do anything with NAS

lleb 09-20-2013 02:06 PM

Quote:

Originally Posted by ronlau9 (Post 5031558)
I do not know what you exactly like to do .
It does not matter if use the linux box , Mac BOx or the windows box .
Typing in firefox find.synology.com it connects to my NAS and it give two options sign as a user or as admin
Signing in as admin I can do anything with NAS

yes each manufacture typically will have something like that, or they will have tools you can run, etc...

also keep in mind, there is zero share by default on many SoHo NAS devices. they are typically configured for DHCP, then require either a web interface access and very few have CLI interface access for configuration.

typically its all web based for configuration. setting user/pw, setting types of shares, setting permissions, etc...

if you are looking to discover a new NAS randomly plugged into your LAN, then configure via same script, i dont know if you will pull that off with all SoHo types of NAS devices just as most dont have CLI interfaces for configuration.

if you are just attempting to detect a new NAS connected to a LAN, then you might look into nmap. i have used that many times to locate a NAS as they typically will report their manufacture and type.

displace 09-21-2013 04:57 AM

Maybe I forgot to mention that I'm doing this on a headless system. So no firefox, file managers or gui tools. There's gonna be a web interface in php where a user can connect, and view a list of NAS devices. Thanks for pointing that out about unconfigured NAS devices, but I'm only interested in configured devices that have some existing shares defined.

I wrote this python script that seems to detect most of the CIFS shares.
Code:

import sys, re, json
import subprocess

try:
        popen = subprocess.Popen("smbtree -N -d 0", shell=True, stdout=subprocess.PIPE)
        ret = popen.wait()
except:
        sys.exit(1)

out = popen.stdout.read()
output = out.split("\n")
results = {}
workgroup = ""
server = "//"

for line in output:
        r = re.match("^\s\s(\S+)\s+(.*)$", line)
        if r is not None:
                share = r.group(1)
                desc = r.group(2).strip()
                results[workgroup][server][share] = desc
                continue
       
        r = re.match("^\s(\S+)\s+(.*)$", line)
        if r is not None:
                server = r.group(1)
                desc = r.group(2).strip()
                results[workgroup][server] = {'desc': desc}
                continue
       
        r = re.match("^\S+", line)
        if r is not None:
                workgroup = r.group(0)
                results[workgroup] = {}
                continue

print json.dumps(results)
sys.exit(0)

I'll have to play around with the exports and see if I can get a list of NFS shares.

Edit: I wrote a simpler python script that only shows visible/public CIFS shares.
Code:

import sys
import re
import json
import subprocess

try:
        popen = subprocess.Popen("smbtree -N -d 0", shell=True, stdout=subprocess.PIPE)
        ret = popen.wait()
        if ret != 0:
                sys.exit(ret)
        out = popen.stdout.read()
except:
        sys.exit(1)

results = []
output = out.split("\n")
for line in output:
        r = re.match("^\t\t(\\\\\S+[^$\s])\s+(.*)$", line)
        if r is not None:
                share = r.group(1)
                desc = r.group(2).strip()
                results.append({share : desc})
                continue
print json.dumps(results)
sys.exit(0)


michaelk 09-21-2013 07:57 AM

The only utility to see NFS shares that I know about is showmount. It is per server i.e. it can not browse a network.

displace 09-21-2013 01:19 PM

Thank you.
Do you perhaps know how to discover local FTP shares (without using a port scanner like nmap)?
I'm hoping for a ftp-equivalent of the "smbtree" and "showmount" commands.

~dis

michaelk 09-21-2013 03:00 PM

No browsing tool. You could use cURL to test for a FTP server. However, since you posted that the NASs would already be configured I would expect that the IP address and the top level shares/protocols would already be defined. By the way smbtree will find all cifs shares regardless if they are on a NAS or other PC with sharing enabled.

displace 09-21-2013 05:00 PM

Quote:

Originally Posted by michaelk (Post 5032159)
No browsing tool. You could use cURL to test for a FTP server. However, since you posted that the NASs would already be configured I would expect that the IP address and the top level shares/protocols would already be defined.

Erm the point is to help the user so that he won't have to remember and manually enter hostname or the IP address of the NAS - just provide a list instead. Though manual configuration is still an option.

Quote:

Originally Posted by michaelk (Post 5032159)
By the way smbtree will find all cifs shares regardless if they are on a NAS or other PC with sharing enabled.

Correct. This is exactly what I'm looking for. A user can easily configure a PC to act like a NAS. I mean I've done it myself ages ago.


Alright then. I believe I have all the information I was looking for. I recall nmap had some interesting lua scripts to scan FTP servers. I'll see if I can adapt them for my use or at least write something similar and simpler.

~dis


All times are GMT -5. The time now is 04:22 PM.