[root@localhost checksalt]# cat checkSalt.sh
#!/bin/sh
rm -f *.txt
salt-key -y -A
salt-key -L |egrep -v 'Keys|192.168' > saltid.txt
salt '*' test.ping > saltTrue.txt
> saltid_ok.txt
> saltid_fail.txt
function checksalt(){
for n in `cat $1`
do
#echo "grep $n saltTrue.txt "
if grep $n saltTrue.txt
then
echo $n >> saltid_ok.txt
else
for m in `seq 3`
do
num=`salt $n cmd.run "ifconfig"|wc -l `
if [ $num -eq 0 ]
then
sleep 5
if [ $m -eq 3 ]
then
salt-key -y -d $n
echo $n >> saltid_fail.txt
fi
else
continue
fi
done
fi
done
}
checksalt saltid.txt
salt-key -L |egrep -v 'Keys|192.168' > saltid_now.txt
sh diff_salt_ipcsv.sh
[root@localhost
#######################################################################
#######################################################################
[root@localhost checksalt]# cat diff_salt_ipcsv.sh
#!/bin/sh
>have_salt_id.txt
>noneed.ip.txt
>unconnect_salt_id.txt
awk -F'[" "]' '{print $4 " " $14 " "$16}' ip.csv >hangld.ip.csv.txt
for n in `awk '{print $2}' hangld.ip.csv.txt`
do
if grep $n saltid_now.txt
then
grep $n hangld.ip.csv.txt >>have_salt_id.txt
else
for m in `cat NoNeed.ip`
do
#echo "------------>> if $m -eq $n "
if [ "$m" == "$n" ]
then
grep $n hangld.ip.csv.txt >>noneed.ip.txt
break
fi
done
grep $n hangld.ip.csv.txt >>unconnect_salt_id.txt
fi
done
diff unconnect_salt_id.txt noneed.ip.txt |awk 'NR>1{print $2 " " $3}' > PROBLEM.txt
if [ -s PROBLEM.txt ]
then
time=`date`
python python_email.py "salt_ __$time" PROBLEM.txt
fi[root@localhost
#######################################################################
#######################################################################
[root@localhost checksalt]# cat python_email.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
def smtp(title,file):
import smtplib
from email.mime.text import MIMEText
from email.header import Header
with open(file, 'r') as f:
content = f.read()
#
msg=MIMEText(content,'plain', 'utf-8')
to_list=['[email protected]',[email protected]]
msg['from'] = '[email protected]'
msg['subject'] = title
#
try:
server = smtplib.SMTP()
server.connect('smtp.exmail.qq.com')
server.login('[email protected]','xxxxx')#XXX ,xxxxx
server.sendmail(msg['from'],to_list,msg.as_string())
server.quit()
print ' '
except Exception, e:
print str(e)
if __name__ == '__main__':
smtp(sys.argv[1],sys.argv[2])
[root@localhost checksalt]#
#######################################################################
#######################################################################
[root@localhost checksalt]# crontab -l
*/10 * * * * /bin/sh /script/shell/gaogd/checksalt/checkSalt.sh >/dev/null 2>&1
[root@localhost checksalt]#