def connect(self): inventory = Inventory() address = self.hostname if inventory.has_option(self.hostname, 'address'): address = inventory.get(self.hostname, 'address') ip = None nb_name = None try: socket.inet_aton(address) ip = address except OSError as e: nb_name = address nb = NetBIOS() if ip is not None and nb_name is None: # need to look up the hostname logger.debug('Looking up NetBIOS name from IP ' + ip) nb_names = nb.queryIPForName(ip) if nb_names is None or len(nb_names) < 1: raise RuntimeError('Cannot connect to host ' + self.hostname + '; looking up NetBIOS name failed') nb_name = nb_names[0] elif ip is None and nb_name is not None: # not a IPv4 address, need to look up the ip nb_name = address logger.debug('Looking up NetBIOS IP from name ' + nb_name) ips = nb.queryName(nb_name) if ips is None or len(ips) < 1: raise RuntimeError('Cannot connect to host ' + self.hostname + '; looking up NetBIOS IP failed') ip = ips[0] nb.close() if inventory.has_option(self.hostname, 'username') and inventory.has_option( self.hostname, 'password'): username = inventory.get(self.hostname, 'username') password = inventory.get(self.hostname, 'password') client_machine_name = ''.join( random.choice(string.ascii_letters + string.digits) for _ in range(15)) logger.debug('Using client name of ' + client_machine_name) logger.info('Connecting to ' + nb_name + ' as ' + username + ' for host ' + self.hostname) self.connection = SMBHost(username, password, client_machine_name, nb_name, use_ntlm_v2=True, sign_options=SMBHost.SIGN_WHEN_SUPPORTED ) #, is_direct_tcp=True) if not self.connection.connect(ip): raise RuntimeError('Cannot connect to host ' + self.hostname + '; connecting via SMB failed') else: raise RuntimeError('No method of authenticating with host ' + self.hostname + ' found') print(str(self.connection.listPath('ADMIN$', '\\')))
def getBIOSIp(remote_smb_name, timeout=30): try: bios = NetBIOS() server_ip_list = bios.queryName(remote_smb_name, timeout=timeout) except: print >> sys.stderr, "Looking up timeout, check remote_smb_name again!!" finally: bios.close() return server_ip_list
def getIP(self): # smb name -> ip try: self.error = None bios = NetBIOS() ip = bios.queryName(self.smb_name) return ip[0] except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) return None
def __resolve__(self, server:str): # str | None # If no valid IP adres, try to obtain it # First as netBIOS on LAN nb = NetBIOS(broadcast=True, listen_port=0) # Send a query on the network and hopes that if machine matching the name will reply with its IP address. res = nb.queryName(server, ip='', port=NETBIOS_PORT, timeout=NETBIOS_TIMEOUT) if isinstance(res, list) and len(res) > 0 and isinstance(res[0], str) and self.isIP4(res[0]): return res[0] # Try to DNS resolve try: return socket.gethostbyname(server) except socket.gaierror as sge: # Nope, didn't work return None
def getIP(self): # smb name -> ip try: self.error = None bios = NetBIOS() ip = bios.queryName(self.smb_name) return ip[0] except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) return None
print('Target is an IP address') target_ip = target except OSError as e: print('Target is not an IP address, trying DNS resolution') try: target_ip = socket.gethostbyname(target) print('Target is DNS resolvable') except socket.gaierror as e: print('Target is not DNS resolvable, assuming NB name') target_nb_name = target nb = NetBIOS() if target_ip is None: print('Looking up IP from target NetBIOS name ' + target_nb_name) ips = nb.queryName(target_nb_name) print('Got IPs:' + str(ips)) if ips is None or len(ips) < 1: raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS IP failed') target_ip = ips[0] if target_nb_name is None: print('Looking up NetBIOS name from target IP: ' + target_ip) nb_names = nb.queryIPForName(target_ip) print('Got NB names: ' + str(nb_names)) if nb_names is None or len(nb_names) < 1: raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS name failed') target_nb_name = nb_names[0] nb.close()
# needed pysmb, netaddr import sys from nmb.NetBIOS import NetBIOS from netaddr import IPNetwork netbios = NetBIOS() for ip in IPNetwork(sys.argv[1]): netbios_names = netbios.queryIPForName(str(ip), timeout=0.1) if netbios_names: print(', '.join( netbios.queryName(netbios_names[0], ip=str(ip)) + netbios_names))
def queryNam(name): n = NetBIOS(broadcast=True, listen_port=0) ip = n.queryName(name, timeout=0.3) return ip
def test_broadcast(): global conn conn = NetBIOS() assert conn.queryName('MICHAEL-I5PC', timeout=10)
def getServerIP(self): q = NetBIOS() self.server_ip = q.queryName(self.server_name)[0] q.close()
print('Target is an IP address') target_ip = target except OSError as e: print('Target is not an IP address, trying DNS resolution') try: target_ip = socket.gethostbyname(target) print('Target is DNS resolvable') except socket.gaierror as e: print('Target is not DNS resolvable, assuming NB name') target_nb_name = target nb = NetBIOS() if target_ip is None: print('Looking up IP from target NetBIOS name ' + target_nb_name) ips = nb.queryName(target_nb_name) print('Got IPs:' + str(ips)) if ips is None or len(ips) < 1: raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS IP failed') target_ip = ips[0] if target_nb_name is None: print('Looking up NetBIOS name from target IP: ' + target_ip) nb_names = nb.queryIPForName(target_ip) print('Got NB names: ' + str(nb_names)) if nb_names is None or len(nb_names) < 1: raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS name failed') target_nb_name = nb_names[0]
def test_broadcast(): global conn conn = NetBIOS() assert conn.queryName('MICHAEL-I5PC', timeout = 10)