diff --git a/README.md b/README.md index fe544ee..20c64b4 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,12 @@ Python Script to Recive and Decode German BOS Information with rtl_fm and multimon-NG -### Note: +#### Note: **This software is for illustrative purposes only and may be used only by authorized persons.** **The intercept of the German BOS radio is strictly prohibited !!!** + **Please** only use Code from **master**-Branch - thats **the only stable!** unless you are developer you can use the develop-Branch - may be unstable! @@ -32,7 +33,8 @@ unless you are developer you can use the develop-Branch - may be unstable! ##### boswatch.py The configuration for the Script you can find in config.ini - You can set the ignore time for double alarms in seconds. -- To use the script with MySQL Support set "useMySQL = 1" and the Userdata to your local MySQL Database. +- to use the script with MySQL Support set "useMySQL = 1" and the Userdata to your local MySQL Database. +- to use the script with HTTP request Support set "useHTTPrequest = 1" and set a URL to your destination. For the other Functions see "Usage" below. @@ -65,6 +67,7 @@ optional arguments: -s SQUELCH, --squelch SQUELCH Level of Squelch -v, --verbose Shows more Information + -q, --quiet Shows no Information. Only Logfiles ``` ### Installation @@ -74,7 +77,7 @@ You can easy install BOSWatch with the install.sh Script. - And use the script `sudo sh install.sh` Now the script downloads and compile all needed data. -At the end you can find the Programm in `/home/pi/bos/BOSWatch` +At the end you can find the Programm in `~/bos/BOSWatch` Caution, script don't install a Webserver with PHP and MySQL. So you have to make up manually if you want to use MySQL support. diff --git a/boswatch.py b/boswatch.py index d16b3a8..04242cf 100644 --- a/boswatch.py +++ b/boswatch.py @@ -11,15 +11,14 @@ import time #import sys import subprocess -#import os +import os #for absolute path: os.path.dirname(os.path.abspath(__file__)) import mysql import mysql.connector -import httplib +import httplib #for the HTTP request import argparse #for parse the args import ConfigParser #for parse the config file -import re #Regex - +import re #Regex for validation # Functions def curtime(format="%Y-%m-%d %H:%M:%S"): @@ -35,17 +34,19 @@ def log(msg, level="log"): if not level == "log" and not args.quiet or args.verbose: print log_entry - bos_log = open("log_bos.txt", "a") + bos_log = open(script_path+"/log_bos.txt", "a") bos_log.write(log_entry+"\n") bos_log.close() try: + script_path = os.path.dirname(os.path.abspath(__file__)) + try: - bos_log = open("log_bos.txt", "w") - rtl_log = open("log_rtl.txt", "w") - mon_log = open("log_mon.txt", "w") + bos_log = open(script_path+"/log_bos.txt", "w") + rtl_log = open(script_path+"/log_rtl.txt", "w") + mon_log = open(script_path+"/log_mon.txt", "w") bos_log.write("##### "+curtime()+" #####\n\n") rtl_log.write("##### "+curtime()+" #####\n\n") mon_log.write("##### "+curtime()+" #####\n\n") @@ -119,11 +120,22 @@ try: print "Verbose Mode!" print "" + #variables pre-load + log("pre-load variables") + fms_id = 0 + fms_id_old = 0 + fms_time_old = 0 + + zvei_id = 0 + zvei_id_old = 0 + zvei_time_old = 0 + + #ConfigParser log("reading config file") try: config = ConfigParser.ConfigParser() - config.read("./config.ini") + config.read(script_path+"/config.ini") fms_double_ignore_time = int(config.get("FMS", "double_ignore_time")) zvei_double_ignore_time = int(config.get("ZVEI", "double_ignore_time")) @@ -146,24 +158,25 @@ try: url = config.get("HTTPrequest", "url") except: - log("config reading error","error") - - #variables pre-load - log("pre-load variables") - fms_id = 0 - fms_id_old = 0 - fms_time_old = 0 + log("cannot read config file","error") - zvei_id = 0 - zvei_id_old = 0 - zvei_time_old = 0 + log("set to standard configuration") + fms_double_ignore_time = 5 + zvei_double_ignore_time = 5 + useMySQL = 0 + useHTTPrequest = 0 + if useMySQL: #only if MySQL is active - log("connect to MySQL database") + log("testing MySQL connection") try: connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) + log("connection test successful") except: - log("MySQL connect error","error") + log("connection test failed - MySQL support deactivated","error") + useMySQL = 0 + finally: + connection.close() #Close connection in every case log("starting rtl_fm") @@ -171,7 +184,7 @@ try: rtl_fm = subprocess.Popen("rtl_fm -d "+str(device)+" -f "+str(freq)+" -M fm -s 22050 -p "+str(error)+" -E DC -F 0 -l "+str(squelch)+" -g 100", #stdin=rtl_fm.stdout, stdout=subprocess.PIPE, - stderr=open('log_rtl.txt','a'), + stderr=open(script_path+"/log_rtl.txt","a"), shell=True) except: log("cannot start rtl_fm","error") @@ -182,7 +195,7 @@ try: multimon_ng = subprocess.Popen("multimon-ng "+str(demodulation)+" -f alpha -t raw /dev/stdin - ", stdin=rtl_fm.stdout, stdout=subprocess.PIPE, - stderr=open('log_mon.txt','a'), + stderr=open(script_path+"/log_mon.txt","a"), shell=True) except: log("cannot start multimon-ng","error") @@ -225,30 +238,31 @@ try: fms_time_old = timestamp #save last time if useMySQL: #only if MySQL is active - log("FMS to MySQL") + log("insert FMS into MySQL") try: + connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) cursor = connection.cursor() cursor.execute("INSERT INTO "+tableFMS+" (time,service,country,location,vehicle,status,direction,tsi) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",(curtime(),fms_service,fms_country,fms_location,fms_vehicle,fms_status,fms_direction,fms_tsi)) cursor.close() connection.commit() except: - log("FMS cannot insert","error") - + log("FMS cannot insert into MySQL","error") + finally: + connection.close() #Close connection in every case if useHTTPrequest: #only if HTTPrequest is active - log("FMS to HTTP") + log("FMS to HTTP request") try: httprequest = httplib.HTTPConnection(url) httprequest.request("HEAD", "/") httpresponse = httprequest.getresponse() #if args.verbose: print httpresponse.status, httpresponse.reason except: - log("FMS cannot request","error") - + log("FMS HTTP request failed","error") else: log("No valid FMS: "+fms_id) else: - log("CRC incorrect") + log("FMS CRC incorrect") #ZVEI Decoder Section @@ -265,29 +279,29 @@ try: log("5-Ton: "+zvei_id,"info") zvei_id_old = zvei_id #save last id zvei_time_old = timestamp #save last time - - + if useMySQL: #only if MySQL is active - log("ZVEI to MySQL") + log("insert ZVEI into MySQL") try: + connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) cursor = connection.cursor() cursor.execute("INSERT INTO "+tableZVEI+" (time,zvei) VALUES (%s,%s)",(curtime(),zvei_id)) cursor.close() connection.commit() except: - log("ZVEI cannot insert","error") - + log("ZVEI cannot insert into MySQL","error") + finally: + connection.close() #Close connection in every case if useHTTPrequest: #only if HTTPrequest is active - log("ZVEI to HTTP") + log("ZVEI to HTTP request") try: httprequest = httplib.HTTPConnection(url) httprequest.request("HEAD", "/") httpresponse = httprequest.getresponse() #if args.verbose: print httpresponse.status, httpresponse.reason except: - log("ZVEI cannot request","error") - + log("ZVEI HTTP request failed","error") else: log("No valid ZVEI: "+zvei_id) @@ -296,9 +310,6 @@ except KeyboardInterrupt: except: log("unknown Error","error") finally: - if useMySQL: #only if MySQL is active - log("disconnect MySQL") - connection.close() rtl_fm.terminate() log("rtl_fm terminated") multimon_ng.terminate() diff --git a/config.ini b/config.ini index 98969de..8771905 100644 --- a/config.ini +++ b/config.ini @@ -4,7 +4,7 @@ [FMS] #time to ignore same alarm in a row (sek) -double_ignore_time = 10 +double_ignore_time = 5 [ZVEI] #time to ignore same alarm in a row (sek) diff --git a/install.sh b/install.sh index 23eac5a..76e3f6d 100644 --- a/install.sh +++ b/install.sh @@ -14,26 +14,26 @@ echo "" echo "Caution, script don't install a Webserver with PHP and MySQL" echo "So you have to make up manually if you want to use MySQL support" -mkdir -p /home/pi/bos/install +mkdir -p ~/bos/install tput cup 13 15 echo "[ 1/10] [#---------]" tput cup 15 5 echo "-> make a apt-get update................" -apt-get update > /home/pi/bos/install/setup_log.txt 2>&1 +apt-get update > ~/bos/install/setup_log.txt 2>&1 tput cup 13 15 echo "[ 2/10] [##--------]" tput cup 15 5 echo "-> download GIT an other stuff.........." -apt-get -y install git cmake build-essential libusb-1.0 qt4-qmake libpulse-dev libx11-dev sox >> /home/pi/bos/install/setup_log.txt 2>&1 +apt-get -y install git cmake build-essential libusb-1.0 qt4-qmake libpulse-dev libx11-dev sox >> ~/bos/install/setup_log.txt 2>&1 tput cup 13 15 echo "[ 3/10] [###-------]" tput cup 15 5 echo "-> download rtl_fm......................" -cd /home/pi/bos/install -git clone git://git.osmocom.org/rtl-sdr.git >> /home/pi/bos/install/setup_log.txt 2>&1 +cd ~/bos/install +git clone git://git.osmocom.org/rtl-sdr.git >> ~/bos/install/setup_log.txt 2>&1 cd rtl-sdr/ tput cup 13 15 @@ -41,17 +41,17 @@ echo "[ 4/10] [####------]" tput cup 15 5 echo "-> compile rtl_fm......................" mkdir -p build && cd build -cmake ../ -DINSTALL_UDEV_RULES=ON >> /home/pi/bos/install/setup_log.txt 2>&1 -make >> /home/pi/bos/install/setup_log.txt 2>&1 -make install >> /home/pi/bos/install/setup_log.txt 2>&1 -ldconfig >> /home/pi/bos/install/setup_log.txt 2>&1 +cmake ../ -DINSTALL_UDEV_RULES=ON >> ~/bos/install/setup_log.txt 2>&1 +make >> ~/bos/install/setup_log.txt 2>&1 +make install >> ~/bos/install/setup_log.txt 2>&1 +ldconfig >> ~/bos/install/setup_log.txt 2>&1 tput cup 13 15 echo "[ 5/10] [#####-----]" tput cup 15 5 echo "-> download multimon-ng................" -cd /home/pi/bos/install -git clone https://github.com/EliasOenal/multimonNG.git >> /home/pi/bos/install/setup_log.txt 2>&1 +cd ~/bos/install +git clone https://github.com/EliasOenal/multimonNG.git >> ~/bos/install/setup_log.txt 2>&1 cd multimonNG/ tput cup 13 15 @@ -60,17 +60,17 @@ tput cup 15 5 echo "-> compile multimon-ng................." mkdir -p build cd build -qmake ../multimon-ng.pro >> /home/pi/bos/install/setup_log.txt 2>&1 -make >> /home/pi/bos/install/setup_log.txt 2>&1 -make install >> /home/pi/bos/install/setup_log.txt 2>&1 +qmake ../multimon-ng.pro >> ~/bos/install/setup_log.txt 2>&1 +make >> ~/bos/install/setup_log.txt 2>&1 +make install >> ~/bos/install/setup_log.txt 2>&1 tput cup 13 15 echo "[ 7/10] [#######---]" tput cup 15 5 echo "-> download MySQL Connector for Python." -cd /home/pi/bos/install -wget "http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-1.0.9.tar.gz/from/http://cdn.mysql.com/" -O mysql-connector.tar >> /home/pi/bos/install/setup_log.txt 2>&1 -tar xfv mysql-connector.tar >> /home/pi/bos/install/setup_log.txt 2>&1 +cd ~/bos/install +wget "http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-1.0.9.tar.gz/from/http://cdn.mysql.com/" -O mysql-connector.tar >> ~/bos/install/setup_log.txt 2>&1 +tar xfv mysql-connector.tar >> ~/bos/install/setup_log.txt 2>&1 cd mysql-connector-python* tput cup 13 15 @@ -78,14 +78,14 @@ echo "[ 8/10] [########--]" tput cup 15 5 echo "-> install MySQL Connector for Python.." chmod +x ./setup.py -./setup.py install >> /home/pi/bos/install/setup_log.txt 2>&1 +./setup.py install >> ~/bos/install/setup_log.txt 2>&1 tput cup 13 15 echo "[ 9/10] [#########-]" tput cup 15 5 echo "-> download BOSWatch..................." -cd /home/pi/bos -git clone https://github.com/Schrolli91/BOSWatch >> /home/pi/bos/install/setup_log.txt 2>&1 +cd ~/bos +git clone https://github.com/Schrolli91/BOSWatch >> ~/bos/install/setup_log.txt 2>&1 tput cup 13 15 echo "[10/10] [##########]" @@ -96,5 +96,5 @@ chmod +x * echo "# blacklist the DVB drivers to avoid conflict with the SDR driver\n blacklist dvb_usb_rtl28xxu \n blacklist rtl2830\n blacklist dvb_usb_v2\n blacklist dvb_core" >> /etc/modprobe.d/boswatch_blacklist_sdr.conf tput cup 17 1 -echo "BOSWatch are now in /home/pi/bos/BOSWatch/" +echo "BOSWatch are now in ~/bos/BOSWatch/" echo "Install ready!" \ No newline at end of file