• About Binary Impulse
  • BashBytes
Binary Impulse

Category Archives: How-to’s

Tutorials and step-by-step instructions.

Sample debug output code for BASH/SHELL

July 5, 2013 10:41 AM / Leave a Comment / Dan

When trying to debug your own scripts it can be difficult to see exactly what you want.  Pages of white text scrolling across the screen.  Here is a quick flexible function that can make life a whole lot easier by setting different debug values and instead of using echo, use the function name.

    #################################################
    # Variable Declarations
    #
    #What output to show on screen
    #debug levels:
    # 0 is no debug or logging.
    # 1 is basic output only (gree text)
    # 2 is basic output plus basic debug  (adds yellow text)
    # 3 is show everything (adds red text)
    debuglevel=2
     
    #log to file
    log=0
     
    #where the logfile is located
    logfile="/dev/null"
     
    #################################################
    #  functions
    output(){
            if [ $debuglevel -gt "0" ] && [ $1 = "1" ];
            then
                    /bin/echo -e "`/bin/date +"%m-%d-%Y %r"`:\e[00;32m $2\e[00m"
            fi
            if [ $debuglevel -gt "1" ] && [ $1 = "2" ];
            then
                    /bin/echo -e "`/bin/date +"%m-%d-%Y %r"`:\e[01;33m $2\e[00m"
            fi
            if [ $debuglevel -gt "2" ] && [ $1 = "3" ];
            then
                    /bin/echo -e "`/bin/date +"%m-%d-%Y %r"`:\e[00;31m $2\e[00m"
            fi
            if [ $log -eq "1" ];
            then
                    /bin/echo `/bin/date +"%m-%d-%Y %r"` :  $@ >> $logfile
            fi
    }

    #################################################
    # Code
    #
    output "1" "this is normal output"
    output "2" "this is basic debug output"
    output "3" "this is everything output"

Copy this into a basic script and run it, adjust the debuglevel variable at the top to see different output.  I know this will help me in the future and hope it will help others as well.

Posted in: How-To's, Other / Tagged: bash, debug, echo, script, scripting, shell

Safely Removing Gnome-Keyring From Xubuntu 12.04

June 5, 2013 4:31 PM / 5 Comments / Kirk Schnable

EDIT: Your results may vary, I am getting some feedback about this solution indicating which it may not be entirely effective, or may not work any more.

In previous Linux deployments, we have had problems with people’s keyring passwords being forgotten or not working, to the point where the universal response if you ask anyone what to do when the Gnome Keyring prompt comes up, it’s “oh, just hit cancel”.

In the latest image, we have decided to remove gnome-keyring.  You would think you would be able to do this very easily.

sudo apt-get remove gnome-keyring

But in reality, this command is dangerous, and threatens to remove xubuntu-desktop.

The following packages will be REMOVED:
  gnome-keyring oneconf python-ubuntu-sso-client seahorse software-center
  ubuntu-sso-client ubuntu-sso-client-gtk xubuntu-desktop
0 upgraded, 0 newly installed, 8 to remove and 0 not upgraded.
After this operation, 11.5 MB disk space will be freed.
Do you want to continue [Y/n]?

So, I did a bit of Googling, and I found this thread on Ask Ubuntu. They suggested installing aptitude and using aptitude to remove gnome-keyring, because they believe aptitude’s dependency tree is different.  However, this solution does not seem to work.

After doing some of my own digging, I found that under “Session and Startup” settings, there is an option called “Launch GNOME services on startup” listed under Compatibility.

GNOME Compatibility XFCE

Mousing over this option shows that disabling it will prevent Gnome Keyring from launching.  This will do away with our Gnome Keyring for good!

Posted in: How-To's, Musings / Tagged: Gnome, gnome-keyring, Linux, Precise, XFCE, Xubuntu

User Tracking and Logon Agents

May 4, 2013 3:06 PM / Leave a Comment / Dan

We believe that tracking when users log into a computer is very good information.  We can easily look up where students log in, track computer usage, aid with helpdesk tasks, etc.  Our primary focus was to show that computer labs and carts are being used, and justify the 1:1 initiative we set forth.

This is was not a difficult task to setup but could be useful to others, so here it is!

First we need a good way to get data from the clients.  With Linux, we could do it a multitude of ways, with windows, it can be a bit limited.  We chose to do our data submission with a simple URL get.  Simple, easy, fast, reliable.  With some reading, it wasn’t too difficult to setup with Windows.

First… Server recording
We need a simple script to take variables and insert them into mysql with the needed information. Really all we need is the computer name and username.  We can use the server time and get the client’s IP from the server so that we know it is consistent.  Here is the server script:

<?php
// Written by Kirk Schnable for Marengo Community High School
// July 20th, 2011

/****************** SAMPLE SYNTAX ********************/
/* http://server/login.php?u=USERNAME&h=COMPUTERNAME */
/*****************************************************/

//# MySQL Connection
$link = mysql_connect("MYSQLSERVER", "DB", "PASSWORD") or die(mysql_error());

//# Gather Information
$user = mysql_real_escape_string($_GET['u']);
$hostname = mysql_real_escape_string($_GET['h']);
$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$date = date("Y-m-d H:i:s");

//# Query Database
$result = mysql_query("INSERT INTO `iplookup`.`login_records` (`LogonTime` ,`IP`, `Hostname` ,`User`)VALUES ('$date', '$ip', '$hostname', '$user');");
if(!$result){ /* do something on failure of mysql query */ }

?> 

MySQL Database Structure
Using the server side script we can see who the IP was of the user submitting the record, time stamp that event, see who the user is, and what the hostname of the computer is.
loa

Linux Logon Agent
The Linux logon agent could be written many different ways using several programs; curl, wget, lynx, etc.  Simply put it needs to pull two variables and curl that URL…  This needs to be put in the users (and/or skel’s) startup so that it runs at logon   This could be done in a single command like:

curl -s "http://SERVER/logon-agent/login.php?u=$USER&h=$HOSTNAME"

Windows Logon Agent
Unfortunately windows does not have awesome tools like wget, curl, lynx, etc so it is a bit more in-depth to get it working.  However it is easier to deploy using group policy on the server, you can deploy this script pretty easily. Put this script in the User Logon scripts:

' DECLARE ENVIRONMENT VARIABLES
dim URL

' GET SYSTEM VARIABLES
Set wshShell = CreateObject( "WScript.Shell" )
username = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
computer = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

' SET URL TO CONTACT
URL = "http://SERVER/logon-agent/login.php?u=" & username & "&h=" & computer

' ECHO VARIABLES FOR TESTING
'WScript.Echo URL
'WScript.Echo username
'WScript.Echo computer

on error resume next  
Set objXML = CreateObject("MSXML2.ServerXMLHTTP")  

if err then  
	msgbox("Error: " & err.description)  
	wscript.quit 1  
end if  

' Call the remote machine the request  
objXML.open "GET", URL, False  

objXML.send()  

' return the response  
'msgbox objXML.responSetext  

' clean up  
Set objXML = Nothing

Searching For Data!
This is a simple search web tool we created to locate user/computer/IP history.

<head>
<title>Realtime User Login Lookup</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<div style="text-align: center;">
<span id="header">Computer Login Database</span>
<form method="post" action="">
<span>Lookup IP: </span><input id="ip" type="text" name="ip" value="<?php echo($_GET['ip']); ?>" />
<br/>
<span>Lookup Host: </span><input id="hostname" type="text" name="hostname" value="<?php echo($_GET['host']); ?>" />
<br/>
<span>Lookup User: </span><input id="user" type="text" name="user" value="<?php echo($_GET['user']); ?>" />
<br/>
<span>Records: </span><input id="records" type="text" name="records" value="10000" />
<br/>
<input type="submit" value="Go" />
</form>
</div>

<?php if($_POST){
$link = mysql_connect("SERVER", "USER", "PASSWORD") or die(mysql_error());
$user = mysql_real_escape_string($_POST['user']);
$hostname = mysql_real_escape_string($_POST['hostname']);
$ip = mysql_real_escape_string($_POST['ip']);
$records = mysql_real_escape_string(intval($_POST['records']));
$standarddate = 'l, F jS, Y \a\t g:ia';

if($ip){ // Lookup IP
	echo("<hr/>");
	echo('<div style="text-align: center;">');
	$rdate = mysql_query("SELECT DISTINCT CAST(`LogonTime` AS DATE) AS dateonly FROM `iplookup`.`login_records` WHERE `IP`='$ip'") or die(mysql_error());
	echo("<p><span>This computer has been logged into on " . mysql_num_rows($rdate) . " different days.</span></p>");
	$r = mysql_query("SELECT * FROM `iplookup`.`login_records` WHERE `IP`='$ip' ORDER BY `LogonTime` DESC LIMIT $records") or die(mysql_error());
	if(mysql_num_rows($r) == 0){ echo("<p><span>No Results for IP <b>$ip</b></span></p>"); }
	elseif(mysql_num_rows($r) == $records){ echo("<p><span>Showing the $records most recent logins on <b>$ip</b></span></p>"); }
	else{ echo("<p><span>Showing all " . mysql_num_rows($r) . " previously recorded logins on <b>$ip</b></span></p>"); }
	while($row = mysql_fetch_assoc($r)){
		$date = $row['LogonTime']; $date = strtotime($date); $date = date($standarddate, $date); $username = $row['User']; $host = $row['Hostname'];
		echo("<span><p><b>$username</b> @$host on $date</span></p>");
	}
	echo('</div>');
}

if($user){ // Lookup Username
	echo("<hr/>");
	echo('<div style="text-align: center;">');
	$rdate = mysql_query("SELECT DISTINCT CAST(`LogonTime` AS DATE) AS dateonly FROM `iplookup`.`login_records` WHERE `User`='$user'") or die(mysql_error());
	echo("<p><span>This user has logged in on " . mysql_num_rows($rdate) . " different days.</span></p>");
	$r = mysql_query("SELECT * FROM `iplookup`.`login_records` WHERE `User`='$user' ORDER BY `LogonTime` DESC LIMIT $records") or die(mysql_error());
	if(mysql_num_rows($r) == 0){ echo("<p><span>No Results for User <b>$user</b></span></p>"); }
	elseif(mysql_num_rows($r) == $records){ echo("<p><span>Showing the $records most recent IPs of <b>$user</b></span></p>"); }
	else{ echo("<p><span>Showing all " . mysql_num_rows($r) . " previously recorded IPs of <b>$user</b></span></p>"); }
	while($row = mysql_fetch_assoc($r)){
		$date = $row['LogonTime']; $date = strtotime($date); $date = date($standarddate, $date); $ip = $row['IP']; $host = $row['Hostname'];
		echo("<span><p><b>$ip</b> ($host) on $date</span></p>");
	}
	echo('</div>');
}

if($hostname){ // Lookup Hostname
	echo("<hr/>");
	echo('<div style="text-align: center;">');
	$rdate = mysql_query("SELECT DISTINCT CAST(`LogonTime` AS DATE) AS dateonly FROM `iplookup`.`login_records` WHERE `Hostname` LIKE '%$hostname%'") or die(mysql_error());
	echo("<p><span>This computer has been logged into on " . mysql_num_rows($rdate) . " different days.</span></p>");
	$r = mysql_query("SELECT * FROM `iplookup`.`login_records` WHERE `Hostname` LIKE '%$hostname%' ORDER BY `LogonTime` DESC LIMIT $records") or die(mysql_error());
	if(mysql_num_rows($r) == 0){ echo("<p><span>No Results for Hostname <b>$hostname</b></span></p>"); }
	elseif(mysql_num_rows($r) == $records){ echo("<p><span>Showing the $records most recent logins on <b>$hostname</b></span></p>"); }
	else{ echo("<p><span>Showing all " . mysql_num_rows($r) . " previously recorded logins on <b>$hostname</b></span></p>"); }
	while($row = mysql_fetch_assoc($r)){
		$date = $row['LogonTime']; $date = strtotime($date); $date = date($standarddate, $date); $ip = $row['IP']; $username = $row['User'];
		echo("<span><p><b>$username</b> ($ip) on $date</span></p>");
	}
	echo('</div>');
}

if(!$hostname && !$user && !$ip){ // Show "all" records.
        echo("<hr/>");
        echo('<div style="text-align: center;">');
        $r = mysql_query("SELECT * FROM `iplookup`.`login_records` ORDER BY `LogonTime` DESC LIMIT $records") or die(mysql_error());
        if(mysql_num_rows($r) == 0){ echo("<p><span>No Results.</span></p>"); }
        elseif(mysql_num_rows($r) == $records){ echo("<p><span>Showing the $records most recent logins.</span></p>"); }
        else{ echo("<p><span>Showing all " . mysql_num_rows($r) . " previously recorded logins.</span></p>"); }
        while($row = mysql_fetch_assoc($r)){
                $date = $row['LogonTime']; $date = strtotime($date); $date = date($standarddate, $date); $ip = $row['IP']; $username = $row['User']; $hostnames = $row['Hostname'];
                echo("<span><p><b>$username</b> ($ip) ($hostnames) on $date</span></p>");
        }
        echo('</div>');
}

}
?>

Its complimentary style.css:

BODY{ background-color: maroon; }
SPAN{ color: white; }
SPAN#header{ font-weight: bold; }

INPUT#ip{ width: 175px; }
INPUT#hostname{ width: 150px; }
INPUT#user{ width: 150px; }
INPUT#records{ width: 50px; }

Custom Reports…
Once you have this information you should be able to run reports however you see fit.  We have created a highly custom report for our use which is specific to our school that shows computer usage.  That code is not ready to be posted at this time however here is a glimpse of what could be done.  Please note that ltcart1,2204, and 2506 have been physically relocated to other rooms but because database entries exists, they show up as 0.
report

With this report we can see that with 30 calendar days, there have been 22 instructional days.  This shows that these rooms are using computers almost every day!

 

Posted in: How-To's / Tagged: curl, Linux, logon, report, tracking, user, Windows

Receiving RSS Updates Via Email

March 25, 2013 6:55 PM / 2 Comments / Kirk Schnable

Rss_Shiny_Icon.svg

If you’re like me, you rely on your email for everything in your life.  My email delivers my latest Facebook notifications, Nagios warnings, text messages (thanks to Google Voice), and oh I guess email too.

I have push email notifications on my mobile devices, as do a lot of people.

Some websites offer RSS feeds of information which is important to you, but don’t offer an option to receive an email.

Yes, you could set this up for any RSS feed, but this quick guide will focus on setting up email notifications for Reddit comment responses and Reddit messages.

Another interesting application for this might be to receive emails when your favorite Podcasts are updated, or if you want to get an email when your favorite blog posts a new update.

Reddit is a very real-time social website, and having instant notifications of events on Reddit could be very useful, however since Reddit doesn’t even require an email address to register, they’re not offering the functionality.

 

What You’ll Need:

  • An RSS feed to subscribe to.
  • An email address to send the notifications to.
  • A Linux server which is always online, which you can create cron jobs on.

 

Let’s Begin
This is the only part of this guide which requires root access to the server.  We will be using RSS2Email, which is a free program available for Linux.  If you are using Ubuntu, it is right in the repositories for you.

If you do not have root access to your server, but you do have SSH access, it may still be possible to use this program if you compile it yourself.  You can get the source code here.

sudo apt-get install rss2email

 

Setting Up RSS2Email
The setup is mind-numbingly easy!  If your email address is you@example.com, and your RSS feed link is http://www.reddit.com/message/inbox/.rss?feed=123foo456bar789&user=MyAwesomeUsername, you could use this syntax to set up your feed.

r2e new you@example.com
r2e add http://www.reddit.com/message/inbox/.rss?feed=123foo456bar789\&user=MyAwesomeUsername
r2e run

Important: Reddit RSS feeds for your Inbox, etc, have an ampersand (&) before your username.  If you do not escape this ampersand by putting a slash in front of it (\&) your feed will not work!

On The First Run, you may receive a high volume of email.  In fact, it was enough to cause my sendmail server to exceed its rate limit and throw a ton of email to the deferred queue.  But, it did eventually all send.  A lot of the first emails will be old RSS data you have already seen before.  After that, on subsequent runs, you will only receive updates.

 

Where Can I Find My Reddit Inbox Feed Link?
If you login to Reddit, and go to Preferences, you’ll find an “RSS Feeds” on the top of the page.  The direct link is https://ssl.reddit.com/prefs/feeds/.

 

Using RSS2Email
You can very easily add and delete feeds from your R2E configuration.

Add new feeds the same way you added the first one:  r2e add http://www.feedwebsite.com/feed

Delete old feeds by running r2e list, then r2e delete # where # is the number corresponding to the feed you want to delete.

 

Scheduling RSS2Email
Obviously, you will only receive updates when r2e is run manually.  So, an easy way to receive routine updates is to cron the command.

crontab -e

To receive updates every 10 minutes, add a line like this to your crontab.

*/10 *  * * * /usr/bin/r2e run

To receive updates every 1 minute, add a line like this to your crontab.

* *  * * * /usr/bin/r2e run

 

You’re Done!
You should now receive emails at your designated time intervals when the RSS feed has new updates!

Posted in: How-To's, Musings / Tagged: crontab, email, reddit, rss

Web filter testing…

February 27, 2013 4:27 PM / Leave a Comment / Dan

We have been having filtering issues at work.  The only pattern I can see is that upon heavy Internet use (40+Mb) the filtering services have a hard time keeping up.  The problem is that Internet usage is impossible to predict so calling support and trying to reproduce the problem is nearly impossible.  Assuming I can get through at the time it is happening, by the time I update the support personnel on the other end of the phone, it usually starts working again.

Instead of fighting this problem further I came up with a quick script to see how often it really isn’t working, and record my results.

A script that loops though and hits a blocked page every ‘X’ seconds and then parses the HTML results to see if the redirect or block page was served.  This should show my reliability of our web filter during the day without detrimenting any network performance.  I whipped together a quick and dirty script with some functions from my other scripts.  It should do the trick.

#!/bin/bash
#######################################################
# Script created to test web filter's reliability
# It polls a webpage that is supposed to be blocked.
#
#                               Dan Kane
#######################################################

# Web filter Server IP or unique text found in the block page html
WebServer="10.9.1"

# just zeroing the counters
blockedcounter=0

# just zeroing the counters
allowedcounter=0

# Log file location
logfile="/var/log/webfilter.log"

# Blocked url
blockurl="facebook.com"

# used for my output module to show debug information
debug="1"

# used for my output module to log everything
log="1"

# sleep through the loop or go as fast as we can?
sleep="1"

#######################################################
#FUNCTIONS
#

#used for debug and logging
output(){
        if [ $debug -eq "1" ];
        then
                /bin/echo `/bin/date +"%m-%d-%Y %r"`:  $@
        fi
        if [ $log -eq "1" ];
        then
                /bin/echo `/bin/date +"%m-%d-%Y %r"` :  $@ >> $logfile
        fi
}

onexit(){
        echo "didnt work $allowedcounter   :::   worked $blockedcounter"; exit
        exit
}

#######################################################
# CODE
#

# catch traps and show results
trap "onexit" SIGINT > /dev/null

# Enter our 1 second loop
while :
do
        website=$(wget -qO- $blockurl)
        found=$(echo $website | grep $WebServer | wc -l)
        if [ $found -lt 1 ];
        then
                allowedcounter=$(( $allowedcounter + 1 ))
                output "Page NOT blocked  ---  didnt work $allowedcounter   :::   worked $blockedcounter"
        else
                blockedcounter=$(( $blockedcounter + 1 ))
                output "Page blocked  ---   didnt work $allowedcounter   :::   worked $blockedcounter"
        fi
        if [ $sleep -eq "1" ];
        then
                sleep 1
        fi
done

We will start recording the results and see what else we can find out.  If the amount of data we are inserting is too much, maybe insert results into MySQL is in order, but we will see what we find first.

Posted in: How-To's / Tagged: scripts, testing, web filter, web fitler testing

Post Navigation

← Older Posts
Newer Posts →

Pages

  • About Binary Impulse
  • BashBytes

Recent Posts

  • My UBook X Linux Tablet Adventure and how I learned more than I ever thought I would need to know about accelerometer drivers
  • Migrating Proxmox Hypervisor’s Boot Volume ZFS Mirror To New (Smaller) Disks
  • OPNsense Performance Tuning for Multi-Gigabit Internet
  • Reddit Deplatforms Popular Microsoft Software Swap Subreddit
  • Comcast Upgrades Gigabit Pro from 3Gbps to 6Gbps!
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Apr    

Recent Comments

  • jasonistre on OPNsense Performance Tuning for Multi-Gigabit Internet
  • Nick on OPNsense Performance Tuning for Multi-Gigabit Internet
  • Kirk Schnable on OPNsense Performance Tuning for Multi-Gigabit Internet
  • nick on OPNsense Performance Tuning for Multi-Gigabit Internet
  • Eric on My UBook X Linux Tablet Adventure and how I learned more than I ever thought I would need to know about accelerometer drivers

Categories

  • How-To's
  • Humorous
  • Musings
  • Newsworthy
  • Other

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© Copyright 2025 - Binary Impulse
Infinity Theme by DesignCoral / WordPress