| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Monday, September 28, 2009

Making a Bash Script into a Web App

I recently wrote a nice little bash script at work that would check to see if a specified bit of information had been properly propagated through several tracking systems. It reduced about 10 minutes of work to about 30 seconds and I was thrilled to have extra time to sip my coffee. When I showed it off to my boss, he said, "Cool, but can you make it a web app?"

After a bit of digging, I figured it out and it's remarkably simple. Here's what you'll need:
  1. A web server capable of running PHP. I'm rocking XAMPP.
  2. A functioning bash script. Just make sure it's working when you run it from the command line. The output you see there is what you'll see when it's been web-app'd.
For the purposes of this article, I'll post a simple bit of PHP that will produce a web app version of ping. This code can be easily modified to display the output of any Bash command on a web page.
<html>
<head>
<title>Web Ping</title>
<style type='text/css'>
pre {
background:black;
border:1px lime solid;
color:lime;
}
td {
font-family:monospace;
}
</style>
</head>
<body>
<?php

# clean the strings to prevent injection attacks
$BADCHARS='/[^\.\w]/';
$HOST=preg_replace($BADCHARS, '', $_GET['host']);
$C=preg_replace($BADCHARS, '', $_GET['c']);
$I=preg_replace($BADCHARS, '', $_GET['i']);

if ($I=="") { $I=1; } # assign default values
if ($C=="") { $C=4; } # assign default values

if ($HOST!="") { # this is where the magic happens
echo '<pre>';
$last_line = system('/bin/ping '.$HOST.' -c '.$C.' -i '.$I, $retval);
echo '</pre>';
}

?>
<form action='ping.php' method='get'>
<table>
<tr>
<td>Hostname/IP:</td><td><input type='text' name='host' value='<?php echo $HOST ?>'/></td>
</tr>
<tr>
<td>count:</td><td><input type='text' name='c' value='<?php echo $C ?>' /></td>
</tr>
<tr>
<td>interval:</td><td><input type='text' name='i' value='<?php echo $I ?>' /></td>
</tr>
<tr>
<td colspan='2'>
<input type='submit' value='Ping It' /><br><br><br>
</td>
</tr>
</table>
</form>
</body>
</html>


Enjoy!

Thursday, September 17, 2009

Minimalist OCD? EeePC Desktop

My constant challenge to myself is to find elegant ways to pack a ton of information into the tiny, tiny 800 x 480 resolution of my EeePC 701. I think this setup accomplishes that quite nicely.As you can see, it's pretty simple. The icon in the top-middle of the screen is trayer, my system tray replacement as I got rid of the toolbar entirely. Instead, I use Gnome-Do to do application launching and general system management.As for the dashboard and calendar, it's all built on Conky. The configs are below:

Dashboard:
# .conkyrc
own_window yes
own_window_title econky
update_interval 1.0
short_units yes
double_buffer yes
own_window_transparent yes
border_width 0
use_xft yes
minimum_size 400
alignment tm
uppercase no
draw_shades no
pad_percents 0

TEXT
${color #D0D0D0}${font Ger4ronL Cond:size=58}${time %R}${color #3B3B3B}${font Ger4ronL Cond:size=24}
${voffset -75}${alignr}${time %A}
${alignr}${time %e %B}${font Envy Code R:size=8}
${hr}
wlan0 - ${addr wlan0} - ${upspeed wlan0}k / ${downspeed wlan0}k${alignr}${wireless_essid wlan0} ${wireless_link_bar 5, 100 wlan0}
cpu - ${freq}MHz ${cpubar 5, 100}${alignr}ram - ${memmax} ${membar 5, 100}
sda - ${fs_free /} ${fs_bar 5, 100 /}${alignr}bat - ${battery_percent BAT0}% ${battery_bar 5, 100 BAT0}

Calendar:
# .conkyrc
own_window yes
own_window_title econky
update_interval 1.0
short_units yes
double_buffer yes
own_window_transparent yes
border_width 0
use_xft yes
alignment br
uppercase no
draw_shades no
pad_percents 0
text_buffer_size 2560

TEXT
${font Envy Code R:size=8}${execp ~/Documents/scripts/calStrip.sh '#3B3B3B' '#D0D0D0' vertical}
Calendar script:
#!/bin/bash
color='${color '$1'}'
highlight='${color '$2'}'

day=$(date +%d)
month=$(date +%m)
year=$(date +%Y)

maxdays=$((
(
$(date -u -d "${year}-${month}-01 +1 month" +%s)
-
$(date -u -d "${year}-${month}-01" +%s)
)
/
86400
))

i=0
daystring=$color
daynames=$color
vstring=$color

while [ $i -lt $maxdays ]; do
let i=$i+1
d=$(printf "%02d" $i)
dayname=$(date -u -d "${year}-${month}-${d}" +%a)
if [ $3 = "horizontal" ]
then
if [ $d = $day ]
then daystring=$daystring" "$highlight$d$color #'\n'
daynames=$daynames" "$highlight${dayname:0:2}$color
else
daynames=$daynames" "${dayname:0:2}
daystring=$daystring" "$d
fi
else
if [ $d = $day ]
then vstring=$vstring'\n>'$highlight${dayname:0:2}" "$d$color"<"
else
vstring=$vstring'\n '${dayname:0:2}" "$d" "
fi
fi
done

hstring=$daynames'\n'$daystring

if [ $3 = "horizontal" ]
then echo -e $hstring
else
echo -e $vstring
fi


Source Image

Friday, September 4, 2009

formWorks 0.6a - New Features!

Thanks in large part to troubleshooting help from Peter Hahn, I was able to clean up the original formWorks code and patch a good-sized bug.

Thanks Peter!

I've also been able to add a couple new features! With formWorks code, you can now:
  1. You can make two columns on the form pages to make better use of the empty/white space.
  2. Create custom administrator interfaces.
So what the heck does making two columns do for you? Have a look:By feeding a row number and two arrays of row names (e.g.: ['procedural1', 'procedural2']) to the twoCols() function, you can make better use of your screen/paper real estate. The details are in the code.

Also, I've added the isAdmin() function that will let you add custom code for specific uses. I've found this to be particularly helpful when I want to test new features on a live form. I can add the test code into a block that is only executed for me thereby preventing other users of the system from watching the development.


Please contact me if you encounter bugs or have additional features you'd like to see in future releases of formWorks!

ben[at]bradleyit.com

Tuesday, September 1, 2009

How to Keep a Geek Happy

Over the last couple weeks I've had the pleasure of working and chatting with several folks that have found bugs in some of my code or have simply expressed their thanks for some of the solutions that I've posted here.

That is the kind of thing that makes this worth it.

Sure I have a hit-counter and visitor statistics to show that people actually are reading some of my stuff. But interacting with the people who find my work valuable is infinitely more rewarding.

So let me say it one more time:
If you have a problem or feature request with any of my code, let me know it. I want to help. The best way to get a hold of me is via email - ben[at]bradleyit.com