【问题标题】:How to use python to run a list from one file through another script如何使用python通过另一个脚本从一个文件运行列表
【发布时间】:2014-11-19 18:15:57
【问题描述】:

好的,因此我的任务是审核我办公室中 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 相同的方式输出它。

【问题讨论】:

  • 听起来你需要修改 Perl 脚本。
  • 脚本接受一个 IP 的输入并告诉我它是否正在使用中...我要做的是运行另一个脚本,该脚本将通过 perl 脚本运行 IP 列表并输出它们逐个。我不能真正修改 perl 脚本以从文件中获取,因为通常它的唯一用途是来自用户的个人提示。

标签: python linux bash perl


【解决方案1】:

@squiguy 说的是正确的。您可能需要修改 PERL 脚本。

grep "This IP is free" checkIP.pl
grep "This IP is used by" checkIP.pl

通过运行几个 grep 来查找文件中脚本中的行所在的行

然后假设脚本使用'Getopt::Std qw(getopts);' -s' 参数应该附加到的变量是“$opt_s”

将行改为

"$opt_s: This IP is free"
"$opt_s: This IP is used by"

【讨论】:

    【解决方案2】:

    您始终可以编写一个由 xargs 调用的小型包装脚本,该脚本依次显示 IP 地址,然后调用 checkIP.pl

    checkIP.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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-20
      • 2016-09-03
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多