Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PID Field to the netstat Function Output #13

Open
brichard1638 opened this issue Jun 14, 2024 · 0 comments
Open

Add PID Field to the netstat Function Output #13

brichard1638 opened this issue Jun 14, 2024 · 0 comments

Comments

@brichard1638
Copy link

brichard1638 commented Jun 14, 2024

Would it be possible to add the PID column to the netstat function's output? This would improve the value proposition of this function. A proposed updated version of the netstat function is provided below:

function ()
{
os <- Sys.info()["sysname"]
switch(os, Windows = {
system("netstat -a -n -o", intern = TRUE)
}, {
system("netstat -a -n -o", intern = TRUE)
})
}

If this change proposal is accepted, the parse_netstat function will also require modification.

Upon further analysis and a desire to work with a parsed version of the code provided above, I rewrote the parse_netstat function. The tested result, which I refer to as conv_ns, is provided below:

conv_ns <- function(netstat_output) {
# Formalize vector structure
netstat_output <- netstat_output[4:length(netstat_output)]

# Rename line columns
netstat_output[1] <- " Proto Local_Address Foreign_Address State PID"

# Split the lines into columns
data <- strsplit(netstat_output, "\\s+")

# Create a matrix
m <- stringi::stri_list2matrix(x = data, byrow = TRUE, fill = 0)

# Convert to dataframe
df <- data.frame(m)

# Remove empty column
df$X1 <- NULL

# Set column names
colnames(df) <- c("Proto", "Local_Address", "Foreign_Address", "State", "PID")

# Reset dataframe
df <- df[-c(1),]

return(df)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant