【问题标题】:Execute two shell commands and pass arguments for the first one执行两个 shell 命令并为第一个传递参数
【发布时间】:2021-05-24 00:16:04
【问题描述】:

我想对 IP 列表进行 whois 查找,然后查找 grep country

我有:

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)
})

相当于运行whois ip1 ip2 ip3 ...

但我想要相当于运行whois ip1 ip2 ip3 ... | grep country

如何在 Node 中做到这一点?

我尝试在 ip_list 末尾添加“| grep country”或只是“grep country”作为参数,但这给了我一个查询错误。

【问题讨论】:

标签: node.js


【解决方案1】:

好的,所以我只好把ip_list变成一个所有IP用空格分隔的字符串,然后使用Using a pipe character | with child_process spawn中提到的方法

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

【讨论】:

    猜你喜欢
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2015-04-11
    相关资源
    最近更新 更多