Archive for the ‘Bash’ Category

We all like quests… right?? Well, mine has to do with getting the most out of my battery on my aging laptop. Aging that includes the power-providing battery. I already have a head start on this with my laptop specs. It is a Dell Inspirion 700m. It is also based on the Centrino platform, which was Intel’s first major play into laptop powersaving. Included within are an arsenal of powerful components that use minimal power (at the time).

My specific laptop was based off of the Carmel platform. It includes an Intel Pentium M 1.6GHz processor with variable speeds ranging from 600MHz to 1.6GHz, the Intel 2200BG wireless networking card, and the (unfortunately) low-powered 855 Chipset which includes Intel Extreme Graphics 2. Also included is a 12.1″ widescreen LCD, which I am sure uses less power than some of the larger screens out there.

I already had low power-consumption in mind when I installed Slackware (started with 10.2). I installed cpufreq-utils and cpufreqd which allowed me to adjust my processor speed depending on certain power conditions. I also purchased a larger battery (on this model the battery is in the back, so the larger one extends further out the back). The original was a 4-Cell that provides 2200mAh. I replaced that with an 8-cell that provides 4400mAh of power. When the battery was new it provided a whooping 5 hours of battery life. Sadly batteries will dwindle and the power it is capable of putting out will shorten over time.

So to get to the point of my quest, my goal was to get the maximum life out of my laptop. The reason was that I was getting ready to go on leave. I currently am stationed at Ramstein Air Base in Germany, and flight times back to the states are kinda long. So I wanted to be able to use my laptop for as long as possible while I was flying (partially due to the fact that I can’t sleep on planes, even when assisted with sleep medication). So with the help of Google, I was able to come up with the following.

First things first, I found I needed to re-compile my kernel. This will be distro-specific, due to different distros compiling kernels different ways. Mine will all be based off of Slack 12.1 which is the version I was running at the time. The biggest reason for me, was to included tickless-support, which basically only wakes up the processor when there is something for it to do.

Next was to get a program called PowerTOP (PowerTOP needs certain things built into the kernel, the items needed are listed on their site). It is developed by Intel and is used to show what is causing your processor to wake from its low power state. The other benefit it gives is it will tell you some information on how to use less power, such as disabling Bluetooth, or setting a lower scan rate for your wireless. It provides hotkeys so you can change these directly in the program without exiting. It also provides the actual commands that it uses to change these. This way you can make them into a script (like I did).

Some of the functionality it wants to change may not be beneficial for everyday use of a laptop (like disabling Bluetooth). So if you want some of these to run everytime you run your laptop you could add them to your system startup, which in Slackware is under /etc/rc.d/rc.local (distros vary in their startup procedures, so yours may be different). For other times (like when on a plane and you don’t want any wireless devices running) you can make a specific script for it that you run when needed.

Another tool worth mentioning is laptop-tools. This is a script that will run in the background and whenever the laptop starts using the battery, it automatically will provide powersavings. There is a detailed article about what it does here.

Now come the common sense items. People should realize these, but lets face it… everyone has brain-farts.

  • Dim your laptop screen to the lowest level still viewable. The higher the brightness of your LCD screen, the more power your backlight will consume.
  • Don’t use your CD/DVD drive. It uses a lot of power to spin a disc. Their are plenty of tutorials to convert your dvd’s to a file on your hard drive.
  • Don’t use external devices. Even an external mouse or usb drive will use additional power.
  • Depending on your distro you may want to consider using a different window manager. Some window managers have a lot of programs running in the background that will take up processing power and in turn use more of your battery. While I normally run KDE 4, I use windowmaker when I am trying to save power.
  • Disable 3D acceleration. Again, this will use unnecessary processing power. You can do this in your /etc/X11/xorg.conf file by commenting out the Load dri and glx modules. Just put a # in front of them.
  • Disable any unused services. If you won’t be using your webserver or database software, there is no reason to be running those in the background. You can add any of these to your powersave script.
  • Disable any unused hardware. You can do this by removing the modules. If you won’t be using usb, then remove it. Some of the devices you may want to consider disabling are: firewire, pcmcia slot, bluetooth, wireless, card readers, and more.
  • The more obvious one is, if possible, buy a larger battery. This will be dependent on the laptop, because some laptops do not have any larger batteries available. Worst case, buy a new one to replace your aging battery.

Unfortunately I don’t have an amount of extra life this gave me, but I did lower my wake-ups from idle per second by more than 400 wake-ups. And this is just when I am using KDE4. With switching to windowmaker I was able to lower the total amount to 40-60 wake-ups per second. Granted, when I am running my video player that number does go up, but the lower the number of wake-ups the less power your processor will consume.

A while ago, I was working on creating a script to create thumbnails of all the pictures I had taken so I could put them up on my personal site for family/friends to view. It started out as a very basic script but as I developed my website, I found new ways to display the photos. Namely using css for the image to come up double sized by mousing over on the picture. Well this created quite a challenge to put all the code for my pictures in the HTML. So I went a step further and adjusted the script to create the php necessary for the galleries. The way I create all my pages is to use a standard header and footer file and include them into my individual pages. So I then changed the script so it created a full blown page ready to be used. The script is below. You may have to modify it slightly for your own needs, but it should work fine. The main requirement is the imagemagick software. In Slackware it is included with the default install. With your own distro you may need to add it yourself.

[code='bash']
#!/bin/bash

echo "Resizing Pictures and Creating PHP"
mkdir thumbs previews view
cp ../rhine-river/view.php .
cp ../rhine-river/download.php .

totalcount=0
currentcount=0

if [ $1 = y ]
then
name=$2
picnum=0
for i in *.jpg *.JPG; do

picnum=$(($picnum + 1))

pic=`echo $i | awk -F . '{print $1}'`
rename="${name}-`printf "%03d" $picnum`.jpg"
mv "$i" $rename

done
fi

for i in *.jpg; do

totalcount=$(($totalcount + 1))

done

echo "There are $totalcount pictures to convert."

# Create the header
echo \<\?php > index.php
echo \$pagetitle \= \"Insert Title Here\"\; >> index.php
echo include_once\(\"../../includes/header.php\"\)\; >> index.php
echo ?\> >> index.php
echo \
Page Description Here\ >> index.php
echo \

I recently read a blog post on the How-To Geek about setting up OpenDNS and the options it gives you to secure your internet browsing. I have found I love the OpenDNS service and the filtering control it gives me. But I tend to try and fight the system. Rather, I try to figure out ways to bypass the filtering service. This way I can find out ways that others would be able to get around services I set up.

Well OpenDNS is fairly straightforward. You add their DNS Server IP’s to your static IP’s in your router, then any computer behind that router will automatically use those DNS servers. So what if you were to change the DNS servers on your local machine? It would bypass your router for those queries and in turn bypass OpenDNS. This doesn’t cut it for me. It is way to easy to change that, and enough people know how to change their IP settings that this is too unsecure for me. So I did some research online and found an easy way to bypass that. Block port 53 on your router.  Some routers may not have this functionality by default, so I can highly recommend checking out if your router is supported by DD-WRT. They support quite a few routers, and I personally love their firmware.

Since I am running DD-WRT that is what this guide will be based on, but it should apply for any router based on linux that you can have it run your own code.

Once you login into your router, go to Administration -> Commands.
From here you just need to paste this in:

[code]iptables -I FORWARD 1 -p tcp --dport 53 -j DROP; iptables -I FORWARD 2 -p udp --dport 53 -j DROP[/code]

Then at the bottom hit the Run Command button. This should block any attempts for the computer to go to another server to resolve names.

This should apply to any router that uses iptables and allows you to run your own code. This is also able to be done through ssh/telnet if your router supports it.

I recently read a blog post on the How-To Geek about setting up OpenDNS and the options it gives you to secure your internet browsing. They have quite a few filtering options including Phishing sites, along with blocking adult-related sites, and about 50 other categories along with a fully redundant DNS nameserver resolving. I decided to try it and set it up on my home network. The problem is that if you have an internet provider that provides you with a dynamic IP (IP address changes occasionally, if you aren’t sure what you have you probably have a dynamic address), you need to update the IP with OpenDNS. They have a lot of clients out there to do it, but as far as I found there were no linux clients. So I created a short linux script to do just that.

[lang='bash']#!/bin/bash

# Copyright (C) 2006 Jeremy Brent Hansen
#
# These are for your OpenDNS username and password. At this time, I do
# not know how to hide this info, so you will need to make sure you have the
# correct file permissions.
username=your_opendns_username
passwd=your_opendns_password

# This is where the log file will be stored. Currently it only logs the current IP
# and the response back from OpenDNS. The log will keep one backup. I
# just used a folder in my home directory (make sure the folder exists).
log_dir=~/.opendns

# Revolves the log file. Keeps one backup.
mv $log_dir/log $log_dir/log.1

while [ 1 ]
do

date >> $log_dir/log
/usr/bin/curl -i -m 60 -k -u $username:$passwd ‘https://updates.opendns.com/account/ddns.php?’ -silent >> $log_dir/log
echo -e “\n” >> $log_dir/log

# Resends the info after 5 minutes. Eventually I plan on changing it,

# so it only updates when your IP changes.
sleep 600

done[/lang]

So, there you have it. No root permissions are required, so I just have it in my .profile for my normal user. Just run it with the & at the end, so it will background the process.