重击-成功或失败-先发送电子邮件,然后退出

时间:2019-06-21 22:31:09

标签: bash email

我想在目标服务器上运行bash脚本,只要任何命令执行失败,我想先发送带有错误详细信息以及失败原因的电子邮件,然后退出程序

我尝试将整个bash脚本执行重定向到日志文件,并通过电子邮件发送日志文件。

现在,仅当脚本成功执行时,我才会收到包含file.log内容的电子邮件,但即使它也失败,我也希望收到包含详细信息的电子邮件。请帮忙。

import can
import csv
import datetime
import pandas as pd

#filename = open('C:\\Users\\shraddhasrivastav\\Downloads\\BLF File\\output.csv', "w")
log = can.BLFReader('C:\\Users\\shraddhasrivastav\\Downloads\\BLF File\\test.blf')

# print ("We are here!")
log_output = []

for msg in log:
    msg = str(msg).split()
    #print (msg)

    data_list = msg[7:(7 + int(msg[6]))]

    log_output_entry = [(msg[1]), msg[3], msg[6], " ".join(data_list), msg[-1]]
    log_output_entry.insert(1, 'ID=')
    assert len(log_output_entry)==4
    log_output.append(log_output_entry)
    #test_entry = " \t ".join(log_output_entry)  # join the list and remove string quotes in the csv file

    #filename.write(test_entry + '\n')

df = pd.DataFrame(log_output)
df.columns = ['Timestamp', 'ID', 'DLC','Channel']
df['Timestamp'] = pd.to_datetime(df['Timestamp'],unit='s')
df['Time Delta'] = df['Timestamp'] - df['Timestamp'].shift(periods=1, axis=0)
df.to_csv('C:\\Users\\shraddhasrivastav\\Downloads\\BLF File\\output_df.csv')

#filename.close()  # Close the file outside the loop

1 个答案:

答案 0 :(得分:0)

尝试:

我不知道您正在使用什么命令,所以我使用了PING:

 #!/bin/bash

 status() {
 for hosts in 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.250; do
    ping -t 1 -c 1 "$hosts";
 if [[ "$?" -gt "0" ]]; then
    echo -e "Host is down: '$hosts'" >> error.log
 fi
 done
 }

 failedornot() {
 while read line; do
    grep -q "down" error.log
 if [[ $? -eq "0" ]]; then
    echo -e "Something went wrong, sending e-mail report to: abcd@abcd.com"
    cat error.log | mailx -s "Status" abcd@abcd.com
    rm error.log
 else
    echo "Everything is fine, executing blabla.sh"
 fi

     done < error.log
 }

 status
 failedornot

输出

Host is down: 192.168.1.250
Something went wrong, sending e-mail report to: <username>..