saulpw / visidata

A terminal spreadsheet multitool for discovering and arranging data
http://visidata.org
GNU General Public License v3.0
7.93k stars 283 forks source link

Type as: IPv4 #1782

Closed ssnkhan closed 1 year ago

ssnkhan commented 1 year ago

It would be helpful to be able to cast values as an IPv4 datatype, and then allow filtering of that data in CIDR notation. Alternatively if someone has a suggestion on how to achieve this currently, I would be very grateful!

Thank you kindly for developing such a fantastic tool.

saulpw commented 1 year ago

Hi @ssnkhan, thanks for the request. If you can share the Python methods you currently use to parse and filter IPv4 addresses, maybe some sample code, we could probably pull something together in short order.

Kondo'ed for now.

ajkerrigan commented 1 year ago

I've got some snippets in my ~/.visidatarc that may be relevant here:

import ipaddress

TableSheet.addCommand(
    None,
    "type-ipaddr",
    "cursorCol.type=ipaddress.ip_address",
    "set type of current column to IP address",
)
TableSheet.addCommand(
    None,
    "type-ipnet",
    "cursorCol.type=ipaddress.ip_network",
    "set type of current column to IP network",
)

@Column.api
def select_supernets(col, addr):
    sheet = col.sheet
    addr = ipaddress.ip_network(addr.strip())
    vd.status(f'selecting rows where {col.name} is a supernet of "{str(addr)}"')
    sheet.select(
        [
            row
            for row in sheet.rows
            if addr.subnet_of(ipaddress.ip_network(col.getValue(row).strip()))
        ]
    )

BaseSheet.addCommand(
    "",
    "select-supernets",
    'cursorCol.select_supernets(inputExpr("ip or cidr block: "))',
    "select rows where the CIDR block contains a provided IP address or network",
)

I wouldn't guess that all of this makes sense out of the box, but maybe at least the type conversions do? I mostly used select-supernets when I was looking at a sheet full of CIDR blocks and trying to quickly find which ones contained an IP address I cared about. Which sounds like it might be in the neighborhood of what @ssnkhan is trying to do.

saulpw commented 1 year ago

@ajkerrigan This is good stuff, can we put this in visidata/features/type_ipaddr.py? I think the only thing I would change is to move the command to TableSheet instead (since it depends on cursorCol, which BaseSheet doesn't necessary have).