【问题标题】:bash to automate input file daily + ping testbash 每天自动化输入文件 + ping 测试
【发布时间】:2018-04-02 14:36:39
【问题描述】:

我每天对 100 个 IP 进行 ping 测试,输入文件 IP 每天都会更新,因为它是动态的。我指定 input(100 IP list) 作为第一个参数。因为下面的脚本将每 10 分钟运行一次

注意:- 我每天都使用不同的IP,所以需要每天凌晨12点更改输入文件

在 4 月 1 日凌晨 12 点作为后台运行脚本

sh ping.sh IPlist_apr1.txt & 

第二天,我的辅助脚本(将创建更新的 IP)将以这种文件格式“IPlist_apr2.txt”选择其他 IP 集 第三天 - IPlist_apr3.txt ...它会每天在 CWD 上记录 $1.log。

同样,这个过程继续,我实际上正在寻找我的 ping 脚本应该每天处理列表作为第一个参数。

我的实际脚本的片段。

t=10m                         
ip=$1                     #### specify the file having IP's
while sleep $t
do
    if ping -c1 $ip >/dev/null 2>&1; then
        echo "`date +%H:%M`: $ip is up";
    else
        echo "`date +%H:%M`: $ip is down"; 
    fi >>$1.log;
done

【问题讨论】:

  • 如果您打算使用bash,为什么要使用sh 调用脚本?
  • 有什么问题?

标签: bash


【解决方案1】:

您需要从参数命名的文件中读取 IP 地址。

  while IFS= read -r ipaddr; do
    if ping -c1 "$ipaddr"; then
      status=up
    else
      status=down
    fi
    printf '%s: %s is %s\n' "$(date +%H:%M)" "$ipaddr" "$status"
  done < "$1"

请注意,这会运行一批 ping,然后退出。调用者应负责休眠 10 分钟并为输入指定正确的文件名。

【讨论】:

  • 这不是我在看这里,输入将作为第一个参数,脚本应该自动选择由其他脚本创建的新文件,并将该文件作为输入作为第二天的第一个参数处理。
  • 简而言之,我正在手动启动脚本 #./ping.sh IPlist_apr1.txt & { first day } #./ping.sh IPlist_apr2.txt & {second day } likewsie 这个过程还在继续....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多