仅在包含 20 行后通过电子邮件发送文件

仅在包含 20 行后通过电子邮件发送文件

我最近开始为一家非常小的公司工作,我正在尝试通过自动化 Mikrotik 路由器的库存处理流程来节省他们的时间,他们将这些路由器放置在每个客户家里用作“Dmarc”或家庭 WiFi 路由器。路由器一次订购 5 个盒子,每盒 20 个。最初,它们的开箱配置不理想,固件也过时。在我到达那里之前,该公司每天花费数小时登录每个路由器,删除开箱即用的配置,并将其替换为适用于公司的配置,更新固件,然后将设备添加到他们的架子上存货。我编写了以下脚本来登录一批路由器(我目前可以一次执行 6 个)并执行这些操作。我需要一些帮助,添加一个 if 循环,以等待“库存表”包含 20 行,然后再通过电子邮件将其添加到库存中。该脚本当前每 16 分钟运行一次...代码:

#!/bin/bash
while true
do
echo $(date "+%F %T") : starting script >> Script_timer.log
#Scans for new Mikrotiks to configure on the office LAN
mactelnet -lB > targetsfull.inv &

# Gets PID of scanning activity
PID=$!

#wait 5 seconds
sleep 5

#end scan
kill $PID

#Grab MAC addresses from scan info into file called targetsMAC.inv
grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' targetsfull.inv > targetsMAC.inv

#Run Expect Script on every MAC address in targetsMAC.inv and adds them to CompleteRouters.inv ( Records Serial Number and MAC address of eth1 for each router and replaces factory config with company default config

/home/michael/hAPRtRMAC.sh



#Formats list of serial numbers and MAC addresses into 2 columns "serialnumber,MACaddress"
xargs -n2 < CompleteRouters.inv >> InventoryConfigsComplete.inv

#places a comma between the serial number and MAC address 
sed -i "s/ /,/g" InventoryConfigsComplete.inv

#removes Duplicate Lines and saves to new file
awk '!seen[$0]++' InventoryConfigsComplete.inv > InventoryConfigsEmail.inv

Kamaraj 的答案 插入即可运行良好

FILE_NAME=InventoryConfigsEmail.inv

NUM_OF_LINES=$(wc -l < ${FILE_NAME})

if [ "${NUM_OF_LINES}" -ge "20" ]
then
    echo "Triggering Inventory Complete Email"
    mail -s "Inventory Configs Complete" [email protected] < "${FILE_NAME}"
    mv *.inv safezone/
    touch CompleteRouters.inv
    touch InventoryConfigsComplete.inv
    touch InventoryConfigsEmail.inv

else
    echo "${NUM_OF_LINES} Routers Complete" >> ChangeTheBatch.inv 
fi
echo $(date "+%F %T") :script ended >> Script_timer.log
sleep 960
done

答案1

该脚本检查 /tmp/my.log 的行号,如果它大于或等于 20,那么它将发送电子邮件

#!/bin/bash

FILE_NAME=/tmp/my.log

NUM_OF_LINES=$(wc -l < ${FILE_NAME})

if [ "${NUM_OF_LINES}" -ge "20" ]
then
    echo "Triggering Email"
    mail -s "Log" [email protected] < "${FILE_NAME}"
else
    echo "Log file contains ${NUM_OF_LINES} lines"
fi

相关内容