Posts Tagged bash

Weather Underground forecast data in text display

One of the uses I found for my Raspberry Pi was using it to display weather data. I retrieved the weather data from Weather Underground using their API, parsed it, and displayed it on my RPi’s small LCD. This gave me an always-on view of the weather, which was nifty, and it was done all in bash scripting with a few external programs to parse the data.

This script is very customizable and extensible. You could use it to do any number of weather-related tasks.

I reduced the font size on the LCD to Terminus 6×12 using the following command:

sudo dpkg-reconfigure console-setup

This helped make room for all the forecast data on the tiny LCD. If you are running this on a PC, it’s not necessary.

For the smoothest updates, I have found it ideal to run the script under ‘watch’, as so:

watch ./weather.sh

I tired doing a ‘while true; do… clear… done’ loop, but the refresh rate was too low and the updates were not smooth.

Here is the script:

UPDATE : This has moved to github, here.

Last updated: 11-21-2015

, , , , ,

2 Comments

Bash script to send contents of file containing URLs to Synology Download Station via API

This bash script will read a file containing a list of URLs line-by-line and send each of those URLs to a Synology DiskStation’s Download Manager via the published API.

Read the comments.

Note that, in user land, it might be easier to simply upload the text file to the Download Station. The below is useful if you want to programatically pass download tasks to Download Station, such as on the update of a web page, etc, etc.

UPDATE: This has been moved to github, here.

If you have any questions or comments on this script, please feel free to comment below. Thank you!

, , ,

2 Comments

Disable and remove .DS_Store files stored on network locations

So today I was going through my Synology NAS and noticed .DS_Store files all over the place.

These are actually files containing extended attributes created by Finder in Mac OS X. But, since they get written out to network locations, they can cause backup and versionining issues.

To disable them from being created on network locations, open a Terminal and run the following

defaults write com.apple.desktopservices DSDontWriteNetworkStores true

(Note: This only affects the currently-logged-in user)

Now in my case, I had these files all over my Synology NAS, so I was able to easily get rid of them by SSHing into the box and running the following:

find / -name .DS_Store -delete

And… done.

, , , , ,

3 Comments

Disable indexing and generation of @eaDir directories on Synology NAS

Various forums throughout the Internet have users stating that even though they’ve disabled media indexing the @eaDir folders are still being generated, and even outside the indexed folders.

In order to completely stop the generation of @eaDir folders, it’s necessary to disable the services that are generating them.

Note that after a DSM update, these services may be re-enabled.

To disable these services, log in to your Synology NAS via SSH, then do the following:

cd /usr/syno/etc.defaults/rc.d/
chmod 000 S66fileindexd.sh S66synoindexd.sh S77synomkthumbd.sh S88synomkflvd.sh S99iTunes.sh

After disabling the services, you may want to delete all the created @eaDir directories.

Any feedback on the above is welcome, please leave it in the comments section below. Thank you!

, , , ,

9 Comments

Getting rid of the @eaDir folders on Synology NAS DSM

The @eaDir directories contain extended attributes and thumbnails that take up quite a bit of space, not unlike Windows Thumbs.db files.

Here’s how to get rid of them easily from the command line.

First, SSH into your Synology NAS box and log in as root, then type this to locate the @eaDir folders:

find . -name "@eaDir" -type d | more

If you’re happy you’re not going to accidentally delete something important, then make it happen:

find . -name "@eaDir" -type d -print0 | xargs -0 rm -rf

Note that after deleting the directories, you may also want to disable the services that created them.

Do you have any feedback on the above? Please leave it in the comments section below. Thank you!

, , , ,

4 Comments

Controlling the front LEDs of a Synology NAS via ttyS1

You can control the front LEDs (as well as triggering other hardware events) on a Synology NAS by sending certain values to /dev/ttyS1, either from a script of from the CLI via Telnet or SSH.

These commands “force” the LED state, and therefore the LEDs can’t be used as status indicators after being forced. You can, however, simply reboot the NAS to restore normal operation; the settings do not survive a reboot.

Below are a list of commands that can be run from the command line (if you are logged in as root) or incorportated into a script. Note that the # character and everything after it are comments, and some characters require escaping.

These are only the commands I could get to work on my NAS.

echo 1>/dev/ttyS1 # Immediate power off (not graceful)
echo 4>/dev/ttyS1 # Power LED on solid
echo 5>/dev/ttyS1 # Power LED flash
echo 6>/dev/ttyS1 # Power LED off
echo 7>/dev/ttyS1 # Status LED off
echo 8>/dev/ttyS1 # Status LED on solid green
echo A>/dev/ttyS1 # USBCopy LED flash
echo @>/dev/ttyS1 # USBCopy LED on solid
echo B>/dev/ttyS1 # USBCopy LED off
echo C>/dev/ttyS1 # Immediate reset (not graceful)
echo :>/dev/ttyS1 # Status LED on solid amber
echo ;>/dev/ttyS1 # Status LED flashing amber

If you know if any other values to send to ttyS1, or anything else you’d like to share regarding this, please feel free to do so in the comments below. Thank you!

, , ,

1 Comment

Simple Linux BASH script to locate and delete duplicate photos

This is a quick bash script I wrote to walk through directories and delete duplicate photos based on MD5.

It was written for a specific scenario and I highly advise against using it as-is. Instead, read through it and tweak it to your own situation. I would appreciate any feedback.

HF="$PWD/hashes.out"
echo HF=$HF
rm "$HF"
touch "$HF"
for x in `ls -bd *`; do
 echo x=$x
 cd $x
 for y in *; do
 m=`md5sum "$y" | awk -F '{print $1}'`
 echo $y: $m
 g=`grep $m "$HF"`
 echo g=$g
 if [[ "$g" != "" ]]; then
 echo "MATCH!!";
 echo rm "$y"
 else
 echo "no match"
 echo $m >> "$HF"
 fi
 done
 cd ..
done

, ,

1 Comment

Set up an encrypted VPN using DD-WRT

DD-WRT is feature-rich alternative firmware for a large number of home router models. It adds a wonderful array of new features, VPN being one of them. This walkthrough will show you how to quickly and easily configure a PPTP VPN server on your DD-WRT-powered router, so you can connect to your home network from afar, create a secure tunnel so you can safely use a public Wifi point with your laptop, or secure your iOS or Android device.

Setting up the VPN Server

So here’s how to get started. First, you’ll need a build of DD-WRT supported by your router which includes the VPN software. If you’re doing this on an Internet connection which has an IP address that changes periodically (i.e. residential), you’ll likely want a Free DynDNS hostname to point to your IP address. You’ll also need a basic familiarity of networking.

For the remainder of this guide, I will assume your router’s internal (LAN) IP address is 192.168.1.1.

Start by going to http://192.168.1.1 and login to your router’s administration panel.

Go to Services > VPN and set PPTP Server to enable. After doing that, a few new options will appear. The only ones you need to set are Server IP, Client IP(s), and CHAP Secrets. Set them as follows:

Server IP: You can set this to your router’s LAN IP, i.e. 192.168.1.1

Client IPs: Set this to an IP range OUTSIDE your DHCP range (See Setup > Basic Setup to figure your DHCP range) A good example value would be 192.168.1.200-250 for clients to receive addresses within that range.

CHAP Secrets: This is the username/password combinations for your VPN clients. Format is:
username*password*
Example:
myname * mypassword *

Neither the username nor password can contain spaces, and must be all-lowercase.

You’re done with this page; Click Apply Settings.

Now go to Security > VPN Passthrough and make sure PPTP is set to Enabled. Click Apply Settings if you had to change the setting.

You should now be able to connect to your VPN using your Windows, Mac, or Linux computer by setting up a PPTP connection to your public (WAN) IP or hostname.

Troubleshooting

Can’t get connected? First, try setting up your connection to the router itself, using the LAN IP (192.168.1.1). If that works, then the VPN server is set up correctly; the problem is likely on the WAN side. Keep reading for suggestions. If you weren’t able to get connected, go back to the top and double-check your settings.

iOS-Specific changes

You may need to make the following settings adjustment if you are having trouble connecting specifically from your iOS device running iOS 4.3 or above. Go to Administration > Commands and paste the following in the box. Click Save Startup.

#!/bin/sh
echo "nopcomp" >> /tmp/pptpd/options.pptpd
echo "noaccomp" >> /tmp/pptpd/options.pptpd
kill `ps | grep pptp | cut -d ' ' -f 1`
pptpd -c /tmp/pptpd/pptpd.conf -o /tmp/pptpd/options.pptpd

(Source: DD-WRT Wiki)

If you can connect from the LAN side, but are still having trouble connecting from the WAN side, it’s likely your ISP or your gateway device (modem) is blocking the needed GRE protocol or the needed PPTP port or traffic. Contact your ISP for further assistance.

Do you have any experience or tips to share regarding VPN connections to a DD-WRT-powered router, or any suggestions in addition to the above? Please feel free to share them in the comments below. Thank you!

, , , , , , , , , ,

Leave a comment

Bash script to automatically sort photos into folders based on EXIF data for Ubuntu Linux

If you — like me — take a lot of digital pictures, you probably have a hundred folders full of images on your hard drive or external drives, and not nearly as sorted as you would like them to be. you have probably gotten to the point that you don’t know what’s in them or can’t find an image when you’re looking. I had around 10,000 images in over a dozen folders spanning 5+ years, and I had no intention of even trying to sort them manually :) So I wrote this script.

The following script was written in bash on Ubuntu Linux and automatically sorts your images into directories based on the date and time the photo was taken. How does it do this? By making use of the EXIF data your digital camera stores inside the image. The date and time the photo was taken is stored in that EXIF data. When an image doesn’t have EXIF data (such as when it was downloaded from the Internet, or taken from a camera that doesn’t support adding EXIF data), it will use the files last-modified time.

First, this should be run in the top-most directory of wherever your pictures are stored. If you have pictures/foldername/somepics/ and pictures/anotherfolder/morepics, run it from your pictures/ directory.

There are quite a few opportunities to improve this script — and some cautionary notes as well — marked within the script with FIXME tags. I’m already finished sorting my images, but anyone is welcome to contribute suggestions and improvements, which I’ll look into incorporating the next time I’m using this. You are welcome to include any suggestions or code improvements in the comments below using <code> and/or <pre> tags.
Usage: Copy the script into a file, editing options where appropriate, and save it. Make it executable and run it from the command line or window, from the directory where your pictures are stored. No command-line arguments. Back up your stuff first :)

UPDATE: Because WordPress keeps mangling this code, it has been moved to github, here.

Questions, comments, or feedback can be left in the comments below, or please use the contact form. Thank you!

, , , ,

53 Comments

Basic Ubuntu VPS server backup via FTP or SSH SFTP

In my quest for the perfect “in my dreams” backup solution for my Ubuntu VPS, I created this very simple script which can be run as a cron job and can be easily modified to backup any amount of data to any remote FTP or SFTP server.

You could very easily include a database backup by running mysqldump beforehand, but I’m not including it in this script.

This required yafc to be installed, but Ubuntu installations can easily install it by running

sudo apt-get install yafc

And now, for the script:

#!/bin/bash
# format of the open command is proto://username:password@HOSTorIP/
# proto is either ftp or ssh
# special characters in the username or password are not well tolerated
# anything in the EOF tags are direct commands to yafc. Test if unsure
DIR=`date +%F`
yafc <<EOF
open ftp://username:password@ftp.example.com/
cd backup-dir
mkdir $DIR
cd $DIR
put -p -r *
close
exit
EOF

Enjoy! Questions, comments, and feedback are welcome and appreciated. Thank you!

, , , , , , , , ,

1 Comment