Skip to content

Commit f4fa975

Browse files
fix: check if the table uses ip or host for hosts before adding
1 parent 166df14 commit f4fa975

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

nxc/database.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,32 @@ def initialize_db():
112112
init_protocol_dbs("default")
113113

114114
def format_host_query(q, filter_term, HostsTable):
115-
# one annoying thing is that if you search for an ip such as '10.10.10.5', it will return 10.10.10.5 and 10.10.10.52, so we have to check if its an ip address first
115+
"""One annoying thing is that if you search for an ip such as '10.10.10.5',
116+
it will return 10.10.10.5 and 10.10.10.52, so we have to check if its an ip address first
117+
"""
118+
# the FTP and SSH protocols call the column host instead of IP
119+
# TODO: normalize these column names
120+
if hasattr(HostsTable.c, "ip"):
121+
ip_column = HostsTable.c.ip
122+
nxc_logger.debug("Using 'ip' column for filtering")
123+
elif hasattr(HostsTable.c, "host"):
124+
ip_column = HostsTable.c.host
125+
nxc_logger.debug("Using 'host' column for filtering")
126+
else:
127+
nxc_logger.debug("Neither 'ip' nor 'host' columns found in the table")
128+
return q
129+
130+
# first we check if its an ip address
116131
try:
117132
ipaddress.ip_address(filter_term)
118133
nxc_logger.debug(f"filter_term is an IP address: {filter_term}")
119-
q = q.filter(HostsTable.c.ip == filter_term)
134+
q = q.filter(ip_column == filter_term)
120135
except ValueError:
121136
nxc_logger.debug(f"filter_term is not an IP address: {filter_term}")
122137
like_term = func.lower(f"%{filter_term}%")
123-
q = q.filter(HostsTable.c.ip.like(like_term) | func.lower(HostsTable.c.hostname).like(like_term))
138+
139+
# check if the hostname column exists for hostname searching
140+
q = q.filter(ip_column.like(like_term) | func.lower(HostsTable.c.hostname).like(like_term)) if hasattr(HostsTable.c, "hostname") else q.filter(ip_column.like(like_term))
124141

125142
return q
126143

0 commit comments

Comments
 (0)