0

I want to do a whois lookup on a list of IPs and then grep country.

I have:

const { spawn } = require('child_process')

const ip_list = [
  '192.168.1.1',
  '192.168.1.2',
  '192.168.1.3'
]

const process = spawn('whois', ip_list)
process.stdout.on('data', (data) => {
  console.log(data)
})

Which is equivalent of running whois ip1 ip2 ip3 ...

But I want the equivalent of running whois ip1 ip2 ip3 ... | grep country

How to do that in Node?

I tried adding " | grep country" or just "grep country" to the end of ip_list as args but that gave me a query error.

Nermin
  • 749
  • 7
  • 17
  • Does this answer your question? [Using a pipe character | with child\_process spawn](https://stackoverflow.com/questions/28968662/using-a-pipe-character-with-child-process-spawn) – Hamza Anis Feb 21 '21 at 15:56
  • No, if I add "grep country" or "| grep country" to the end of the ip_list, I get an error "Invalid query ..." – Nermin Feb 21 '21 at 16:22

1 Answers1

0

Okay, so I had to turn the ip_list into a string of all the IPs separated with space and then use the method mentioned in Using a pipe character | with child_process spawn

const str1 = ip_list.join(' ')
const process = spawn('sh', ['-c', 'whois ' + str1 + ' | grep country'])
Nermin
  • 749
  • 7
  • 17