0

following problem:

we have a file called "file.conf"

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

first is the hostname second is the ipv4

now we have a script where he should automaticaly insert the hosts and ip's via automated user from checkMK

#!/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


but if we do it like that we get the following error cause he try's to put in every host in host and every ip in ip without going through all lines

{"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}

how can we make the curl script go through each line to get host and ip individually

4

1 回答 1

0

使用你已经完成的,以这种方式尝试。

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
于 2020-03-31T12:24:16.583 回答