好吧,我正在承担一项艰巨的任务,即审核办公室 IP 的使用情况,因为我们的 IP 资源不足。因此,我尝试编写一个基本脚本,其中包含我需要验证的 IP 列表,并根据另一个验证 IP 使用情况的脚本对它们进行调整。我可以在 Bash 中使用它,但我想使用 Python,这样我就可以按照我喜欢的方式进行一些调整。
所以我有一个名为“iprange”的文件,还有一个前同事制作的 perl 文件,但它不再在这里工作,它需要一个 IP 并检查它,验证什么在使用它,然后将其输出回来。以下是 Bash 中对我“有效”的示例。
cat iprange | xargs -I % checkIP.pl -s % > ipresults.txt 2>&1
问题是,这给了我一个看起来像这样的输出。
"This IP is free"
"This IP is free"
"This IP is used by XPNSE43525"
有两个问题。 1)有数千个IP,所以我需要找到一种方法来拥有它,这样它看起来更像这样。
10.4.8.5
"This IP is free"
或者
checkIP.pl -s 10.4.8.5
"This IP is free"
由于这在 bash 中似乎不可能,我希望用 Python 来解决这个问题。但不知道如何让 python 启动脚本并像使用 bash 一样输出它。
最佳答案
您始终可以编写一个由 xargs 调用的小包装脚本,该脚本依次显示 IP 地址,然后调用 checkIP.pl
。
检查IP.sh
#!/usr/bin/env sh
echo -n "$1 : "
checkIP.pl -s $*
然后你可以这样调用它:
cat iprange | xargs -I % checkIP.sh % > ipresults.txt 2>&1
并期望看到如下输出:
10.4.8.5 : This IP is free
10.1.1.1 : This IP is free
192.168.1.1 : This IP is used by XPNSE43525
8.8.8.8 : This IP is used by Google DNS
关于python - 如何使用 python 通过另一个脚本运行一个文件中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030696/