1
2
3
4
5
6
7
8
9
10
11
12
13
14 import logging
15
16 from twisted.internet import reactor, error
17 from twisted.python import failure
18
19 import Globals
20
21 from Products.ZenUtils import PortScan
22
23 log = logging.getLogger("zen.PortscanClient")
24
26
29 self.hostname = hostname
30 self.device = device
31 self.options = options
32 self.datacollector = datacollector
33 self.plugins = plugins
34 self.results = []
35
36 maxPort = getattr(device,'zIpServiceMapMaxPort', 1024)
37 self.portRange = (1, maxPort)
38
39
40 self.portList = []
41
42 if self.portList:
43 kwds = {'portList': self.portList}
44 else:
45 kwds = {'portRange': self.portRange}
46 kwds.update(dict(timeout=self.options.portscantimeout))
47 self.scanner = PortScan.Scanner(ipaddr, **kwds)
48
50 """
51 Start portscan collection.
52 """
53 for plugin in self.plugins:
54 pluginName = plugin.name()
55 log.debug("sending queries for plugin %s", pluginName)
56 d = self.scanner.prepare()
57 d.addCallback(self._cbScanComplete, pluginName)
58 d.addErrback(self._ebScanError, pluginName)
59
61 log.debug("received plugin:%s getOids", pluginName)
62 self.clientFinished(pluginName)
63
65 """Handle an error generated by one of our requests.
66 """
67 self.clientFinished(pluginName)
68 log.debug('device %s plugin %s %s', self.hostname, pluginName, err)
69 if isinstance(err, failure.Failure):
70 actualError = err.value
71 trace = err.getTraceback()
72 else:
73 actualError = err
74 trace = log.getException(err)
75 log.error(
76 """device %s plugin %s unexpected error: %s""",
77 self.hostname, pluginName, trace,
78 )
79
81 """
82 ZenUtils.PortScan records open ports in a list that are the
83 values for a key in a dict where the key is the IP address
84 scanned.
85
86 For example, if we scan host 10.0.4.55 and ports 22 and 80 are
87 open, getSuccesses() will return the following:
88
89 {'10.0.4.55': [22, 80]}
90 """
91 return self.results
92
94 """
95 Tell the datacollector that we are all done.
96 """
97 log.info("portscan client finished collection for %s" % self.hostname)
98
99
100
101 data = (pluginName, self.scanner.getSuccesses())
102 self.results.append(data)
103 if self.datacollector:
104 self.datacollector.clientFinished(self)
105 else:
106 reactor.stop()
107
109 "build options list that both telnet and ssh use"
110
111 if not usage:
112 usage = "%prog [options] hostname[:port] oids"
113
114 if not parser:
115 from optparse import OptionParser
116 parser = OptionParser(usage=usage)
117
118
119
120
121
122
123
124 if __name__ == "__main__":
125 import sys
126 sys.path.extend([
127 '/usr/local/zenoss/Products/DataCollector/plugins',
128 ])
129 import pprint
130 from zenoss.portscan import IpServiceMap
131
132 logging.basicConfig()
133 log = logging.getLogger()
134 log.setLevel(20)
135 ipmap = IpServiceMap.IpServiceMap()
136 psc = PortscanClient("localhost", '127.0.0.1', plugins=[ipmap,])
137 psc.run()
138 reactor.run()
139 pprint.pprint(psc.getResults())
140