Tag Archives | BASH

BASH, cURL script to retrieve Google Analytics data

Last week I wrote this PowerShell code to retrieve Google Analytics data. Below is similar code in BASH using the cURL command.

#!/bin/bash
stty -echo
read -p "Password: " password; echo
stty echo
RESULT=$(curl -s https://www.google.com/accounts/ClientLogin \
--data-urlencode Email=email@company.com --data-urlencode Passwd=$password \
-d accountType=GOOGLE \
-d source=YourSource \
-d service=analytics)


AUTH=$(echo "$RESULT" | grep 'Auth=' | sed  s/Auth=//)

curl -s "https://www.google.com/analytics/feeds/data?ids=ga%3AXXXXXXXX&metrics=ga%3Avisits&start-date=2012-05-01&end-date=2012-05-31&max-results=50" \
--request GET --header "Authorization: GoogleLogin auth=$AUTH"

#Done

Update DynDNS from the command line

My router is not updating DynDNS correctly, so I wanted to use a simple cron job to do the same. Here is a simple BASH/Shell script to update DynDNS:

IP=$(curl -s icanhazip.com)
curl -v -k -u username:password “https://members.dyndns.org/nic/update?hostname=DNSHOSTNAME.dyndns.org&myip=$IP”

Using cURL to pull Google Reader starred items – Part 1 xpath

A while ago, I wrote a post about using ruby to parse the xml feed of shared starred items in Google Reader. One thing that I did not like about this solution was that I could not get the URL AND mark the item as un-starred. Since I had been playing with REST in these two prior posts, I figured I could re-write my code to pull down starred item’s URLs, and mark them as un-starred. I wanted to eliminate ruby, as I just don’t use it that often and I feel like I am re-inventing the wheel every time. This too a lot longer than I thought it would, but I figured it out (I think)

As this article explains, you can share out your Google reader starred items. First step was the find the needed values from this XML feed and put the values into variables. I turned to xpath (xpath is installed by default on OS X, it is part of the Perl library XML::XPath).

For the second part of this tutorial, where we mark the item as “un-starred”, we need 2 variables form the Xml feed: id & source. My end goal was to put these URLs into Together.app, so I needed the URL too. Title was just for fun.

Here is the non ruby code to pull the variables that I needed out of the XML of the shared starred items in Google Reader.

XML=$(curl -s http://www.google.com/reader/public/atom/user/YOURUSERID/state/com.google/starred?n=1 | xmllint --format -)
OBJID=$(echo "$XML" | xpath "//entry/id" 2>/dev/null | awk -F"[<>]" '{print $3}')
TITLE=$(echo "$XML" | xpath "//entry/title" 2>/dev/null | awk -F"[<>]" '{print $3}')
URL=$(echo "$XML" | xpath "//entry/link/@href" 2>/dev/null | sed 's/\"//g' | sed 's/href\=//g' | sed 's/\ //g')
SOURCE=$(echo "$XML" | xpath "//entry/source/@gr:stream-id" 2>/dev/null | sed "s/gr:stream-id=//g" | sed "s/\"//g" | sed 's/\ //g')

After getting those variables, I wanted to put the URL in together.app (this has not changed since the previous post). Here is that code:

echo "Adding $TITLE to Together"
osascript << EOT
tell application "Together" to import url "$URL" as web PDF
EOT

BASH script to email if WordPress plugins or themes need updating

This one took me all day. But I got it. I wanted to have a script that could look through all WordPress sites and find if there are outdated themes or plugins. All I need to do is slap it into a cron job and I am good to go!

Here is the BASH code to do it.

 

UPDATESNEEDED=""

for installpath in $(find /var/www -name wp-config.php)
do
cd $(dirname $installpath)

THEMENEEDED=$(php -r 'require_once("./wp-load.php");
delete_site_transient("update_themes");
wp_update_themes();
$new = get_site_transient("update_themes");
echo count($new->response);')

if [ ! $THEMENEEDED = 0 ]; then
UPDATESNEEDED="$UPDATESNEEDED \n Site $installpath needs $THEMENEEDED theme(s) updated"
fi

PLUGSNEEDED=$(php -r 'require_once("./wp-load.php");
delete_site_transient("update_plugins");
wp_update_plugins();
$new = get_site_transient("update_plugins");
echo count($new->response);')

if [ ! $PLUGSNEEDED = 0 ]; then
UPDATESNEEDED="$UPDATESNEEDED \n Site $installpath needs $PLUGSNEEDED plugin(s) updated"
fi
done

if [ -n "$UPDATESNEEDED" ]; then
echo -e "$UPDATESNEEDED" | mail -s "Updates are needed" email@company.com
fi

I have been needing this script for a while.

OS X: Running a script when a USB drive is inserted

I rsync all my data to a USB drive that I keep at work. I wanted a way to have my rsync script automatically run when I plugged in the drive – kinda like Time Machine.

It ended up being pretty simple. All I needed to do is create and AppleScript and attach it to a “Folder Action” for the /Volumes folder. This script below is launched when a new item is added to the /Volumes folder, i.e. when you insert a new volume. This script will try to run a BASH script if it exists on that volume (.OnInsert)

on adding folder items to this_folder after receiving these_items
	repeat with current_item in these_items
		try
			do shell script POSIX path of current_item & ".OnInsert"
		end try
	end repeat
end adding folder items to

Save this as a .scpt file and put it in ~/Library/Workflows/Applications/Folder\ Actions folder.
Next, right click the /Volumes folder and select Services -> Folder Action Setup and attach the script you just created
AutoMagic!

Running a BASH script when my Laptop is opened at home – Part 2

As I mentioned in this post I should be able to run a script to find my current gateway’s MAC address. With this info, I should be able to tell when I am home, and launch a script. In this post, I talk about sleepwatcher, which runs a script when the machine is woken up. Sleepwatcher looks for a file named .wakerc and executes it when the machine wakes. If I put the following in my .wakerc, it will execute whatever I like (for example a rsync script) when the machine is woken up and at home:

 

#!/bin/bash
sleep 120
GATEWAY=`/usr/sbin/netstat -rn | /usr/bin/grep default | /usr/bin/grep en1 | /usr/bin/cut -c20-35`
MACADDRESS=`/usr/sbin/arp -n $GATEWAY | /usr/bin/cut -f4 -d' '`
if [ "$MACADDRESS" = "xx:xx:xx:xx:xx:xx" ]; then
	/bin/echo "$(date): I am at home now: $MACADDRESS" >> ~/Desktop/wake.txt
	# script I want to run at home is next line
	rsync Documents/ server:Documents/
else
	/bin/echo "$(date): I don't know where I am: $GATEWAY $MACADDRESS" >> ~/Desktop/wake.txt
fi

Powershell script to install Cygwin

I like having Cygwin installed on my machine, and since I always re-image, I needed a script to install Cygwin automatically.

function Install-Cygwin {
   param ( $TempCygDir="$env:temp\cygInstall" )
   if(!(Test-Path -Path $TempCygDir -PathType Container))
    {
       $null = New-Item -Type Directory -Path $TempCygDir -Force
    }
   $client = new-object System.Net.WebClient
   $client.DownloadFile("http://cygwin.com/setup.exe", "$TempCygDir\setup.exe" )
   Start-Process -wait -FilePath "$TempCygDir\setup.exe" -ArgumentList "-q -n -l $TempCygDir -s http://mirror.nyi.net/cygwin/ -R c:\Cygwin"
   Start-Process -wait -FilePath "$TempCygDir\setup.exe" -ArgumentList "-q -n -l $TempCygDir -s http://mirror.nyi.net/cygwin/ -R c:\Cygwin -P openssh"
}

This will download and install Cygwin and install the openssh package.

Running a BASH script when my Laptop is opened at home – Part 1

My laptop hard drive died. I was upset only because there was data on my laptop that had not been moved over to my desktop. My desktop has time machine and is rsynced to several other drives and locations.

So I lost data. My own fault because I was not diligent in moving data off my laptop.

To make sure this did not happen again, I needed the ability to run a script when I open my laptop at home. But how?

  • First, I thought about identifying being at home by my wireless SSID. That was okay, but what if I was connected by wire?
  • Second, I thought about identifying home based on my subnet. Well, I could find myself on a network with the same range, and that could be bad.
  • Then I found this link that showed some code on how to get the MAC address of the defined Default Gateway. Perfect.

MAC addresses should be unique. Therefore the BASH script should only run when I am on my home network.

Here is the BASH script to find the Default Gateway’s MAC address

GATEWAY=`netstat -rn | grep default | cut -c20-35`
MACADDRESS=`arp -n $GATEWAY | cut -f4 -d' '`

Next step is to use this code when I open my Laptop lid.

Comparing master and slave MySQL WordPress DBs

My VPS host – Rackspace contacted me the other day to tell me that my host server became unresponsive. After it came back online I went to my MySQL replicated slave to check the status. The /var/log/mysqld.log said:

Error reading packet from server: Client requested master to start replication from impossible position

This article siad

“9 times out of 10 it’s because the master crashed and when it came back online a new binlog file was made”.

That sounds about right, my virtual host was probably force rebooted, and my VM crashed. I recovered my DBs by re-mirroring my DBs over an AutoSSH connection between my two servers.

The article goes on to say:

Now if your super sensitive of lost events because a row or two could of been lost from this replication event, do some spot testing for tables written to often”

I wanted to verify my DBs were the same on both master and slave, so I wrote this BASH function:

function CompareDBs {
if [ $1 ]; then
DB=$1;
RDBDUMP=$(mysqldump -h <a href="tel:127.0.0.1">127.0.0.1</a> -P 7777 --order-by-primary --skip-extended-insert --skip-opt --skip-comments $DB)
LDBDUMP=$(mysqldump --order-by-primary --skip-extended-insert --skip-opt --skip-comments $DB)
diff -y --suppress-common-lines &lt;(echo &quot;$RDBDUMP&quot;) &lt;(echo &quot;$LDBDUMP&quot;)
fi
}

Call the function with the DB you want to compare. This script assumes that you have an SSH Tunnel between your two servers.

 

********* MAKE SURE YOUR TIME IS RIGHT ON BOTH SERVERS*********
This will save you some hair pulling!

Powered by WordPress. Designed by WooThemes