【问题标题】:insert hosts in check via for loop通过 for 循环插入主机检查
【发布时间】:2020-07-11 22:42:58
【问题描述】:

以下问题:

我们有一个名为“file.conf”的文件

192.168.30.1|192.168.30.1|os _gateway|192.168.30.2|Linux 2.6.18 - 2.6.22 ...

第一个是主机名第二个是ipv4

现在我们有了一个脚本,他应该通过 checkMK 的自动用户自动插入主机和 IP

#!/bin/bash

FILE=filename
source $FILE

for i in ${FILE}
do

HOSTNAME=$(cat $i | cut -d '|' -f1)
IP=$(cat $i | cut -d '|' -f2)

curl "http://checkmkadress/check_mk/host&user" -d 'request={"hostname":"'"$HOSTNAME"'","folder":"ansible","attributes":{"ipaddress":"'"$IP"'","site":"sitename","tag_agent":"cmk-agent"}}'


done


但是如果我们这样做,我们会得到以下错误,因为他试图将每个主机放入主机中,并将每个 ip 放入 ip 中而不经过所有行

{"result": "Check_MK exception: Failed to parse JSON request: '{\"hostname\":\"allhostnames":{\"ipaddress\":all_ips\",\"site\":\"sitename\",\"tag_agent\":\"cmk-agent\"}}': Invalid control character at: line 1 column 26 (char 25)", "result_code": 1}

如何让 curl 脚本遍历每一行以分别获取主机和 ip

【问题讨论】:

    标签: bash curl check-mk


    【解决方案1】:

    使用你已经做过的,以这种方式尝试。

    FILE="filename"
    source $FILE
    
    while read line
    do
    
        HOSTNAME=$(echo $line | cut -d '|' -f1)
        IP=$(echo $line | cut -d '|' -f2)
    
        #your curl command here
    
    done <$FILE 
    

    或者,我更喜欢这个

    while IFS='|' read -r host ip description
    do 
       #your curl command here
       echo "$host : $ip : $description"
    done <$FILE
    

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多