Benjamin Renard commited on 2012-07-25 17:04:50
Showing 3 changed files, with 237 additions and 0 deletions.
| ... | ... |
@@ -0,0 +1,34 @@ |
| 1 |
+#!/bin/bash |
|
| 2 |
+# |
|
| 3 |
+# Nagios plugin to check Gnokii USB modem state |
|
| 4 |
+# |
|
| 5 |
+# Author : Benjamin Renard <brenard@easter-eggs.com> |
|
| 6 |
+# Date : Wed, 25 Jul 2012 18:33:33 +0200 |
|
| 7 |
+# |
|
| 8 |
+ |
|
| 9 |
+# Path to gnokii binary (default : /usr/bin/gnokii) |
|
| 10 |
+GNOKII=/usr/bin/gnokii |
|
| 11 |
+ |
|
| 12 |
+# Path to gnokii config file (default : /etc/gnokiirc) |
|
| 13 |
+CONFIG=/etc/gnokiirc |
|
| 14 |
+ |
|
| 15 |
+KEY=$( $GNOKII --config $CONFIG --identify 2>/dev/null|tr '\n' " "|grep IMEI|sed 's/IMEI *: \([0-9]*\) Manufacturer *: \(.*\) Model *: \(.*\) Product name.*Revision.*/\2 \3 \(IMEI \: \1\)/' ) |
|
| 16 |
+if [ ! -n "$KEY" ] |
|
| 17 |
+then |
|
| 18 |
+ echo "CRITICAL - USB key not detected" |
|
| 19 |
+ exit 2 |
|
| 20 |
+fi |
|
| 21 |
+ |
|
| 22 |
+PINSTATUS=$( $GNOKII --config $CONFIG --getsecuritycodestatus 2>/dev/null|grep "Security code status"|sed 's/Security code status: //' ) |
|
| 23 |
+if [ "$PINSTATUS" == "waiting for PIN." ] |
|
| 24 |
+then |
|
| 25 |
+ echo "CRITICAL - SIM Locked ! You have to unlocked SIM !" |
|
| 26 |
+ exit 2 |
|
| 27 |
+elif [ "$PINSTATUS" != "nothing to enter." ] |
|
| 28 |
+then |
|
| 29 |
+ echo "UNKNOWN - PIN status unkwown : $PINSTATUS" |
|
| 30 |
+ exit 3 |
|
| 31 |
+fi |
|
| 32 |
+ |
|
| 33 |
+echo "OK - Key : $KEY - SIM Unlocked" |
|
| 34 |
+exit 0 |
| ... | ... |
@@ -0,0 +1,99 @@ |
| 1 |
+#!/usr/bin/perl |
|
| 2 |
+# |
|
| 3 |
+# gnokii-init-modem |
|
| 4 |
+# |
|
| 5 |
+# ABOUT |
|
| 6 |
+# |
|
| 7 |
+# Copyright Benjamin Renard 2012 <brenard@easter-eggs.com> |
|
| 8 |
+# |
|
| 9 |
+# Inspired by Roy Sigurd Karlsbakk's 'gnokii-init-modem' script |
|
| 10 |
+# Source : http://karlsbakk.net/gnokii-init-modem/ |
|
| 11 |
+# |
|
| 12 |
+# Licensed under GPL v2.0. See LICENSE file for details. |
|
| 13 |
+# |
|
| 14 |
+# This software initialize a GSM modem. |
|
| 15 |
+# |
|
| 16 |
+ |
|
| 17 |
+use Device::SerialPort qw( :PARAM :STAT 0.07 ); |
|
| 18 |
+ |
|
| 19 |
+# |
|
| 20 |
+# Configuration |
|
| 21 |
+# |
|
| 22 |
+ |
|
| 23 |
+ |
|
| 24 |
+my $port_name = '/dev/ttyUSB0'; # This is the serial port device. |
|
| 25 |
+my $pin = '0000'; # Here goes our pin code. |
|
| 26 |
+my $ready_timeout = 60; # GSM-ready timeout in seconds. |
|
| 27 |
+my $debug=1; |
|
| 28 |
+ |
|
| 29 |
+# Communication settings |
|
| 30 |
+my $baudrate = 9600; # Set to current speed. |
|
| 31 |
+my $parity = 'none'; # Set to 'none', 'even', or 'odd'. |
|
| 32 |
+my $databits = 8; # Between 5 and 8. Only 8 is commonly used. |
|
| 33 |
+my $stopbits = 1; # Set to 1 or 2. 1 is most common. |
|
| 34 |
+my $handshake = 'rts'; # Set to 'none', 'rts' or 'xoff' |
|
| 35 |
+ |
|
| 36 |
+# Functions |
|
| 37 |
+ |
|
| 38 |
+sub debug {
|
|
| 39 |
+ (my $text)=@_; |
|
| 40 |
+ if ($debug eq 0) {
|
|
| 41 |
+ print "."; |
|
| 42 |
+ } |
|
| 43 |
+ else {
|
|
| 44 |
+ print "$text\n"; |
|
| 45 |
+ } |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+# Main code |
|
| 49 |
+ |
|
| 50 |
+print "Initialising GSM modem at $port_name..."; |
|
| 51 |
+ |
|
| 52 |
+debug ""; |
|
| 53 |
+debug "Open device $port_name"; |
|
| 54 |
+my $PortObj = new Device::SerialPort ($port_name) or |
|
| 55 |
+ die "Can't open device $port_name: $!\n"; |
|
| 56 |
+ |
|
| 57 |
+debug "Set options"; |
|
| 58 |
+$PortObj->handshake($handshake); |
|
| 59 |
+$PortObj->baudrate($baudrate); |
|
| 60 |
+$PortObj->parity($parity); |
|
| 61 |
+$PortObj->databits($databits); |
|
| 62 |
+$PortObj->stopbits($stopbits); |
|
| 63 |
+ |
|
| 64 |
+$PortObj->read_char_time(0); # don't wait for each character |
|
| 65 |
+$PortObj->read_const_time(1000); # 1 second per unfulfilled "read" call |
|
| 66 |
+ |
|
| 67 |
+debug "Start communication"; |
|
| 68 |
+debug " -> Send ATZ"; |
|
| 69 |
+$PortObj->write("ATZ\r\n"); sleep(2);
|
|
| 70 |
+debug " -> Send ATE1"; |
|
| 71 |
+$PortObj->write("ATE1\r\n"); sleep(2);
|
|
| 72 |
+debug " -> Send AT+CMEE=1"; |
|
| 73 |
+$PortObj->write("AT+CMEE=1\r\n"); sleep(2);
|
|
| 74 |
+debug " -> Send AT+CSCS"; |
|
| 75 |
+$PortObj->write("AT+CSCS\r\n"); sleep(2);
|
|
| 76 |
+ |
|
| 77 |
+debug " -> Read return ..."; |
|
| 78 |
+(my $count_in, my $string_in) = $PortObj->read(256); |
|
| 79 |
+debug "Return :\n\n----\n$string_in----\n"; |
|
| 80 |
+if ($string_in =~ /CME ERROR/ms) {
|
|
| 81 |
+ debug "SIM locked."; |
|
| 82 |
+ debug " -> Send AT+CPIN=\"PIN\""; |
|
| 83 |
+ $PortObj->write("AT+CPIN=\"$pin\"\r\n"); sleep(2);
|
|
| 84 |
+ debug " -> Read return ..."; |
|
| 85 |
+ (my $count_in, my $string_in) = $PortObj->read(256); |
|
| 86 |
+ debug "Return :\n\n----\n$string_in----\n"; |
|
| 87 |
+ if ($string_in =~ /OK/ms) {
|
|
| 88 |
+ print "Sim unlocked\n"; |
|
| 89 |
+ exit 0; |
|
| 90 |
+ } |
|
| 91 |
+ else {
|
|
| 92 |
+ print "ERROR unlocking SIM (bad PIN code ?)\n"; |
|
| 93 |
+ exit 1; |
|
| 94 |
+ } |
|
| 95 |
+} |
|
| 96 |
+else {
|
|
| 97 |
+ print "SIM already unlocked\n"; |
|
| 98 |
+ exit 0; |
|
| 99 |
+} |
| ... | ... |
@@ -0,0 +1,104 @@ |
| 1 |
+#!/usr/bin/perl -w |
|
| 2 |
+ |
|
| 3 |
+# # mail2gnokii |
|
| 4 |
+# |
|
| 5 |
+# Postfix transport to sending SMS by gnokii |
|
| 6 |
+# |
|
| 7 |
+# Usage : |
|
| 8 |
+# |
|
| 9 |
+# in master.cf : |
|
| 10 |
+# |
|
| 11 |
+# sms unix - n n - 1 pipe |
|
| 12 |
+# flags=Rq user=gnokii:dialout argv=/usr/local/sbin/mail2gnokii ${user}
|
|
| 13 |
+# |
|
| 14 |
+# in transport : |
|
| 15 |
+# |
|
| 16 |
+# sms.example.tld sms |
|
| 17 |
+# |
|
| 18 |
+# # arg1: phone number |
|
| 19 |
+ |
|
| 20 |
+use Email::Simple; |
|
| 21 |
+use Encode qw/encode decode/; |
|
| 22 |
+ |
|
| 23 |
+# Configuration |
|
| 24 |
+$smsc="+33609001390"; |
|
| 25 |
+@authorized_number=("0612345678","0033600000000");
|
|
| 26 |
+$gnokii_config="/etc/gnokiirc"; |
|
| 27 |
+ |
|
| 28 |
+# constantes tirees de <sysexits.h> |
|
| 29 |
+use constant {
|
|
| 30 |
+ EX_OK => 0, |
|
| 31 |
+ EX_USAGE => 64, |
|
| 32 |
+ EX_DATAERR => 65, |
|
| 33 |
+ EX_NOINPUT => 66, |
|
| 34 |
+ EX_NOUSER => 67, |
|
| 35 |
+ EX_NOHOST => 68, |
|
| 36 |
+ EX_UNAVAILABLE => 69, |
|
| 37 |
+ EX_SOFTWARE => 70 |
|
| 38 |
+}; |
|
| 39 |
+ |
|
| 40 |
+ |
|
| 41 |
+if($#ARGV + 1 != 1){
|
|
| 42 |
+ print "Usage : mail2gnokii [phone number]"; |
|
| 43 |
+ exit EX_USAGE; |
|
| 44 |
+} |
|
| 45 |
+ |
|
| 46 |
+ |
|
| 47 |
+ |
|
| 48 |
+# recuperation du mail |
|
| 49 |
+ |
|
| 50 |
+my $text; |
|
| 51 |
+ |
|
| 52 |
+while(<STDIN>){
|
|
| 53 |
+ $text .= $_; |
|
| 54 |
+} |
|
| 55 |
+ |
|
| 56 |
+ |
|
| 57 |
+if($text eq ''){
|
|
| 58 |
+ print "Mail is empty !"; |
|
| 59 |
+ exit EX_NOINPUT; |
|
| 60 |
+} |
|
| 61 |
+ |
|
| 62 |
+my $email = Email::Simple->new($text); |
|
| 63 |
+ |
|
| 64 |
+ |
|
| 65 |
+# recuperation du sujet |
|
| 66 |
+ |
|
| 67 |
+my $subject = $email->header("Subject");
|
|
| 68 |
+ |
|
| 69 |
+if($subject eq ''){
|
|
| 70 |
+ print "No text in Subject header !"; |
|
| 71 |
+ exit EX_NOINPUT; |
|
| 72 |
+} |
|
| 73 |
+ |
|
| 74 |
+$subject = decode('MIME-Header', $subject);
|
|
| 75 |
+ |
|
| 76 |
+ |
|
| 77 |
+ |
|
| 78 |
+# recuperation du numero de mobile |
|
| 79 |
+my $mob=$ARGV[0]; |
|
| 80 |
+ |
|
| 81 |
+if (not $mob =~ m/^[0-9]*$/) {
|
|
| 82 |
+ print "Invalid phone number : $mob"; |
|
| 83 |
+ exit EX_DATAERR; |
|
| 84 |
+} |
|
| 85 |
+ |
|
| 86 |
+sub in_array {
|
|
| 87 |
+ my ($arr,$search_for) = @_; |
|
| 88 |
+ my %items = map {$_ => 1} @$arr; # create a hash out of the array values
|
|
| 89 |
+ return (exists($items{$search_for}))?1:0;
|
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+# Envoie du SMS |
|
| 93 |
+if ($mob ne '' && in_array(\@authorized_number,$mob)){
|
|
| 94 |
+ `echo "$subject" | gnokii --config $gnokii_config --sendsms $mob --smsc $smsc`; |
|
| 95 |
+ if($? != 0){
|
|
| 96 |
+ print "Error sending SMS"; |
|
| 97 |
+ exit EX_SOFTWARE; |
|
| 98 |
+ } |
|
| 99 |
+} |
|
| 100 |
+else{
|
|
| 101 |
+ print "Phone number $mob not in authorized list"; |
|
| 102 |
+ exit EX_DATAERR; |
|
| 103 |
+} |
|
| 104 |
+exit EX_OK; |
|
| 0 | 105 |