Issue
I have export txt file in linux as
2021-11-10T02:54:18+00:00 172.31.0.110 51.146.201.110 running [email protected] m5.2xlarge vpc-95525af1
2021-11-23T17:40:04+00:00 10.0.0.254 192.168.10.20 running [email protected] t2.micro vpc-068cf656faf214df2
I looking for bash script that read from the file, if the date is less then 30 days from the current date, then send email as row 5 . email subset: You vm is .... body You vm is over 5... please power off ....
Thanks
Solution
#!/usr/bin/bash
IFS=$'\r\n'
GLOBIGNORE='*'
text=($(cat "export.txt")) # NAME OF THE FILE
for i in "${text[@]}"; do
obj_ip=$(echo $i | cut -d $'\t' -f 2)
obj_another_ip=$(echo $i | cut -d $'\t' -f 3)
obj_email=$(echo $i | cut -d $'\t' -f 5)
obj_datetime=$(echo $i | cut -d $'\t' -f 1)
obj_name=$(echo $i | cut -d $'\t' -f 6)
timeago='30 days ago' #EDIT TO MATCH YOUR IDEAS
dtSec=$(date --date "$obj_datetime" +'%s')
taSec=$(date --date "$timeago" +'%s')
if [ $dtSec -lt $taSec ]; then
to_send_text="Your VM $obj_name with IP $obj_ip is Too Old, please power off"
mail -s $to_send_text [email protected]
echo $to_send_text
fi
done
Answered By - indicozy