-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhois-demo.Rmd
86 lines (66 loc) · 2.46 KB
/
whois-demo.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
title: "Whois Demo"
output:
html_document:
keep_md: true
---
We will first install a [whois](https://en.wikipedia.org/wiki/WHOIS) utility
if we are running Windows, as this operating system does not come with this
tool by default. Other popular operating systems like OSX and Linux come with
`whois` pre-installed, so we will not perform this step on those systems.
```{r}
if (Sys.info()["sysname"] == "Windows") {
# Get the whois.exe file for Windows from SysInternals.
exefile <- ".\\whois.exe" # This syntax will work in DOS and Bash.
# Only download and extract the utility if we don't already have it.
if (! file.exists(exefile) == TRUE) {
zipfile <- "WhoIs.zip"
if (! file.exists(zipfile) == TRUE) {
url <- "http://download.sysinternals.com/files/WhoIs.zip"
download.file(url, zipfile)
# Run this utility once (first time) to agree to the EULA.
system(paste(exefile, "--help"), intern = FALSE)
}
unzip(zipfile)
}
} else {
exefile <- "whois"
}
```
Run the `whois` command with the domain `who.int` as a test.
```{r}
# Try out the whois utility, removing any carriage-return (\r) characters.
whois.data <- gsub(pattern = "\\r", replacement = "",
x = system(paste(exefile, "who.int"), intern = TRUE))
# Take a quick look at the results.
length(whois.data)
head(whois.data, 10)
```
Read the data into a `data.frame`.
```{r}
# clean up the the whois data and read into a data.frame.
# Use the vertical bar symbol (|) as the delimiter instead of the colon (:).
whois.data <- gsub(pattern = ":[[:space:]]", replacement = "|", x = whois.data)
# Remove lines which do not contain the delimiter.
whois.data <- whois.data[grepl(x = whois.data, pattern = "\\|")]
# Take a quick look at the results.
length(whois.data)
head(whois.data, 10)
# Take a look at the last few lines.
tail(whois.data, 10)
# Remove any '>>>' and '<<<' strings, if present.
whois.data <- gsub("[[:space:]]?[<>]{3}[[:space:]]?", "", whois.data)
# Check the last line again.
whois.data[length(whois.data)]
# Read the file into a data.frame (df).
whois.df <- read.table(text = whois.data, sep = "|", as.is = TRUE)
names(whois.df) <- c("variable", "value")
```
Load the `knitr` package so that we can use the `kable()` function.
```{r, message=FALSE}
library(knitr)
```
Produce a formatted table from the `data.frame` using `kable()`.
```{r, results='asis'}
kable(whois.df)
```