SMS Gateway with Siemens TC35i
To support the computer monitoring at the University of Trier, a Nagios system was set up, which tests the function of important systems at regular intervals and notifies the administrators of problems if necessary. However, there was initially only email notification, so that the problem report was sometimes seen very late, which in exceptional cases may cause further problems. That’s why we wanted to implement a faster notification, in this case via an SMS Gateway.
Basic information
As hardware we use a Siemens TC35i on a Ubuntu 8.04 server system. Since the modem is addressed via serial interface, it should be available on the computer (or can be retrofitted via USB adapter).
Linux offers a program called smstools, which is supposed to take over the function of an SMS gateway. After many attempts and constant low success (zero is just zero) the smstools were shot into the moon, because nothing worked. More or less by accident I stumbled upon the program gnokii, which I knew from synchronization attempts with my mobile phone. But this tool has some nice features that we could use here.
Setting up your SMS Gateway
In preparation, you should get a SIM card that is able to send SMS (sufficient credit, known PIN,…). You should deactivate the PIN entry to avoid further problems in this direction.
On the target system, you should set up a new user and a new group to restrict the rights of the scripts used later. However, insane or suicidal administrators are also welcome to use the root user. We also install gnokii for communication with the modem and xinetd as the recipient of the SMS to be sent. (I assume that you are on the system as root and can execute the following commands):
useradd -d /home/smsd -m smsd apt-get install gnokii gnokii-cli gnokii-common xinetd
As a further step, add the newly created user smsd to the group dialout so that this user can access the serial ports.
Then switch to the user smsd and go to his home directory and create the file .gnokiirc with the following content (provided that the modem is connected to /dev/ttyS0, please don’t forget the dot in the file name):
[global] port = /dev/ttyS0 model = AT initlength = default connection = serial
Now you should be able to access the modem. You can test this with the following command:
gnokii –identify
If you didn’t switch to the user smsd, you get a nice error now, just switch, then he finds the file with the settings to the modem and spits something nice out. Now we create a shell script, which is called by xinetd and passes the SMS to gnokii. This file is called sendsms. sh (also in the home of smsd):
#!/bin/bash LOGFILE=/home/smsd/sendsms.log CONFFILE=/home/smsd/.gnokiirc read telnr read text while read line do text=”$text\n$line” done echo ========= `date +’%d.%m.%Y %H:%M’` =========== >> $LOGFILE echo -e “SMS TO $telnr FROM $REMOTE_HOST” >> $LOGFILE echo “== BEGIN ==” >> $LOGFILE echo -e “$text” | sed -e “s/ *$//” >> $LOGFILE echo “== END ==” >> $LOGFILE echo -e “$text” | sed -e “s/ *$//” | gnokii –config $CONFFILE –sendsms “$telnr” >> $LOGFILE echo “gnookii return code was: $?” >> $LOGFILE
This script reads from the first line of the incoming data the mobile phone number to which the SMS is to be transmitted. The remaining text (up to the EOF) is sent as content of the SMS. So that you can run the program, we change the permissions:
chmod u+x /home/smsd/sendsms.sh
Next, we’ll convince xinetd to listen on a port and call our small send script at data input. This will be done by creating the file (again as root) /etc/xinetd. d/smsd:
service smsd { id = smsd type = UNLISTED socket_type = stream protocol = tcp port = 1254 wait = no user = smsd group = smsd groups = yes server = /home/smsd/sendsms.sh }
Now restart xinetd:
/etc/init.d/xinetd restart
Testing your SMS Gateway
Now your SMS Gateway should work. A small test sequence with the program netcat:
echo -e “+49123456789\nHello world!” | netcat -q 0 12.34.56.78 1254
This line sends an SMS with the text “Hello world!” to the mobile phone number +49123456789. In this case, it is assumed that the gateway’s IP address is 12.34.56.78. The port can be created in the xinetd configuration.
By the way, it makes sense to set up a small firewall via iptables, who is allowed to send and who isn’t, otherwise anyone who somehow gets to the computer can access it. Very diligent people can of course also implement authentication.
If you want to, you can of course use other usernames, paths, etc., but a certain basic understanding of Linux systems is required.