Unix 版。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib
import urllib2
import time
import os
import sys
#import subprocess
before = len(open("/tmp/twsms.log").readlines())
# 在呼叫網頁前將 ping 的資料寫入 Log
os.system('/bin/date >> /tmp/pingtwsms.log')
ps = os.system('/bin/ping -c 3 -i 0.2 api.twsms.com >> /tmp/pingtwsms.log')
#subprocess.call("ping -c 3 -i 0.2 api.twsms.com",shell=True,stdout=open('/tmp/pingtwsms.log','w'),stderr=subprocess.STDOUT)
'''
pinglog = os.popen("/bin/ping -c 3 -i 0.2 api.twsms.com").readlines()
file = open('/tmp/pingtwsms.log','a')
for i in pinglog:
file.write(i)
file.close()
'''
if ps == 0:
os.system('echo "Ping Success." >> /tmp/pingtwsms.log')
else:
os.system('echo "Ping Fail." >> /tmp/pingtwsms.log')
# 系統變數
site = sys.argv[1]
IP = sys.argv[2]
username = 'flyfox'
password = '123'
mobile = ["0910234567", "0910345678", "0910456789"]
message = '簡訊內容測試'+ site + IP
message = urllib.quote(message)
file = open('/tmp/twsms.log', 'a')
for i in mobile:
msg = 'username='+username+'&password='+password+'&mobile='+i+'&message='+message
url = 'http://api.twsms.com/smsSend.php?'+msg
resp = urllib2.urlopen(url)
file.write(time.asctime()+" "+resp.read()+"\n")
file.close()
# 判斷是否成功發送簡訊
fo = open("/tmp/twsms.log","r")
mylist = fo.readlines()
after = mylist[before+len(mobile)-1][41:46]
fo.close()
if after == '00000':
os.system('echo "Send Success." >> /tmp/pingtwsms.log')
else:
f = open('/tmp/pingtwsms.log','a')
sys.stderr = f
os.system('echo "Send Fail." >> /tmp/pingtwsms.log')
os.system('/bin/mv /usr/local/nagios/bin/sms.sent /usr/local/nagios/bin/sms')
f.close()
留言列表