Send SMS with Gammu + Ubuntu

After reading this, you should be able to send SMS from commandline using Gammu on top of Ubuntu 16.04.

I have tried this with newer versions of Ubuntu with some minor bumps in the road, but with this exact setup, it should work flawlessly.

Hardware:
I run this as a VM in Proxmox. The VM is assigned one CPU core, 2048 megs of memory, and a small 10 gig drive.
My modem is a Wavecom Fasttrack Supreme 10. This is a COM device, but I have paired it with my VM host with a serial to USB dongle.
In Proxmox I just passed the USB device through to the VM. More on this further down.
Also, I have a simcard from my local carrier. This is SIM Pin protected.

Software:
Bone stock Ubuntu 16.04 server
Gammu sms server daemon
wvdial
… and that’s it really.

First off, you need to make sure it is updated.

sudo apt-get update && sudo apt-get upgrade

Then we can check the server if the USB device is detected.

frank@smsgw01:~$ lsusb
Bus 001 Device 002: ID 0627:0001 Adomax Technology Co., Ltd
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

As you can see, the USB device is not present there. It’s because I haven’t assigned the device to the VM in Proxmox.
Shut down the VM, assigned the USB device, and booted it up again. Now it should look like this:

frank@smsgw01:~$ lsusb
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0627:0001 Adomax Technology Co., Ltd
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

So, the USB device is attached and recognized.
The next step is to se if our OS can see this device as a GSM modem.
Install wvdial:

sudo apt-get install wvdial

When the installation is finished, ron wvdialconf to check for applicable modems:

frank@smsgw01:~$ sudo wvdialconf
Editing `/etc/wvdial.conf'.

Scanning your serial ports for a modem.

Modem Port Scan<*1>: S0   S1   S2   S3   S4   S5   S6   S7
Modem Port Scan<*1>: S8   S9   S10  S11  S12  S13  S14  S15
Modem Port Scan<*1>: S16  S17  S18  S19  S20  S21  S22  S23
Modem Port Scan<*1>: S24  S25  S26  S27  S28  S29  S30  S31
ttyUSB0<*1>: ATQ0 V1 E1 -- failed with 2400 baud, next try: 9600 baud
ttyUSB0<*1>: ATQ0 V1 E1 -- �ۓ��
ttyUSB0<*1>: failed with 9600 baud, next try: 115200 baud
ttyUSB0<*1>: ATQ0 V1 E1 -- OK
ttyUSB0<*1>: ATQ0 V1 E1 Z -- OK
ttyUSB0<*1>: ATQ0 V1 E1 S0=0 -- OK
ttyUSB0<*1>: ATQ0 V1 E1 S0=0 &C1 -- OK
ttyUSB0<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 -- OK
ttyUSB0<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 -- OK
ttyUSB0<*1>: Modem Identifier: ATI -- WAVECOM MODEM
ttyUSB0<*1>: Speed 230400: AT --
ttyUSB0<*1>: Speed 230400: AT --
ttyUSB0<*1>: Speed 230400: AT --
ttyUSB0<*1>: Max speed is 115200; that should be safe.
ttyUSB0<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 -- OK

Found a modem on /dev/ttyUSB0.
Modem configuration written to /etc/wvdial.conf.
ttyUSB0: Speed 115200; init "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0"

Voilà – we have a working modem on /dev/ttyUSB0

Next, we’ll install Gammu.

sudo apt-get install gammu

When the installation is complete, use the gammu-config command to configure the communication with the GSM modem.

gammu-config

You should now see this:

Change everything to this,

and hit Save.

Now, if your SIM card is not Pin protected, you can try to send a SMS with the following command:

frank@smsgw01:~$ sudo gammu --sendsms TEXT <landcode and phonenumber> -text "lol"
If you want break, press Ctrl+C...
Sending SMS 1/1....waiting for network answer..OK, message reference=11

If your simcard is PIN Locked, check status, and give the modem a PIN with the following commands:

# sudo gammu getsecuritystatus
Waiting for Pin.
# sudo gammu entersecuritycode PIN -
Enter PIN code:

Congrats, you can now bug your frinds not only by texting on the phone, but also form commandline.

 

Having trouble?
I have had some issues with my gateway, and this mainfests itself in Gammu with this errormessage:

Can not access SIM card.

In this case, try the following:
1. Make sure your SIM card is properly inserted into SIM tray.
2. Make sure the SIM card is activated. Test if you can send and receive messages in normal phone.
3. Powercycle your device. Please remember that you must insert a SIM card when the device is SWITCHED OFF.

In my case, Powercycling the device has hepled.

Linux: Logfiles, timestamps and datematching

I recently came across someting at work that had me bothered for a while. A customer wanted to check some logfiles, where this one specific line is stamped ever so often. If the line is NOT stamped in a timely matter, they wanted to be notified.

Seems simple, right? Well, for me it wasn’t that easy. I rarely do scripts in bash – and when I do, I usually use a lot of time on something that would have taken me five minutes to write in powershell.

Nevertheless, here is what I came up with. Note that this most definitely can be done in simpler and better ways, but this works for me.

So. The logfile is huge, but the guys that “owns” it, only wanted to be notified when a certain string wasn’t beeing stamped in a timely manner.
The string in question is beeing stamped every minute – meaning, a threshold of not beeing written to in the last 10 minutes would do the trick here.

Example of the string we are interested in:

10:24:00,002 DEBUG [scheduled.jobs.ScheduledTasksJob] (EJB default - 4) Starting Scheduled Tasks Job

To manage this, we will be using cat, grep, tail, and awk.

cat to read the logfile
grep to grab the string we are looking for
tail to fetch the last occurcance of this string
awk to fetch the timestamp, which is in the beginning of the string, and split on comma in this case, to only fetch the actual time.
date to do some dateformat-juggeling.

#!/bin/bash

# var
logname="/tmp/file.log"
logcheck=$( cat $logname | grep 'Starting Scheduled Tasks Job' | tail -1 | awk -F ',' '{print $1}' )
logdate=$( date -d $logcheck +%H:%M:%S )
nowminus10=$( date -d "-10 minutes" +%H:%M:%S )
nowplus10=$( date -d "+10 minutes" +%H:%M:%S )
now=$( date +%H:%M:%S )

# construct
if [[ "$logdate" > "$nowminus10" && "$logdate" < "$nowplus10" ]];
then
	echo "HEALTHY: The logfile is beeing written to in a timely manner."
else
	echo "ERROR: The logfile is not beeing written to in a timely manner. The time now: $now. Last timestamp in log: $logdate."
fi

This just echoes out the result – it is up to you to do something useful with the script.

In my case, I use this with SCOM – more on this in the next article.

-F

Install Grafana on Debian

Here I will show you how to install Grafana on a headless Debian server using the last current (at the time) stable release.

First, make sure your Debian server is up to date:

sudo apt-get update && apt-get upgrade

Download the Grafana stable release. I put theese packages in the /tmp folder:

cd /tmp/
sudo wget https://dl.grafana.com/oss/release/grafana_5.4.3_amd64.deb

Install adduser and libfontconfig:

sudo apt-get install -y adduser libfontconfig

Unpack grafana:

sudo dpkg -i grafana_5.4.3_amd64.deb

Start Grafana server:

systemctl unmask grafana-server.service
systemctl start grafana-server

You should now reach your server on the standard webport for Grafana:

http://your.ip.address.here:3000

Install InfluxDB on Debian

I use InfluxDB paired together with HomeAssistant to easy and effortlessly access data by query.
Also, this would be an ideal way to make data available to Grafana.

First of all, make sure that your Debian server is up to date:

sudo apt-get update && apt-get upgrade

Install Curl:

sudo apt-get install curl

Add the InFluxData repository:

curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/os-release
test $VERSION_ID = "7" && echo "deb https://repos.influxdata.com/debian wheezy stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
test $VERSION_ID = "8" && echo "deb https://repos.influxdata.com/debian jessie stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
test $VERSION_ID = "9" && echo "deb https://repos.influxdata.com/debian stretch stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

Install InFluxDB Service:

sudo apt-get update && sudo apt-get install influxdb

Start InfluxDB Service (See last bracket if you are using Debian 8+)

sudo service influxdb start
sudo systemctl unmask influxdb.service
sudo systemctl start influxdb

And then you are done.
You should now reach your installation on the default port 8086 (in my case, it’s 8083 because of other hosted sites – more information here: https://docs.influxdata.com/influxdb/v1.7/administration/ports/)

http://your.ip.address.here:8086/