Thursday, March 20, 2014

Window 7 Desktop Picture via .bat script

I have a case when I wanted to set my desktop background at startup via a bat script.

put a wallpaper.jpg picture that you want on your desktop somewhere.

Create a [whatever].bat file in a location,

ECHO OFF

reg add "HKCU\Control Panel\Desktop" /v Wallpaper /f /t REG_SZ /d C:\path\to\wallpaper.jpg

RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True


You could run the .bat now if you want.
To have it apply each time you restart, put a shortcut to the .bat file C:\Users\[your account name]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Saturday, March 15, 2014

Installing pfsense on an HP T5720

Well after some time spent on on some unsuccessful projects, after getting inspired from this video

I decided that making my own router would be a good idea!

Some friends of mine had tried pfsense so I thought that I would give that a go.

I had some old hardware that I bough cheap a while ago when I wanted to do something, I just didn't know what. So I decided to use an old HP T5720 Thin Client that I had.

It seemed to be perfect for this setup, a small, silent, power efficient, powerful enough little server.

The HP 5720 has 512Mb of Flash HD, the one I purchased had 1Gb SODIMM RAM already and came with a PCI Riser card. So I put in a 2x 100/1000 PCI Ethernet Card that I had on a server I had and decided to give it a go.

Here is mine when set up:


Parts:
HP T5720 Thin Client with associated parts (power etc) and PCI Riser Card.
1x PCI Ethernet Card
Monitor during install.
Keyboard During install.
External CD drive during install.

I set the BIOS to boot of USB, hooked up my external CD Drive, hooked up the T5720 onboard Ethernet to my ADSL modem and booted off the pfsense Live CD.

I tried the default boot at first, but I kept having ACPI error messages scrolling past. So I rebooted and selected option 2 to start pfsense with ACPI disabled.

When it asked if I wanted to go to Installer or continue with Live CD, I selected Install.

This takes me to the ncurses install menus. I selected the default.

I selected Quick/Easy Install. 

I had some popoups about how it would erase everything etc. but it kicked off. During the install there were some errors, like this, 



but I skipped those and it seemed to install okay.

after install, I shutdown, set the bios to boot from Flash, disconnected the CD drive and started it up.

EDIT - After installing pfsense 2.1.2 on an external hard drive, I was getting booting errors.
I solved them by at the pfsense menu, hitting space, and then 7 to go into the loader prompt and inputting

set kern.cam.boot_delay="10000"
boot

I then once it booted okay, I installed nano as mentioned below, etc, but also added to /boot/loader.conf the line

kern.cam.boot_delay="10000"

in order to make it persistent.

END EDIT

I was then in the main pfsense command line menu.



I still had the ACPI errors scrolling past, so I needed to stop those on the default option at pfsense startup.

I needed to google how to turn it off, but once I found out how it was easy.

I went to the shell (option 8)
and installed nano using: 

pkg_add -r nano

EDIT
on pfsense 2.2.2 you use pkg to install other packages
pkg install nano

END EDIT

To default ACPI to off I modified /boot/device.hints file adding:

hint.acpi.0.disabled="1"

The machine I was on, was also on the ADSL LAN, and not hooked up to my pfsense box.
I could see from the pfsense menu what IP was assigned to my WAN interface so I tried to connect to that using my browser to use the web configuration.
That was denied and is the pfsense default not to allow connections to the webconfigurator over the WAN interface.
you could temporarily disable the packet filter by going to the shell and running: 

pfctl -d

Then you should be able to get to the web configuration over the WAN.

I just hooked my computer up to the pfsense box LAN interface and rebooted both my machine and the pfsense box.

in doing that, I think I bumped the  PCI card, as the pfsense box came up only recognizing the onboard interface.

I reseated and re-powered the pfsense box, and it came back up. But the LAN interface no longer seemed to be a DHCP server and I could't get in to the web GUI.

So how to start the DHCP server on the LAN interface?

Easy, on the console just assign it a static IP and it will ask you if you want to turn on DHCP for the interface.
Give it an IP range and you're back up and running. Your computer on the LAN interface will now be able to get an IP, get to the web configuration, and probably can already access the net if your ADSL is up and has assigned your WAN interface an IP.

So now I will be spending some time, configuring it with all the rules and options I want, maybe adding wireless, maybe mounting the T5720 onto something. Lots to play with now.

Monday, October 7, 2013

Picture of the Day - Part 3

Continuing on from Part 1 and Part 2.

I wasn't happy with the results of the Automator workflow.
It was nice to have a play around and see what it could do, but it seemed that I was wanting to do some more advanced functions, like text append, that just weren't available or easy in Automator.

So I used my Bash scripting power to come up with another approach that gave me the flexibility that I needed.

 
#!/bin/bash
#build up the url
MYURL="http://en.wikipedia.org/wiki/Template:POTD/";
DATE=`date +%Y-%m-%d`
MYURL=$MYURL$DATE

temp=$(/opt/local/bin/lynx -dump -nonumbers -listonly -image_links $MYURL | grep en\. | grep -E "(.jpg|.JPG)" | grep -v thumb)

temp2=$(/opt/local/bin/lynx -dump -nonumbers -listonly -image_links $temp | grep upload | grep -E "(.jpg|.JPG)" | grep -v thumb | uniq)

#get the image
/opt/local/bin/wget --no-check-certificate -O /Users/jono/Documents/Backgrounds/1.jpg $temp2

To break it down,
First I would build up the URL in the form that I needed.

Then I would use the command line browser lynx to grab the image links out of that URL. I would filter the results on some text like 'en.' (to make sure I hit en.wikipedia.org) and '.jpg' (to make sure I just got jpg results) but I didn't want any URLs returned with the text 'thumb' - as they were thumbnails, so I used the grep filter 'grep -v thumb' to NOT include those results.

So I should now have the URL of the wikipedia file document in the form
http://en.wikipedia.org/wiki/File:xxxxxxxxxx.jpg

That file document, again had just a larger version of the thumbnail on that page, but it linked to the original higher resolution image.

So I ran lynx again, applied pretty much the same filters, and made sure that I only had unique results (the pipe to uniq command)

I now have the link to the full resolution uploaded file and I use the command wget to save it straight to the location I wanted named as the filename I wanted (1.jpg)

No need to save it and then move and rename, like I tried to do in Automator.

Now my geektool geeklet that is looking in that location every 5 secs or so kicks in and changes the desktop background to the newly download picture.

Now you can just set that up as a login script and you can have dynamic fresh Picture of the Day each time you log in.

Sunday, October 6, 2013

Picture of the day - Part 2

Picking up where Part 1 left off, I tried to do a better way to create a picture of the day for Geektool to use.

 As Wikipedia picture of the day was in the standard format

http://en.wikipedia.org/wiki/Template:POTD/yyyy-mm-dd 

I decided to just grab what I needed.
At first I researched using variables in Automator, but it seem to handle appending text,all the forums had people creating Applescripts to build up dynamic text.

So I whipped up the applescript:

 set myURL to "http://en.wikipedia.org/wiki/Template:POTD/"
 
 set myshort to short date string of (current date)
 set myday to items 1 through 2 of myshort
 set mymonth to items 4 through 5 of myshort
 set myyear to items 7 through 10 of myshort
 
 set myURL to myURL & myyear & "-"
 set myURL to myURL & mymonth & "-"
 set myURL to myURL & myday
 
 return myURL

in order to get constancy on the date I had to force the short dat to be 2 digits\2digits\4 digits
You can do this in System Preferences > Language and Text > Formats > Customize
that gave the short date the format 01/01/1970 where I could use the "to items 1 through 2" to pick it apart so I could use it as I wanted.

So I came up with what I hoped was a working workflow:

Except it didn't work.
It dumped the files to folder abc123 okay, again it had the extra images I didn't need as well as the POTD in .jpg format.
Putting in a 'view results' into each step of the workflow, I could see what was going on.
It seems that my 'Filter Finder Items' wasn't working as I expected. It didn't return anything to be renamed, and then moved. Maybe I wasn't using it properly and selecting Desktop > abc123 properly.
Maybe I could work it out, but I still wasn't happy that the Save Images was only getting the thumbnail of the Picture of the Day.

How I solved it in a way I was happy with will be part 3.

Picture of the day - Part 1

Okay, so I use the great app Geektool to change/show some desktop pictures.

I have Geektool configured to look in my backgrounds folder for a file 1.jpg and display it as my background picture. I thought it would be cool if that picture changed daily.

So what site has a great looking picture of the day? Wikipedia of course.

So my idea was to use Mac OS X Automator to follow this workflow:

Use the main wikipedia URL
Get the links from the page
Filter them down as the POTD always contained http://en.wikipedia.org/wiki/Template:POTD/ then todays date
Get the contents of the page
Save images

So I created this workflow, which worked okay.


That dumped the files to the folder abc123 on my desktop but it wasn't efficient. It would grab all the other images like the wikipedia logo as well as the picture of the day as small thumbnails.
I tried to use automators various other finder functions to move and rename, but I didn't have much luck.

I thought that I could do better by getting only the exact image I wanted at the size I wanted, so see what else I tried in Part 2.


Sunday, August 18, 2013

Conditional Statement in Bash on OS X

Ok,
I had a bit of trouble getting a conditional statement to evaluate on my bash script in OS X.
I was trying to perform an action if I was online or not.

A good way to test if you are online - ping google of course!

For testing purposes I just got the computer to use text to speech to let me know the result.
Here is my working example, spaces are important!

#!/bin/bash

FLAG=0

ping -q -c 1 google.com && FLAG=1
if [ $FLAG = "1" ] ; then
say $FLAG
else 
say $FLAG
fi

exit

If you can ping google,the computer will speak "one"

If you you want to test, change google.com to something you cant resolve such as googlekdfsjbg.com and run it again.

You shouldn't be able to ping googlekdfsjbg.com so the computer will speak "zero"

Of course this is a simple example and you need to tailor to your situation, just because you can't ping google doesn't mean you are not online. For example pings could be blocked on your network, however this suits my situation.

enjoy!

Thursday, August 1, 2013

Raspberry Pi Temperature Sensing.

Thanks to Adafruits Raspberry Pi tutorials on temperature sensing,
and this blog on Raspberry Pi temperature sensing to Sen.se.

I decided to do my own.
I also wanted to store the results locally, so I also saved it to a file.

I used a DS18B20 1-wire temperature sensor
Adafruit T-Cobbler
Breadboard and wires.

I wired it up just like they did in their diagrams.



Add one wire support.
In /boot/config.txt add
dtoverlay=w1-gpio
I subscribed to Sen.se and in your profile you can find your API key.

After checking out their tutorials I then wrote a bash script to grab the the temperature, then use curl and JSON to upload it.

Here is the script replace SERIALNUMBERHERE with the serial number of your sensor.
API KEY HERE with your API key, and SENSEFEEDID with your Sen.se feed id.

#!/bin/bash

sudo modprobe w1-gpio

sudo modprobe w1-therm

cd /sys/bus/w1/devices/SERIALNUMBERHERE/


URL="http://api.sen.se/events/?sense_key=API KEY HERE"


DATE=$(date)

NOW=$(date +"%d_%m_%Y")


DATA=$(cat w1_slave)


DATA1=${DATA:69:2}

DATA2=${DATA:71}

DATA3=$DATA1.$DATA2


OUTPUT="$DATE , $DATA1.$DATA2"


echo $OUTPUT >> /root/output_$NOW.csv


curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"feed_id": SENSEFEEDID,"value": '$DATA3'}' $URL



I then use Cron to schedule a reading every few minutes.

Then back in Sen.se you can graph it or manipulate it like you want.