boB Rudis
4 years ago
20 changed files with 3157 additions and 16 deletions
@ -0,0 +1,2 @@ |
|||
YEAR: 2020 |
|||
COPYRIGHT HOLDER: Bob Rudis |
@ -0,0 +1,21 @@ |
|||
# MIT License |
|||
|
|||
Copyright (c) 2020 Bob Rudis |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
@ -1,4 +1,7 @@ |
|||
# Generated by roxygen2: do not edit by hand |
|||
|
|||
import(httr) |
|||
importFrom(jsonlite,fromJSON) |
|||
export(create_cidr_lookup_table) |
|||
export(create_ip_source_table) |
|||
export(find_ips_in_cidrs) |
|||
importFrom(glue,glue) |
|||
importFrom(readr,write_csv) |
|||
|
@ -0,0 +1,2 @@ |
|||
ac <- as.character |
|||
gg <- glue::glue |
@ -0,0 +1,83 @@ |
|||
#' Creates a CIDR lookup table |
|||
#' |
|||
#' The columns named in `entity_col` and `cidr_col` *must exist* in the |
|||
#' data frame or CSV file. All `_name`s must be valid PostgreSQL syntax for |
|||
#' said names. Setting `drop=TRUE` is _destructive_.\cr |
|||
#' \cr |
|||
#' NOTE that `cidr_col` is a primary key. Thus duplicate CIDRs are not allowed |
|||
#' and it's up to you to ensure that. |
|||
#' |
|||
#' @param pgcon a PostgreSQL DBI connection |
|||
#' @param tbl_name name of the CIDR table (valid PG table name syntax) |
|||
#' @param drop drop existing CIDR table if it has the same name? Defaults to `FALSE`. |
|||
#' @param xdf Data frame to use to populate the table or a path to a CSV file. |
|||
#' If a path it will be [path.expand()]ed. |
|||
#' @param entity_col Name of the column that holds the entity reference name for the CIDR |
|||
#' @param cidr_col Name of the column that holds the CIDR block |
|||
#' @export |
|||
#' @examples \dontrun{ |
|||
#' DBI::dbConnect( |
|||
#' odbc::odbc(), |
|||
#' driver = "/usr/local/lib/psqlodbca.so", |
|||
#' Database = "working", |
|||
#' Host = "localhost" |
|||
#' ) -> con |
|||
#' |
|||
#' create_cidr_lookup_table( |
|||
#' pgcon = con, |
|||
#' tbl_name = "amazon_cidrs", |
|||
#' drop = TRUE, |
|||
#' xdf = system.file("extdat", "amzn-cidrs.csv", package = "pgcidr"), |
|||
#' ) |
|||
#' |
|||
#' create_ip_source_table( |
|||
#' pgcon = con, |
|||
#' tbl_name = "weblog", |
|||
#' drop = TRUE, |
|||
#' xdf = system.file("extdat", "weblog.csv", package = "pgcidr") |
|||
#' ) |
|||
#' |
|||
#' find_ips_in_cidrs( |
|||
#' con, "weblog", "amazon_cidrs" |
|||
#' ) |
|||
#' |
|||
#' } |
|||
create_cidr_lookup_table <- function(pgcon, tbl_name, drop=FALSE, xdf, |
|||
entity_col="entity", cidr_col="cidr") { |
|||
|
|||
stopifnot(inherits(con, "PostgreSQL")) |
|||
|
|||
tbl_name <- tbl_name[1] |
|||
idx_name <- ac(gg("{tbl_name}_idx")) |
|||
|
|||
entity_col <- entity_col[1] |
|||
cidr_col <- cidr_col[1] |
|||
|
|||
if (inherits(xdf, "character")) { |
|||
|
|||
tf <- path.expand(xdf[1]) |
|||
stopifnot(file.exists(tf)) |
|||
|
|||
} else { |
|||
|
|||
tf <- tempfile(fileext = ".csv") |
|||
on.exit(unlink(tf)) |
|||
|
|||
xdf <- xdf[,c(entity_col, cidr_col)] |
|||
readr::write_csv(xdf, tf) |
|||
|
|||
} |
|||
|
|||
if (drop) { |
|||
dbExecute(con, ac(gg("DROP TABLE IF EXISTS {tbl_name}"))) |
|||
dbExecute(con, ac(gg("DROP INDEX IF EXISTS {idx_name}"))) |
|||
} |
|||
|
|||
dbExecute(con, ac(gg("CREATE TABLE {tbl_name}({entity_col} text not null, {cidr_col} ip4r primary key)"))) |
|||
|
|||
dbExecute(con, ac(gg("COPY {tbl_name} FROM '{tf}' WITH (format csv, header)"))) |
|||
dbExecute(con, ac(gg("CREATE INDEX {idx_name} ON {tbl_name} USING gist({cidr_col})"))) |
|||
|
|||
invisible(NULL) |
|||
|
|||
} |
@ -0,0 +1,78 @@ |
|||
#' Creates a IP address table |
|||
#' |
|||
#' The column named in `ip_col` ` *must exist* in the |
|||
#' data frame or CSV file. All `_name`s must be valid PostgreSQL syntax for |
|||
#' said names. Setting `drop=TRUE` is _destructive_. |
|||
#' |
|||
#' @param pgcon a PostgreSQL DBI connection |
|||
#' @param tbl_name name of the IP table (valid PG table name syntax) |
|||
#' @param drop drop existing IP table if it has the same name? Defaults to `FALSE`. |
|||
#' @param xdf Data frame to use to populate the table or a path to a CSV file. |
|||
#' If a path it will be [path.expand()]ed. |
|||
#' @param ip_col Name of the column that holds the IP addresses |
|||
#' @export |
|||
#' @examples \dontrun{ |
|||
#' DBI::dbConnect( |
|||
#' odbc::odbc(), |
|||
#' driver = "/usr/local/lib/psqlodbca.so", |
|||
#' Database = "working", |
|||
#' Host = "localhost" |
|||
#' ) -> con |
|||
#' |
|||
#' create_cidr_lookup_table( |
|||
#' pgcon = con, |
|||
#' tbl_name = "amazon_cidrs", |
|||
#' drop = TRUE, |
|||
#' xdf = system.file("extdat", "amzn-cidrs.csv", package = "pgcidr"), |
|||
#' ) |
|||
#' |
|||
#' create_ip_source_table( |
|||
#' pgcon = con, |
|||
#' tbl_name = "weblog", |
|||
#' drop = TRUE, |
|||
#' xdf = system.file("extdat", "weblog.csv", package = "pgcidr") |
|||
#' ) |
|||
#' |
|||
#' find_ips_in_cidrs( |
|||
#' con, "weblog", "amazon_cidrs" |
|||
#' ) |
|||
#' |
|||
#' } |
|||
create_ip_source_table <- function(pgcon, tbl_name, drop=FALSE, xdf, ip_col = "ip") { |
|||
|
|||
stopifnot(inherits(con, "PostgreSQL")) |
|||
|
|||
tbl_name <- tbl_name[1] |
|||
idx_name <- ac(gg("{tbl_name}_idx")) |
|||
|
|||
ip_col <- ip_col[1] |
|||
|
|||
if (inherits(xdf, "character")) { |
|||
|
|||
tf <- path.expand(xdf[1]) |
|||
stopifnot(file.exists(tf)) |
|||
|
|||
} else { |
|||
|
|||
tf <- tempfile(fileext = ".csv") |
|||
on.exit(unlink(tf)) |
|||
|
|||
xdf <- xdf[,c(ip_col)] |
|||
readr::write_csv(xdf, tf) |
|||
|
|||
} |
|||
|
|||
if (drop) { |
|||
|
|||
dbExecute(con, ac(gg("DROP TABLE IF EXISTS {tbl_name}"))) |
|||
dbExecute(con, ac(gg("DROP INDEX IF EXISTS {idx_name}"))) |
|||
|
|||
} |
|||
dbExecute(con, ac(gg("CREATE TABLE {tbl_name}({ip_col} ip4 not null)"))) |
|||
|
|||
dbExecute(con, ac(gg("COPY {tbl_name} FROM '{tf}' WITH (format csv, header)"))) |
|||
dbExecute(con, ac(gg("CREATE INDEX {idx_name} ON {tbl_name}({ip_col})"))) |
|||
|
|||
invisible(NULL) |
|||
|
|||
} |
@ -0,0 +1,64 @@ |
|||
#' Finds IPs in CIDR blocks |
|||
#' |
|||
#' After you use [create_cidr_lookup_table()] and [create_ip_source_table()] |
|||
#' use this function to do the lookup. |
|||
#' |
|||
#' @param pgcon a PostgreSQL DBI connection |
|||
#' @param ip_tbl name of the IP table (valid PG table name syntax) |
|||
#' @param cidr_tbl name of the CIDR table (valid PG table name syntax) |
|||
#' @param ip_col Name of the column that holds the IP addresses in `ip_tbl` |
|||
#' @param cidr_col Name of the column that holds the CIDR block in `cidr_tbl` |
|||
#' @export |
|||
#' @examples \dontrun{ |
|||
#' DBI::dbConnect( |
|||
#' odbc::odbc(), |
|||
#' driver = "/usr/local/lib/psqlodbca.so", |
|||
#' Database = "working", |
|||
#' Host = "localhost" |
|||
#' ) -> con |
|||
#' |
|||
#' create_cidr_lookup_table( |
|||
#' pgcon = con, |
|||
#' tbl_name = "amazon_cidrs", |
|||
#' drop = TRUE, |
|||
#' xdf = system.file("extdat", "amzn-cidrs.csv", package = "pgcidr"), |
|||
#' ) |
|||
#' |
|||
#' create_ip_source_table( |
|||
#' pgcon = con, |
|||
#' tbl_name = "weblog", |
|||
#' drop = TRUE, |
|||
#' xdf = system.file("extdat", "weblog.csv", package = "pgcidr") |
|||
#' ) |
|||
#' |
|||
#' find_ips_in_cidrs( |
|||
#' con, "weblog", "amazon_cidrs" |
|||
#' ) |
|||
#' |
|||
#' } |
|||
find_ips_in_cidrs <- function(pgcon, ip_tbl, cidr_tbl, |
|||
ip_col = "ip", cidr_col = "cidr") { |
|||
|
|||
stopifnot(inherits(con, "PostgreSQL")) |
|||
|
|||
ip_tbl <- ip_tbl[1] |
|||
cidr_tbl <- cidr_tbl[1] |
|||
ip_col <- ip_col[1] |
|||
cidr_col <- cidr_col[1] |
|||
|
|||
DBI::dbGetQuery( |
|||
con, |
|||
ac(gg(" |
|||
SELECT |
|||
* |
|||
FROM {ip_tbl}, {cidr_tbl} WHERE {ip_col} <<= {cidr_col} |
|||
")) |
|||
) -> res |
|||
|
|||
if (inherits(res, "data.frame")) { |
|||
class(res) <- c("tbl_df", "tbl", "data.frame") |
|||
} |
|||
|
|||
res |
|||
|
|||
} |
@ -0,0 +1,42 @@ |
|||
#' @name macos_postgresql_setup_with_ip4r |
|||
#' @title Setting up macOS PostgreSQL 12 with ip4r extension |
|||
#' @section Setup: |
|||
#' ``` |
|||
#' $ brew uninstall postgresql |
|||
#' $ rm -rf /usr/local/var/postgres |
|||
#' |
|||
#' $ brew tap petere/postgresql |
|||
#' $ brew install petere/postgresql/postgresql@12 |
|||
#' $ brew install petere/postgresql/postgresql-common |
|||
#' $ initdb DATA_PATH -E utf8 |
|||
#' $ pg_ctl -D /Volumes/otg/postgres -l logfile start |
|||
#' |
|||
#' $ ps -ef | grep postgr |
|||
#' |
|||
#' $ pg_config |
|||
#' |
|||
#' $ createdb ${USER} |
|||
#' |
|||
#' $ psql |
|||
#' username=# \q |
|||
#' |
|||
#' $ cd /place/where/you/clone/stuff |
|||
#' $ git clone git@@github.com:RhodiumToad/ip4r.git |
|||
#' $ cd ip4r |
|||
#' $ make |
|||
#' $ sudo make install |
|||
#' |
|||
#' $ cd /place/where/you/clone/stuff |
|||
#' $ wget https://ftp.postgresql.org/pub/odbc/versions/src/psqlodbc-12.01.0000.tar.gz |
|||
#' $ tar -xvzf psqlodbc-12.01.0000.tar.gz |
|||
#' $ cd psqlodbc-12.01.0000 |
|||
#' $ ./configure |
|||
#' $ make -j 8 |
|||
#' $ sudo make install |
|||
#' $ ls -l /usr/local/lib/psqlodbca.so |
|||
#' |
|||
#' $ createdb working |
|||
#' $ psql --command="CREATE EXTENSION ip4r;" working |
|||
#' $ psql --command="CREATE EXTENSION ip4r;" ${USER} |
|||
#' ``` |
|||
NULL |
@ -1,9 +1,15 @@ |
|||
#' ... |
|||
#' |
|||
#' Perform IP Address in CIDR Lookups with PostgreSQL |
|||
#' |
|||
#' PostgreSQL has built-in support for IP address and CIDR types |
|||
#' but the ip4r <https://github.com/RhodiumToad/ip4r> extension is much faster. |
|||
#' Tools are provided to create CIDR lookup and IP address source tables and |
|||
#' perform IP address in CIDR queries. Documentation on how to setup macOS |
|||
#' with PostgreSQL and ip4r is also provided. |
|||
#' |
|||
#' @md |
|||
#' @name pgcidr |
|||
#' @keywords internal |
|||
#' @author Bob Rudis (bob@@rud.is) |
|||
#' @import httr |
|||
#' @importFrom jsonlite fromJSON |
|||
#' @importFrom glue glue |
|||
#' @importFrom readr write_csv |
|||
"_PACKAGE" |
|||
|
@ -0,0 +1,117 @@ |
|||
|
|||
[![Project Status: Active – The project has reached a stable, usable |
|||
state and is being actively |
|||
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) |
|||
[![Signed |
|||
by](https://img.shields.io/badge/Keybase-Verified-brightgreen.svg)](https://keybase.io/hrbrmstr) |
|||
![Signed commit |
|||
%](https://img.shields.io/badge/Signed_Commits-100%25-lightgrey.svg) |
|||
[![Linux build |
|||
Status](https://travis-ci.org/hrbrmstr/pgcidr.svg?branch=master)](https://travis-ci.org/hrbrmstr/pgcidr) |
|||
![Minimal R |
|||
Version](https://img.shields.io/badge/R%3E%3D-3.5.0-blue.svg) |
|||
![License](https://img.shields.io/badge/License-MIT-blue.svg) |
|||
|
|||
# pgcidr |
|||
|
|||
Perform IP Address in CIDR Lookups with PostgreSQL and ip4r |
|||
|
|||
## Description |
|||
|
|||
PostgreSQL has built-in support for IP address and CIDR types but the |
|||
ip4r <https://github.com/RhodiumToad/ip4r> extension is much faster. |
|||
Tools are provided to create CIDR lookup and IP address source tables |
|||
and perform IP address in CIDR queries. Documentation on how to setup |
|||
macOS with PostgreSQL and ip4r is also provided. |
|||
|
|||
## What’s Inside The Tin |
|||
|
|||
The following functions are implemented: |
|||
|
|||
- `create_cidr_lookup_table`: Creates a CIDR lookup table |
|||
- `create_ip_source_table`: Creates a IP address table |
|||
- `find_ips_in_cidrs`: Finds IPs in CIDR blocks |
|||
- `macos_postgresql_setup_with_ip4r`: Setting up macOS PostgreSQL 12 |
|||
with ip4r extension |
|||
|
|||
## Installation |
|||
|
|||
``` r |
|||
remotes::install_git("https://git.rud.is/hrbrmstr/pgcidr.git") |
|||
# or |
|||
remotes::install_git("https://git.sr.ht/~hrbrmstr/pgcidr") |
|||
# or |
|||
remotes::install_gitlab("hrbrmstr/pgcidr") |
|||
# or |
|||
remotes::install_bitbucket("hrbrmstr/pgcidr") |
|||
# or |
|||
remotes::install_github("hrbrmstr/pgcidr") |
|||
``` |
|||
|
|||
NOTE: To use the ‘remotes’ install options you will need to have the |
|||
[{remotes} package](https://github.com/r-lib/remotes) installed. |
|||
|
|||
## Usage |
|||
|
|||
``` r |
|||
library(pgcidr) |
|||
|
|||
# current version |
|||
packageVersion("pgcidr") |
|||
## [1] '0.1.0' |
|||
``` |
|||
|
|||
``` r |
|||
DBI::dbConnect( |
|||
odbc::odbc(), |
|||
driver = "/usr/local/lib/psqlodbca.so", |
|||
Database = "working", |
|||
Host = "localhost" |
|||
) -> con |
|||
|
|||
create_cidr_lookup_table( |
|||
pgcon = con, |
|||
tbl_name = "amazon_cidrs", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "amzn-cidrs.csv", package = "pgcidr"), |
|||
) |
|||
|
|||
create_ip_source_table( |
|||
pgcon = con, |
|||
tbl_name = "weblog", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "weblog.csv", package = "pgcidr") |
|||
) |
|||
|
|||
find_ips_in_cidrs( |
|||
con, "weblog", "amazon_cidrs" |
|||
) |
|||
|
|||
## # A tibble: 83 x 3 |
|||
## ip entity cidr |
|||
## <chr> <chr> <chr> |
|||
## 1 54.235.230.238 us-east-1 54.234.0.0/15 |
|||
## 2 54.227.107.78 us-east-1 54.226.0.0/15 |
|||
## 3 96.127.69.213 us-gov-west-1 96.127.0.0/17 |
|||
## 4 3.82.223.254 us-east-1 3.80.0.0/12 |
|||
## 5 3.83.153.45 us-east-1 3.80.0.0/12 |
|||
## 6 54.160.105.136 us-east-1 54.160.0.0/13 |
|||
## 7 54.225.41.245 us-east-1 54.224.0.0/15 |
|||
## 8 3.87.18.119 us-east-1 3.80.0.0/12 |
|||
## 9 54.91.31.71 us-east-1 54.88.0.0/14 |
|||
## 10 54.90.66.13 us-east-1 54.88.0.0/14 |
|||
## # … with 73 more rows |
|||
``` |
|||
|
|||
## pgcidr Metrics |
|||
|
|||
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) | |
|||
| :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | --: | |
|||
| R | 7 | 0.88 | 75 | 0.72 | 37 | 0.65 | 177 | 0.8 | |
|||
| Rmd | 1 | 0.12 | 29 | 0.28 | 20 | 0.35 | 44 | 0.2 | |
|||
|
|||
## Code of Conduct |
|||
|
|||
Please note that this project is released with a Contributor Code of |
|||
Conduct. By participating in this project you agree to abide by its |
|||
terms. |
File diff suppressed because it is too large
|
@ -0,0 +1,66 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/create-cidr-tbl.R |
|||
\name{create_cidr_lookup_table} |
|||
\alias{create_cidr_lookup_table} |
|||
\title{Creates a CIDR lookup table} |
|||
\usage{ |
|||
create_cidr_lookup_table( |
|||
pgcon, |
|||
tbl_name, |
|||
drop = FALSE, |
|||
xdf, |
|||
entity_col = "entity", |
|||
cidr_col = "cidr" |
|||
) |
|||
} |
|||
\arguments{ |
|||
\item{pgcon}{a PostgreSQL DBI connection} |
|||
|
|||
\item{tbl_name}{name of the CIDR table (valid PG table name syntax)} |
|||
|
|||
\item{drop}{drop existing CIDR table if it has the same name? Defaults to \code{FALSE}.} |
|||
|
|||
\item{xdf}{Data frame to use to populate the table or a path to a CSV file. |
|||
If a path it will be \code{\link[=path.expand]{path.expand()}}ed.} |
|||
|
|||
\item{entity_col}{Name of the column that holds the entity reference name for the CIDR} |
|||
|
|||
\item{cidr_col}{Name of the column that holds the CIDR block} |
|||
} |
|||
\description{ |
|||
The columns named in \code{entity_col} and \code{cidr_col} \emph{must exist} in the |
|||
data frame or CSV file. All \verb{_name}s must be valid PostgreSQL syntax for |
|||
said names. Setting \code{drop=TRUE} is \emph{destructive}.\cr |
|||
\cr |
|||
NOTE that \code{cidr_col} is a primary key. Thus duplicate CIDRs are not allowed |
|||
and it's up to you to ensure that. |
|||
} |
|||
\examples{ |
|||
\dontrun{ |
|||
DBI::dbConnect( |
|||
odbc::odbc(), |
|||
driver = "/usr/local/lib/psqlodbca.so", |
|||
Database = "working", |
|||
Host = "localhost" |
|||
) -> con |
|||
|
|||
create_cidr_lookup_table( |
|||
pgcon = con, |
|||
tbl_name = "amazon_cidrs", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "amzn-cidrs.csv", package = "pgcidr"), |
|||
) |
|||
|
|||
create_ip_source_table( |
|||
pgcon = con, |
|||
tbl_name = "weblog", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "weblog.csv", package = "pgcidr") |
|||
) |
|||
|
|||
find_ips_in_cidrs( |
|||
con, "weblog", "amazon_cidrs" |
|||
) |
|||
|
|||
} |
|||
} |
@ -0,0 +1,52 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/create-ip-tbl.R |
|||
\name{create_ip_source_table} |
|||
\alias{create_ip_source_table} |
|||
\title{Creates a IP address table} |
|||
\usage{ |
|||
create_ip_source_table(pgcon, tbl_name, drop = FALSE, xdf, ip_col = "ip") |
|||
} |
|||
\arguments{ |
|||
\item{pgcon}{a PostgreSQL DBI connection} |
|||
|
|||
\item{tbl_name}{name of the IP table (valid PG table name syntax)} |
|||
|
|||
\item{drop}{drop existing IP table if it has the same name? Defaults to \code{FALSE}.} |
|||
|
|||
\item{xdf}{Data frame to use to populate the table or a path to a CSV file. |
|||
If a path it will be \code{\link[=path.expand]{path.expand()}}ed.} |
|||
|
|||
\item{ip_col}{Name of the column that holds the IP addresses} |
|||
} |
|||
\description{ |
|||
The column named in \code{ip_col} \verb{*must exist* in the data frame or CSV file. All}_name\verb{s must be valid PostgreSQL syntax for said names. Setting }drop=TRUE` is \emph{destructive}. |
|||
} |
|||
\examples{ |
|||
\dontrun{ |
|||
DBI::dbConnect( |
|||
odbc::odbc(), |
|||
driver = "/usr/local/lib/psqlodbca.so", |
|||
Database = "working", |
|||
Host = "localhost" |
|||
) -> con |
|||
|
|||
create_cidr_lookup_table( |
|||
pgcon = con, |
|||
tbl_name = "amazon_cidrs", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "amzn-cidrs.csv", package = "pgcidr"), |
|||
) |
|||
|
|||
create_ip_source_table( |
|||
pgcon = con, |
|||
tbl_name = "weblog", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "weblog.csv", package = "pgcidr") |
|||
) |
|||
|
|||
find_ips_in_cidrs( |
|||
con, "weblog", "amazon_cidrs" |
|||
) |
|||
|
|||
} |
|||
} |
@ -0,0 +1,52 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/find-ips-in-cidrs.R |
|||
\name{find_ips_in_cidrs} |
|||
\alias{find_ips_in_cidrs} |
|||
\title{Finds IPs in CIDR blocks} |
|||
\usage{ |
|||
find_ips_in_cidrs(pgcon, ip_tbl, cidr_tbl, ip_col = "ip", cidr_col = "cidr") |
|||
} |
|||
\arguments{ |
|||
\item{pgcon}{a PostgreSQL DBI connection} |
|||
|
|||
\item{ip_tbl}{name of the IP table (valid PG table name syntax)} |
|||
|
|||
\item{cidr_tbl}{name of the CIDR table (valid PG table name syntax)} |
|||
|
|||
\item{ip_col}{Name of the column that holds the IP addresses in \code{ip_tbl}} |
|||
|
|||
\item{cidr_col}{Name of the column that holds the CIDR block in \code{cidr_tbl}} |
|||
} |
|||
\description{ |
|||
After you use \code{\link[=create_cidr_lookup_table]{create_cidr_lookup_table()}} and \code{\link[=create_ip_source_table]{create_ip_source_table()}} |
|||
use this function to do the lookup. |
|||
} |
|||
\examples{ |
|||
\dontrun{ |
|||
DBI::dbConnect( |
|||
odbc::odbc(), |
|||
driver = "/usr/local/lib/psqlodbca.so", |
|||
Database = "working", |
|||
Host = "localhost" |
|||
) -> con |
|||
|
|||
create_cidr_lookup_table( |
|||
pgcon = con, |
|||
tbl_name = "amazon_cidrs", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "amzn-cidrs.csv", package = "pgcidr"), |
|||
) |
|||
|
|||
create_ip_source_table( |
|||
pgcon = con, |
|||
tbl_name = "weblog", |
|||
drop = TRUE, |
|||
xdf = system.file("extdat", "weblog.csv", package = "pgcidr") |
|||
) |
|||
|
|||
find_ips_in_cidrs( |
|||
con, "weblog", "amazon_cidrs" |
|||
) |
|||
|
|||
} |
|||
} |
@ -0,0 +1,48 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/macos-setup.R |
|||
\name{macos_postgresql_setup_with_ip4r} |
|||
\alias{macos_postgresql_setup_with_ip4r} |
|||
\title{Setting up macOS PostgreSQL 12 with ip4r extension} |
|||
\description{ |
|||
Setting up macOS PostgreSQL 12 with ip4r extension |
|||
} |
|||
\section{Setup}{ |
|||
\preformatted{$ brew uninstall postgresql |
|||
$ rm -rf /usr/local/var/postgres |
|||
|
|||
$ brew tap petere/postgresql |
|||
$ brew install petere/postgresql/postgresql@12 |
|||
$ brew install petere/postgresql/postgresql-common |
|||
$ initdb DATA_PATH -E utf8 |
|||
$ pg_ctl -D /Volumes/otg/postgres -l logfile start |
|||
|
|||
$ ps -ef | grep postgr |
|||
|
|||
$ pg_config |
|||
|
|||
$ createdb $\{USER\} |
|||
|
|||
$ psql |
|||
username=# \\q |
|||
|
|||
$ cd /place/where/you/clone/stuff |
|||
$ git clone git@github.com:RhodiumToad/ip4r.git |
|||
$ cd ip4r |
|||
$ make |
|||
$ sudo make install |
|||
|
|||
$ cd /place/where/you/clone/stuff |
|||
$ wget https://ftp.postgresql.org/pub/odbc/versions/src/psqlodbc-12.01.0000.tar.gz |
|||
$ tar -xvzf psqlodbc-12.01.0000.tar.gz |
|||
$ cd psqlodbc-12.01.0000 |
|||
$ ./configure |
|||
$ make -j 8 |
|||
$ sudo make install |
|||
$ ls -l /usr/local/lib/psqlodbca.so |
|||
|
|||
$ createdb working |
|||
$ psql --command="CREATE EXTENSION ip4r;" working |
|||
$ psql --command="CREATE EXTENSION ip4r;" $\{USER\} |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue