def run(self, searchstring, password=False, show_hidden=False, search_all=False, regexp=False, max_results=500): self.transport.cmd.setSessionTimeout(60 * 60) # One hour. if search_all: regexp = True attr_limit = [] if not search_all: attr_limit = ['name'] username = None if password: if '@' in searchstring: username, searchstring = searchstring.split('@', 1) devices = utils.search_device(self.object_store, searchstring, attr_limit, not regexp, max_results) if password: if len(devices) == 0: raise errors.SiptrackError('no devices matched') if len(devices) > 1: raise errors.SiptrackError('matched to many devices') device = devices[0] self._displayUserPassword(username, device) else: device = utils.select_device_from_list(devices) self._displayDisabled(device) self._displayAttributes(device, show_hidden) self._displayNetworks(device) self._displayAssociatedDevices(device) self._displayPasswords(device)
def run(self, device_name, config_name, timestamp=None): devices = self.object_store.search(device_name, include=['device'], attr_limit=['name']) if len(devices) == 0: raise errors.SiptrackError('no devices matched') if len(devices) > 1: raise errors.SiptrackError('matched to many devices') device = devices[0] match = None for config in device.listChildren(include=['device config']): if config.name == config_name: match = config break if not match: raise errors.SiptrackError( 'matched device but no config with that name found') if timestamp: res = config.getTimestampConfig(int(timestamp)) else: res = config.getLatestConfig() if res: res, _ = res if not res: raise errors.SiptrackError('no config data submitted') print(res) return 0
def _created(self): if self.username == None: raise errors.SiptrackError('invalid username in user object') if self.password == None: raise errors.SiptrackError('invalid password in user object') if self.administrator == None: raise errors.SiptrackError( 'invalid administrator value in user object') self.oid = self.transport.add(self.parent.oid, self.username, self.password, self.administrator) self.password = None
def run(self, daemonize=False): try: import pygtk except ImportError: raise errors.SiptrackError('pygtk >= 2.0 required') try: pygtk.require('2.0') except AssertionError: raise errors.SiptrackError('pygtk >= 2.0 required') import cmdgtkconnect cmdgtkconnect.cmd_gtkconnect(self.cm, daemonize) return 0
def range(self, range_string): if self.protocol == 'ipv4': return ipv4.range_from_string(range_string) elif self.protocol == 'ipv6': return ipv6.range_from_string(range_string) else: raise errors.SiptrackError('unknown protocol in network tree')
def address(self, address_string): if self.protocol == 'ipv4': return ipv4.address_from_string(address_string) elif self.protocol == 'ipv6': return ipv6.address_from_string(address_string) else: raise errors.SiptrackError('unknown protocol in network tree')
def getNetworkOrRange(self, string, create_if_missing = True): if self.isValidAddress(string): return self.getNetwork(string, create_if_missing) elif self.isValidRange(string): return self.getRange(string, create_if_missing) else: raise errors.SiptrackError('invalid address/range string')
def isValidRange(self, range): if self.protocol == 'ipv4': return ipv4.is_valid_range_string(range) elif self.protocol == 'ipv6': return ipv6.is_valid_range_string(range) else: raise errors.SiptrackError('unknown protocol in network tree')
def isValidAddress(self, address): if self.protocol == 'ipv4': return ipv4.is_valid_address_string(address) elif self.protocol == 'ipv6': return ipv6.is_valid_address_string(address) else: raise errors.SiptrackError('unknown protocol in network tree')
def _created(self): if type(self.server) not in [unicode, str]: raise errors.SiptrackError( 'invalid server in UserManagerActiveDirectory object') if type(self.base_dn) not in [unicode, str]: raise errors.SiptrackError( 'invalid base dn in UserManagerActiveDirectory object') if type(self.valid_groups) is not list: raise errors.SiptrackError( 'invalid valid groups in UserManagerActiveDirectory object') if type(self.user_domain) not in [unicode, str]: raise errors.SiptrackError( 'invalid user domain in UserManagerActiveDirectory object') self.oid = self.transport.add(self.parent.oid, self.server, self.base_dn, self.valid_groups, self.user_domain)
def createObject(self, class_id, *args, **kwargs): if class_id not in self.object_classes: raise errors.SiptrackError( 'trying to create object with invalid class id \'%s\'' % (class_id)) object_class = self.object_classes[class_id] obj = object_class.class_reference(*args, **kwargs) return obj
def _created(self): if self.address is None: raise errors.SiptrackError('invalid address in network object') self.address = ipaddr.IPNetwork(self.address, version=6, mask_address=True) # Use self.address.printableCIDR() rather then the (original) # self.address string so that /32 is added if necessary. self.oid = self.transport.add(self.parent.oid, str(self.address))
def _displayUserPassword(self, username, device): passwords = list(device.listChildren(include=['password'])) if not username and len(passwords) > 1: raise errors.SiptrackError('matched to many passwords') for password in passwords: if password.attributes.get('username') == username: print(password.password) break
def unlink(self, other): if self.isAssociated(other): self.disassociate(other) elif other.isAssociated(self): other.disassociate(self) else: raise errors.SiptrackError('not linked: %s' % other) return True
def createChildByID(self, class_id, *args, **kwargs): if not object_registry.isValidChild(self.class_id, class_id): raise errors.SiptrackError( 'trying to create child of invalid type \'%s\' for type \'%s\' (oid: %s)' % (class_id, self.class_id, self.oid)) child = object_registry.createObject(class_id, self, *args, **kwargs) self.children.append(child) self._children_sorted = False return child
def run(self, device_name, config_name): devices = self.object_store.search(device_name, include=['device'], attr_limit=['name']) if len(devices) == 0: raise errors.SiptrackError('no devices matched') if len(devices) > 1: raise errors.SiptrackError('matched to many devices') device = devices[0] match = None for config in device.listChildren(include=['device config']): if config.name == config_name: match = config break if not match: config = device.add('device config', config_name, 50) data = self._getSTDINData() config.addConfig(data) return 0
def _created(self): if type(self.connection_type) not in [unicode, str]: raise errors.SiptrackError( 'invalid connection type in UserManagerLDAP object') if type(self.server) not in [unicode, str]: raise errors.SiptrackError( 'invalid server in UserManagerLDAP object') if type(self.port) not in [unicode, str]: raise errors.SiptrackError( 'invalid port in UserManagerLDAP object') if type(self.base_dn) not in [unicode, str]: raise errors.SiptrackError( 'invalid base dn in UserManagerLDAP object') if type(self.valid_groups) is not list: raise errors.SiptrackError( 'invalid valid groups in UserManagerLDAP object') self.oid = self.transport.add(self.parent.oid, self.connection_type, self.server, self.port, self.base_dn, self.valid_groups)
def findMissingNetworks(self): """Return a list of unallocated subnets of the network tree. The list consist of UnallocatedNetwork objects. """ missing = self.transport.findMissingNetworks(self.oid) if self.protocol == 'ipv4': return ipv4.parse_missing_networks_list(missing) elif self.protocol == 'ipv6': return ipv6.parse_missing_networks_list(missing) else: raise errors.SiptrackError('unknown protocol in network tree')
def select_device_from_list(devices): if len(devices) == 0: raise errors.SiptrackError('no matching device found') elif len(devices) == 1: device = devices[0] else: options = [ PicklistOption(device.attributes.get('name'), device) for device in devices ] device = pick_from_list(options, 'Select matching device').value return device
def split_devicename(devicename): """Split a [username@]hostname string. If there is no @ in the string it will be considered a hostname only. """ username = None hostname = devicename if '@' in devicename: try: username, hostname = devicename.split('@') except ValueError: raise errors.SiptrackError('invalid devicename') return username, hostname
def range_from_string(range): """Return a Range object matching an range string. The range string must be two ipv6 address, start and end. End must be equal to or higher than start If a Range object is passed in it is returned untouched. """ if type(range) not in [str, unicode]: return range split = range.split() if len(split) != 2: raise errors.SiptrackError('invalid range string') return Range(split[0], split[1])
def _connectSSH(self, hostname, username, password): if not self.win32_ssh_bin: if os.path.isfile(DEFAULT_PUTTY_BIN): self.win32_ssh_bin = DEFAULT_PUTTY_BIN self.win32_ssh_pwopt = DEFAULT_PUTTY_PWOPT else: raise errors.SiptrackError('set the "win32-ssh-bin" option in your config file') sshcmd = [self.win32_ssh_bin, '%s@%s' % (username, hostname)] if self.win32_ssh_pwopt: sshcmd.append(self.win32_ssh_pwopt) sshcmd.append(password) if self.win32_ssh_extraopt: sshcmd += self.win32_ssh_extraopt.split() self.addStringToClipboard(password) subprocess.Popen(sshcmd)
def connect(self, device, username, password, hostname): host_os = device.attributes.get('os') if host_os == 'linux': print 'Trying ssh connection to %s@%s with password %s' % (username, hostname, password) self._connectSSH(hostname, username, password) elif host_os == 'windows': print 'Trying rdp connection to %s@%s with password %s' % (username, hostname, password) self._connectRDP(hostname, username, password) else: if self._checkTCPPort(hostname, 3389): print 'Trying rdp connection to %s@%s with password %s' % (username, hostname, password) self._connectRDP(hostname, username, password) elif self._checkTCPPort(hostname, 22): print 'Trying ssh connection to %s@%s with password %s' % (username, hostname, password) self._connectSSH(hostname, username, password) else: raise errors.SiptrackError('Sorry, I don\'t know how to connect to this device')
def __setitem__(self, item, value): atype = 'text' if type(value) in [list, tuple]: if len(value) != 2: raise errors.SiptrackError('invalid attribute dict access') atype, value = value elif type(value) == bool: atype = 'bool' elif type(value) == int: atype = 'int' attr_exists = False for attr in self._listAttributes(): if attr.name == item: attr.value = value attr_exists = True if not attr_exists: attribute = self.real.add('attribute', item, atype, value)
def copy(self, target, include_nodes=[], exclude_nodes=[], include_links=[], exclude_links=[], skip_safety_check=False): if not hasattr(self, '_copySelf'): return if not self._isSafeCopyTarget(target): raise errors.SiptrackError('unsafe copy target: %s' % (target)) copy = self._copySelf(target) for node in self.listChildren(include=include_nodes, exclude=exclude_nodes): node.copy(copy, include_nodes, exclude_nodes, include_links, exclude_links, skip_safety_check=True) for node in self.listLinks(include=include_links, exclude=exclude_links): copy.associate(node) return copy
def addChildByName(self, class_name, *args, **kwargs): """Identical to addChildByID, but with class_name.""" class_id = object_registry.getIDByName(class_name) if class_id is None: raise errors.SiptrackError('unknown class name for creating child') return self.addChildByID(class_id, *args, **kwargs)
def _created(self): if self.protocol == None: raise errors.SiptrackError('invalid protocol in network tree') self.oid = self.transport.add(self.parent.oid, self.protocol)
def _created(self): if self.range is None: raise errors.SiptrackError('invalid range in network range object') self.range = self.rangeFromString(self.range) self.oid = self.transport.add(self.parent.oid, self.range.transportable())