I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from 'trusted-ip-list.txt' file and check whether the read value exists in 'ip-list.txt' file and write some log info to some other file named 'checked-trust.log' file.
Here is the code i'm trying.. But I'm not able to do so :wall:..
trust_file=trusted-ip-list.txt
ip_file=ip-list.txt
while ip= read -r line
do
test=`grep -w "$line" $ip_file`
echo " "
if [ "$ip" -e "$test" ] ; then
echo "Trusted access at `date` by $line" > checked-trust.log
else
echo "Untrusted access at `date` by $line" > check-trust.log
fi
done < "$trust_file"
Please help me in solving this.. Waiting for a helping hand..
although I suspect you may want to leave that out, since you are doing a word match with it and like this it will contain any spaces in the input file..
I don't know what the
echo " "
is for.. (it's not written to the log)
$ip_file could use a pair of double quotes although with the filename contained therein, this is not strictly necessary..
If you still can't get it to work, please post sample input files...
I've done a small change in the code. Please find it:
trust_file=trusted-ip-list.txt
ip_file=ip-list.txt
while read ip
do
test=`grep -w "$ip" $ip_file`
echo "The value of test is : $test"
if [ "$ip" = "$test" ] ; then
echo "Trusted access at `date` by $line" > checked-trust.log
else
echo "Untrusted access at `date` by $line" > check-trust.log
fi
done < $trust_file
Even though it is not working.. When I run the script, I'm getting the following output:
I'm not able to grep the $ip value from ip-list.txt file and set to "test" variable..
#!/bin/bash
ip_file=ip-list.txt
trust_file=trusted-${ip_file}
while read ip; do
if grep -w "${ip}" ${ip_file}; then
value=Trusted
else
value=Untrusted
fi
echo "${value} access at $(date) by ${ip}" >> checked-trust.log
done < ${trust_file}