Friday, November 06, 2015

Sending Whatsapp Messages from Raspberry Pi

yowsup is the Whatsapp library. It is written in python so we can run even with a Raspberry Pi. After the installation there is a client script that we can send message to other Whatsapp user. 

Reasons to use yowsup:
  1. send messages to mobile device like SMS
  2. the cost is cheap: all we need is just a mobile number ( a number that has not been registered in Whatsapp) and a Raspberry Pi ( I am using Raspberry Pi B2, but any model should be found)
Installation

$ virtualenv2 venv/
New python executable in venv/bin/python2
Also creating executable in venv/bin/python
Installing setuptools, pip, wheel...done.
$ . venv/bin/activate
(venv)$ git clone https://github.com/tgalal/yowsup

Cloning into 'yowsup'...
remote: Counting objects: 6983, done.
remote: Total 6983 (delta 0), reused 0 (delta 0), pack-reused 6983
Receiving objects: 100% (6983/6983), 1.49 MiB | 574.00 KiB/s, done.
Resolving deltas: 100% (4388/4388), done.
Checking connectivity... done.

python setup.py  install

After the setup there will be a script "yowsup-cli"

Registration

Prepare the following information:
    • the phone number           (XXXXXXXX)
    • the country code             (CCC)
    • the mobile country code (mcc)
    • the mobile network code (mnc)

Note that the phone number is in the form of Country Code + Number. And the mcc and mnc can be found at https://en.wikipedia.org/wiki/Mobile_country_code

For example, if you are using Verizon LTE in US, that the mcc and mnc should be 310 and 012 respectively.

Run the command:
./yowsup-cli registration --requestcode sms --phone CCCXXXXXXXX --cc CCC --mcc 310 --mnc 012

INFO:yowsup.common.http.warequest:{"status":"sent","length":6,"method":"sms","retry_after":1805}

status: sent
retry_after: 1805
length: 6
method: sms


If you are lucky enough you will receive a SMS from the mobile phone.

With this WhatsApp code we can run another command:
./yowsup-cli registration --register 800-269 --phone CCCXXXXXXXX --cc CCC
INFO:yowsup.common.http.warequest:{"status":"ok","login":"CCCXXXXXXXX","pw":"kaiaCmXHgRxxxvuOfbbCFHlqbLI=","type":"new","expiration":1478245880,"kind":"free","price":"$1.00","cost":"1.00","currency":"USD","price_expiration":1449782266}

status: ok
kind: free
pw: kaiaCmXHgRxxxvuOfbbCFHlqbLI=
price: $1.00
price_expiration: 1449782266
currency: USD
cost: 1.00
expiration: 1478245880
login: CCCXXXXXXXX
type: new


Note that the line pw: xxxxxxxxxxxxx . That's the password we need to put in a config file.

create a file yowsup-cli.conf with the following content:

cc=CCC # The country code
phone=XXXXXXXX # the phone number
id= # 
password=xxxxxxxxxxx # the password

Finally we can test by sending a message to a Whatsapp user (eg. CCCYYYYYYYY):

./yowsup-cli demos -c yowsup-cli.conf -s YYYYYYY “hello world”


By using the simple command we can send Whatsapp message to another user programmatically. Possible usages are to send alerts when some health check jobs failed, or writing authentication apps that can send the passcode.

Ref:
  • https://github.com/tgalal/yowsup
  • https://en.wikipedia.org/wiki/Mobile_country_code



Friday, October 30, 2015

Using trickle to control the bandwidth usage of a particular process

Recently I have written a little python script to upload my photos to flickr using the flickr api. It was fun and I will share how to do it later. However when I started uploading 600 photos to flickr at home my wife complained the Internet speed was too slow. She said she could not watch youtube and even some foreign website!

Telling her to wait is not a solution ( I could be killed :p ). So I had stop the script and wait for the 'non-busy hour' to continue. However, this way is not practical, because it's hard to define a non-busy hour. In fact, mid night might not be good as well, because sometimes I need to have a conference call. Therefore, I tried to find if any tools that can limit the traffic so that the script will not upset our life.

Finally I found trickle and it totally solves my problem.

First, we have to install trickle.

Depending on the distributions:

  • pacman -S trickle
  • apt-get install trickle
  • yum install trickle

To run a program with limited bandwidth, just run:
$ trickle -s -u UPLOAD -d DOWNWORD THE_PROGRAM

in my case:

$ trickle -s -u 40 -d 40 python flickr_upload.py

-d and -u are to specify the download and upload speed respectively. Remember the unit is KB/s while your ISP usually use kbs as the unit. You will need to convert it in order to set a reasonable throughput for the process. You can check with this online conversion calculator

If you have a set of programs run by an user you can limit them buy running a trickled. For example:
$ trickled -u 40 -d 40
$

Then you can limit any program run by this user with this limit setting:
$ trickle program1

$ trickle program2
$ # more...

After limited the upload and download speed of my python script, everybody is satisfied. Though it takes much longer to upload photos to flickr, it's okay for me and my wife can browse and watch whatever she wants. Happy ending...

PS: 
Please note that trickle cannot resolve all cases. It only works when the application (the binary) is using the dynamic library glibc. To check if it is using the glibc, you can use the following command

$ ldd /usr/bin/python |grep libc
libc.so.6 => /usr/lib/libc.so.6 (0x00007f96a991f000)

If it is not using glibc, then you have find another way, probably using iptable or enable QoS on the router if it is supported

Ref:

  1. https://wiki.archlinux.org/index.php/Trickle
  2. http://linux.die.net/man/1/trickle

Thursday, September 24, 2015

12 Things do to after just started your DigitalOcean VPS

Recently I helped one of my client to setup a VPS on DigitalOcean (DO). The price is very competitive (the cheapest plan is $5 per month) and they offer SSD as the storage. I highly recommend to give a try if someone is looking for a virtual hosting.

Actually a VPS is just a server running on other location. Once you subscribe a plan and start to OS, it's your responsibility to maintain the system.

The steps below are to enhance the security once the server is deployed ( we are using Ubuntu 14.04 in this case). The objective is to harden the server so that it can only be accessed from a client with ssh

1. Setup ssh keyfrom a linux server at home or office. Assumed you have a linux client with a user "user1"

Friday, September 11, 2015

Benchmarking of Hard disks on N54L using Bonnie++

Getting the hard disk details

First we use smartctl to get the details, you can see there are 3 disks. 2x WD Green 2TB 5400rpm and 1x HP 500GB 7200rpm. The HP one is bundled and I think it's not enough for me to store my data So I buy the WD disks and make it as a mirror.

#smartctl -i /dev/sda
smartctl 6.4 2015-06-04 r4109 [x86_64-linux-3.14.51-1-lts] (local build)
Copyright (C) 2002-15, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Model Family:     Western Digital Green
Device Model:     WDC WD20EZRX-00D8PB0
Serial Number:    WD-xxxxxxxxxx
LU WWN Device Id: 5 0014ee 6055d1735
Firmware Version: 80.00A80
User Capacity:    2,000,398,934,016 bytes [2.00 TB]
Sector Sizes:     512 bytes logical, 4096 bytes physical
Rotation Rate:    5400 rpm
Device is:        In smartctl database [for details use: -P show]
ATA Version is:   ACS-2 (minor revision not indicated)
SATA Version is:  SATA 3.0, 6.0 Gb/s (current: 3.0 Gb/s)
Local Time is:    Fri Sep 11 17:09:32 2015 HKT
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

Continue...

Friday, August 28, 2015

Pass Store (Password Manager in command line)


Everybody is facing a problem: too many password to remember. If there are too many to store in brian, then its time to store somewhere else. Some people will simply write on a notebook, some will store it in an excel, and some will download a password manager such as KeePass/KeePassX and LastPass. But as a Linux guy I would like to try another tool in command line: Pass (http://www.passwordstore.org/)

Why need a command line password manager? Having a command line password manager can provide flexibility:

  • you use SSH to connect to the Linux so it is secure and you can view a password of a particular account without touching a mouse
  • you can write simple script to deal with the passwords. for example, generate a number of account/ password pair, or dump a password list
  • This tool is very lightweight. You can store your passwords in a Raspberry Pi !


If you visit the website you may find it is not difficult to use. However, there are some tricks

1. You must setup gpg keyrings before initialising the pass store
2. The name of the pass store is the ID of the gpg key.

Once you setup the pass store you are good to enjoy this tool.

The following steps are the example:

Tuesday, August 25, 2015

HP Gen7 N54L Hardware Upgrade


I have a HP Gen7 N54L. Although Gen8 is released but it also makes N54L more attractive. It is a Microserver and it's worth to get one as a home server. There is a 4 bay enclosure. If you make use of the space of optical drive and the eSata output, the server can serve totally 6 SATA devices which is good enough for a storage server for a SME. 

Originally the configuration is a 4GB ECC memory with a 500GB Black disk. The CPU is AMD Turion(tm) II Neo N54L Dual-Core Processor. It is embedded in the mainboard so there is no way to upgrade it. Also, the main board doesn't have any USB3.0, which is not acceptable nowadays. 

So what can we do in order to make it more productive? For me, I have purchased the following :
  • 2x 2TB Green disks (with software raid, will explain later)
  • Add 4GB non-ECC memory (totally 8GB)
  • Add a USB3.0 pci low profile adapter
I also upgraded the BIOS which can make the harddisks hot pluggable. 

Since I need some time to make a short procedure for the hardware, I just show the result at the moment.

# free
total used free shared buff/cache available
Mem: 8052260 826372 6648096 14800 577792 6854592
Swap: 0 0 0
# lspci |grep -i USB.3
02:00.0 USB controller: Renesas Technology Corp. uPD720202 USB 3.0 Host Controller (rev 02)

Of course I am a Linux guy so it is expected I installed a Linux on it. You can see there are 6GB free right now. And I can even use the USB3.0 ports to connect more hard disks (to backup, for example).