Update Wallpaper from Bing (OSX)

I’m not a huge fan of Bing search engine, I’ve tried to use it but I don’t like the format of the search results and I don’t think it’s particularly good at finding relevant results either.

I do like Bing wallpapers, and I use Bing Desktop on my Windows laptop to update my desktop to Bings daily wallpaper.

Now that I’ve moved to a Mac I still want to get the picture, but the application is Windows only - so the script below will do the job for you. I’ve set it to download to the users picture folder ~/Pictures/bing-wallpapers just using the current date for the filename.

import urllib2
import json
from os.path import expanduser

response = urllib2.urlopen("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")
obj = json.load(response)

url = (obj['images'][0]['urlbase'])
name = (obj['images'][0]['fullstartdate'])
url = 'http://www.bing.com' + url + '\_1920x1080.jpg'
home = expanduser('~')
path = home +'/Pictures/bing-wallpapers/'+name+'.jpg'
print ("Downloading %s to %s" % (url, path))
f = open(path, 'w')
pic = urllib2.urlopen(url)

To run on a schedule, set up a cron job to run the script at 10am using crontab -e and add the line

0 10 \* \* \* python ~/Pictures/wallpaper.py

Creating environment variables from the command line (Windows)

I know that it is incredibly lazy and a non problem but I find it quite tedious in Windows 8 to go digging for the system environment variable GUI whenever I need to add or update something.

Generally I’m already in the command prompt so I was keen to find a way to create them from there without having to go into search for it each time.

Since Windows XP, setx has been available as an extra download, and more recently it’s included in Windows out of the box - this is the command that I wanted.

To create a persistent STORM_HOME environment variable, use the following command. The /M sets it as a system variable rather than the default user variable.

setx /M STORM_HOME d:\storm-latest

There are a number of other options, do setx /? to see them.

SFTP Connection Closed - password expired?

I’ve just had an interesting problem with an SFTP account that suddenly stopped working from a cron job. When the account was used directly from the bash prompt the response was simply Connection closed immediately.

Every thing was set up correctly as far as authorized_keys and /etc/ssh/sshd_config looked fine but the account wouldn’t connect.

As I don’t have the private key for the user that was connecting to the server, I created a new key pair and added the public key to the authorized_keys file in the users .ssh folder. Using the following command I got a response that was no more helpful;

psftp logdrop@ -i logdrop_pk.ppk

This gave the response;

FATAL: Recieved unexpected end-of-file from SFTP Server

After a couple more checks on the server and no success I decided to try

putty logdrop@ -i logdrop_pk.ppk

This opened putty which connected but reported that the password for the user had expired and needed a new one. From here I was able to reset the password then go on the server and set

passwd -x 0 logdrop

to ensure that the aging was deactivated.

So an interesting issue for a Friday afternoon.

Cross Join Columns in Excel

As part of a project I’m working on I need to create a cross join of users against symbols; the idea being that any of the given users might look at any of the given symbols. I have a list of users and a list of symbols which I essentially need a cross-join on.

For this example I’ll use cars and engine/transmission variants.


From these two lists I need to quickly create all possible combinations;

cross-joined lists

I can’t find any function that will do what I was so I’ve created a VBA function that will do it;

Function CrossJoin(r1 As Range, r2 As Range) As Variant
    Dim size As Integer
    Dim arr As Variant
    Dim offset As Integer
    size = r1.Rows.Count * r2.Rows.Count
    ReDim arr(0 To size, 0 To 1)
    offset = 0

    For i = 1 To r1.Rows.Count
        For j = 1 To r2.Rows.Count
            arr(offset, 0) = r1.Cells(i, 1).Value2
            arr(offset, 1) = r2.Cells(j, 1).Value2
            offset = offset + 1
        Next j
    Next i

    CrossJoin = arr
End Function

To use the function select and empty cell and insert =CROSSJOIN(A1:A4, B1:B4) (adjusting the input range as required) then when the function has evaluated extend the range to the size of the expected cross join. (in this case 16x2).

To calculate the array function hit F2 then press Ctrl+Shift+Enter to calculate the array.

Using yum history

I have been setting up a couple of Red Hat boxes and I need to have a cron task to mount a network share from an AD domain, copy the files then dismount.

In the past I have successfully accessed the network shares using a given set of credentials but I could not get it to work after install samba-client and samba-common.

My script on the new machine was the same as the script on the working machine so there had to be a discrepency on installed packages, step in yum history.

To get the history of yum commands use

yum history

which will give the output similar to;

yum history output

Thankfully, from the dates I was able to work out the point that I had done the install. From this point you can look at a specific item, for example;

yum history info 57

which will return more information about the command

yum history info for id 57