Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 107 lines (86 sloc) 3.1 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#!/usr/bin/env python
#
# Copyright (C) 2009-2012:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
# Gregory Starck, g.starck@gmail.com
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

import optparse
import subprocess

VERSION = '1.0'

def p_debug(s):
    if debug:
        print "DEBUG:", s

def get_elements(line):
    elts = line.split('|', 2)
    if len(elts) < 2:
        p_debug("Not a good line: %r" % line)
        return None
    return elts


parser = optparse.OptionParser(
    "%prog [options] -H HOSTADRESS -u DOMAIN\\USER -p PASSWORD",
    version="%prog " + VERSION)

parser.add_option('-H', "--hostname",
                  help="Hostname to scan")
parser.add_option('-u', '--user', default='guest',
                  help="Username to scan with. Default to '%default'")
parser.add_option('-p', '--password', default='',
                  help="Password of your user. Default to ''")
parser.add_option('-d', "--debug", action='store_true',
                  help="Debug mode")

opts, args = parser.parse_args()

if not opts.hostname:
    parser.error("Requires one host to scan (option -H)")

hostname = opts.hostname
user = opts.user
debug = opts.debug
password = opts.password

cred = '%s%%%s' % (user, password)

cmd = ["smbclient", '--user', cred, '--grepable', '-L', hostname]
p_debug("Launching command %s" % cmd)
try:
    process = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        close_fds=True)
except OSError, exp:
    print "Error in launching command:", cmd, exp
    raise SystemExit(2)

p_debug("Try to communicate with the subprocess")
(stdoutdata, stderrdata) = process.communicate()

if process.returncode != 0:
    print "Error: the share scanner return an error: '%s'" % (stderrdata + stdoutdata)
    raise SystemExit(2)

disks = []
printers = []

p_debug("Good return" + stdoutdata)


for line in stdoutdata.splitlines():
    elts = get_elements(line.strip())
    # Skip strange lines
    if not elts:
        continue
    typ, sharename, desc = elts
    if typ == 'Printer':
        printers.append(sharename)
    if typ == 'Disk' and not sharename.endswith('$'):
        disks.append(sharename)


if len(disks) > 0:
    print "%s::shares_detected=1" % hostname
    print "%s::_shares=%s" % (hostname, ','.join(disks))

if len(printers) > 0:
    print "%s::printers_detected=1" % hostname
    print "%s::_printers=%s" % (hostname, ','.join(printers))
Something went wrong with that request. Please try again.