3

I am new to scripting and R.

In the Windows cmd.exe I can perform an NSLOOKUP on a domain by:

nslookup www.google.com

I have a dataset of domain names that I would like to verify as valid or invalid as part of my grouping process in R. Is there a way to do NSLOOKUP in base R or one of the packages?

Edit1: I made some alterations to loop through 3 domains using the system call suggested. The call works, however the output will not save directly into a vector (line 7 below). How would I need to rework this line so that I can capture the output?

domains <- c('www.google.com','www.badtestdomainnotvalid.com','www.yahoo.com')
dns <- vector()
dnsreturn <-vector()
for(i in 1:length(domains)){
  dns[i] <- paste('nslookup ',domains[i],sep='')
  dnsreturn[i] <- system(dns[i],intern=TRUE)}
}
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187

1 Answers1

5

If nothing else you could do

system("nslookup www.google.com", intern=TRUE)

In response to your edit:

domains = c('www.google.com','www.badtestdomainnotvalid.com','www.yahoo.com')
sapply(domains, function(x) system(paste("nslookup", x), intern=TRUE))

This will return a list of vectors, you can manipulate as you see fit beyond that

geoffjentry
  • 4,674
  • 3
  • 31
  • 37
  • THat worked for a single line, I'm editing my question to include altering your solution to save the output from making the system call back into a vector or frame. – Connie Marie Aug 05 '13 at 15:48
  • @ConnieMarie Note that Geoff didn't initialize his objects like you're doing in the question. [It's a good idea to preallocate a vector if it's going to get really big](http://stackoverflow.com/questions/12464379/preallocate-list-in-r), but you need to specify the length or it doesn't do you any good. It's an even better idea to use the `apply` family of functions like Geoff does above. – Matt Parker Aug 05 '13 at 19:31