Get email upon change of IP address

Sometimes we just don’t have a fixed IP on a PC. Here is how to setup a cron script to email upon change (checked each hour). It uses the sendmail program to setup an email server on your PC.

sudo apt-get install sendmail
then type
> ~/.current_ip

then type
nano ~/scripts/checkIP
and cut and paste (change you@youremail to your email address)

#!/bin/bash

YOUREMAIL="you@youremail"

IPADDRESS=$(hostname -I | tr -d [:space:])
OldIP=$(<~/.current_ip)

if [[ ${IPADDRESS} != ${OldIP} ]]; then
echo "Your new IP address is ${IPADDRESS} (old address was ${OldIP} )" | mail -s "IP address change" $YOUREMAIL
echo ${IPADDRESS} > ~/.current_ip
fi

4) chmod +x ~/scripts/checkIP

5)
crontab -e
and then at the top add:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin

and at the bottom add:

0 * * * * ~/scripts/checkIP

Reference: http://www.tuxradar.com/answers/516

7zipping and moving recursively by top-level directory

The following script takes the top-level directories and makes a 7zip archive from each top-level directory, containing all its subdirectories.

This function has a primitive “no-clobber” that won’t overwrite existing files.

Note that you can use up all HDD space if you have a lot of huge files — watch the progress of the program. You assume all risks for using this script.


#!/bin/bash
# Michael Hirsch 2013
# BSD license
# 7zip's and moves files per directory

: ${2?example: 7z_mv ~/inDir /media/outDir}

MainDir=$1
OutDir=$2

#check if OutDir directory already exists
ArcOutDir=$OutDir/$MainDir #not for use inside tar command
if [ ! -d $ArcOutDir ]; then
echo creating $ArcOutDir
mkdir $ArcOutDir
fi

#list ONLY 1st level subdirectories
DirList=($(find "$MainDir" -maxdepth 1 -mindepth 1 -type d | sort))
echo Found directories: ${DirList[@]}
#exit
nDir=${#DirList[@]}
nDir1=$(($nDir - 1))

#main loop
for i in $(seq 0 1 $nDir1); do
currDirIn="${DirList[$i]}"
currArcOut="$OutDir/${DirList[$i]}.7z"

###################
# no clobber
if [ -a $currArcOut ]; then
echo "Skipping directory $currDirIn since $currArcOut already exists"
continue #skips to top of for loop
fi
####################

echo "7zipping and moving $currDirIn to $currArcOut"

# don't use the -o option, it doesn't seem to work right
7za a -t7z -mx=3 -mmt=on -m0=lzma2 "$currArcOut" "$currDirIn"

done

Printing PDF from Matlab on Ubuntu

Sometimes you don’t have a color printer handy, and/or just want to print a color PDF for later reference — say a hand-annotated Matlab figure.
You need the CUPS PDF program, which saves printed PDFs to the ~/PDF directory.


sudo apt-get install cups-pdf

The output directory can be configured by editing the “Out” directory in:
sudo nano /etc/cups/cups-pdf.conf

Note, if you already have Matlab open, just restart Matlab to make the PDF printer available.

NOTE: Due to bug with Java, per Mathworks they say WONTFIX and here is a workaround that should always print in color (perhaps for Mac OS X and Windows as well)

example:
imagesc(rand(10))
print(gcf,’-dpdf’,’test.pdf’)

and you’ll find the color test.pdf in your current directory. Phew!

Disable Beaglebone Black flashing heartbeat LED

To disable the annoying heartbeat LED:

sudo -s
echo none > /sys/class/leds/beaglebone\:green\:usr0/trigger
exit

Note, this will be forgotten at reboot.

You can search the web for how to make this permanent via the device tree (requires extraction and recompilation).

Might also be able to do something with rc.local — didn’t try this.

Reference: http://www.perturb.org/display/1127_BeagleBone_Black__USR_Leds.html

RTL2832 ADS-B decoder

the dump1090 program can be obtained by:


sudo apt-get install git libusb-1.0-0-dev cmake
git clone git://git.osmocom.org/rtl-sdr.git
mkdir ~/rtl-sdr/build
cd ~/rtl-sdr/build
cmake ../ -DINSTALL_UDEV_RULES=ON -DDETACH_KERNEL_DRIVER=ON
make
sudo make install
sudo ldconfig

git clone https://github.com/antirez/dump1090.git
cd ~/dump1090
make

Then, do:

lsusb

and look for the RTL2832 id codes similar to those below, and insert them in ATTRS(idvendor) and idProduct) if they’re different

sudo nano /etc/udev/rules.d/20.rtlsdr.rules

copy and paste this text

SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838", GROUP="adm", MODE="0666", SYMLINK+="rtl_sdr"

then

sudo service udev restart

Now you can run the dump1090 ADS-B program by typing:

~/dump1090/dump1090 --interactive

Note: if you get a complaint about permissions, it’s easiest to just reboot your PC, which should fix the error.

WGET: download an HTTP or FTP directory recursively

Many science data sites are an Apache server with a HTTP index.html file listing, perhaps recursively, a lot of files and directories. You can download them recursively to your PC as follows (use this responsibly, you can consume gigabytes of data at cost to science host or yourself!)


wget -r -np -nc -nH --cut-dirs=4 --random-wait --wait 1 -e robots=off http://mysite.com/aaa/bbb/ccc/ddd/

This plops the files to whatever directory you ran the command in.

To use this on FTP site, just change the http:// to ftp:// and type the proper ftp site address.

Option explanation:
-r: download recursively (and place in recursive folders on your PC)
-np: Never get parent directories (sometimes a site will link back up and you don’t want that)
-nc: no clobber — don’t re-download files you already have
-nH: don’t put obnoxious site name directories on your PC
–cut-dirs=4: don’t put an obnoxious hierarchy of directories above the desired directory on your PC. Note you must set the number equal to the number of directories on server (here aaa/bbb/ccc/ddd is four)
-e robots=off: Many sites will block robots from mindlessly consuming huge amounts of data. Here we override this setting telling Apache that we’re (somewhat) human.
–random-wait: To avoid excessive download requests (that can get you auto-banned from downloading) we politely wait in-between files–better than trying to get yourself un-banned!
–wait 1: making the random wait time average to about 1 second before starting to download the next file.

Matlab missing libstdc++ error

if you get an error like:
/usr/local/MATLAB/R2013a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.15′ not found (required by /usr/lib/libGraphicsMagick++.so.3)

try doing:

cd /usr/local/MATLAB/R2013a_Student/sys/os/glnxa64
sudo mv libstdc++.so.6 libstdc++.so.6.bak
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6

Install Debian to eMMC (internal flash drive) of Beaglebone Black

Note there’s now a semi-automated script to do this. However I liked doing it the way below better.

You need AT LEAST a 4GB micro SD card to do this!

This procedure assumes you’re on a Linux laptop.

1) on your laptop PC, download the Debian Beaglebone Black (BBB) image. To be safe, get the “all revs” 2GB image in case you have a pre-Rev C BBB that only has 2GB eMMC. You can easily expand the partition after install if you have a Rev C BBB with 4GB eMMC.

2) [optional] check that md5 sum is correct by typing in Terminal:

md5sum BBB-eMMC-flasher-debian*2gb.img.xz

this should match the md5 given at the download page.

3) type lsblk, note which drives are listed, then insert the SD card into your laptop and type lsblk again–the new item is your SD card. Let’s say it was /dev/mmcblk0 (check on your PC!!)

4) type in Terminal:

unxz bone-debian*.img.xz

sudo -s

xz -cd BBB-eMMC-flasher-debian*2gb.img.xz > /dev/mmcblk0

5) This operation takes about 5 to 20 minutes. You’re writing data to the SD card at say 2MB/sec if using a Class 4 SD card, and say 6MB/sec if using a Class 10 SD card, and you’re writing uncompressed about 2GB to the SD card. For fun, you can open another Terminal window and type sudo iotop or indicator-multiload to monitor the data writing.

6) insert this micro SD card into the (non-powered) BBB and then apply the power. The BBB’s four onboard LEDs flash back and forth in a “cylon” or “knight rider” pattern. During this time, the micro SD card program is flashing the onboard eMMC automatically for you. If you have an FTDI to USB adapter that fits onto J1, you can watch this process via the screen program.

This automatic flashing to eMMC process should complete in about 10-20 minutes. When it’s done, the BBB shuts itself down (no LEDs on).

7) Once the BBB has shut down, REMOVE the micro SD card from the BBB.

8) Unplug/plug the power or just push the onboard POWER button next to the BBB Ethernet jack.

Note, you might be one of those whose microHDMI adapter/monitor combo doesn’t work with the BBB. Well, it’s OK, since there is an SSH server running by default available through the mini-USB port.

Now power up the BBB by plugging in a mini-USB cable from your laptop to the Beaglebone Black, or use the standard DC power adapter.

You do need to know the IP address of the BBB on your LAN (plug the BBB into your Ethernet).

Then, on your laptop Terminal, (assuming your local network IP addresses are like 192.168.xxx.xxx) type:

nmap -v -sP 192.168.0.0/24 | grep -B1 up

This will list everything on your network. Do it once with the BBB unplugged from the network, then again with the BBB plugged in. Let’s say the BBB is at 192.168.0.5

8) in Terminal on your laptop, type:
ssh debian@192.168.0.5
password: temppwd

Change this password now by typing passwd at the
debian@beaglebone:~$
prompt

Now you’re running with the latest kernel off the eMMC. When I type

df -h

I see I have 89% free eMMC space, using

uname -a

the 3.8.13-bone70 kernel.