Code Project: Network Map Webpage, Making it Better

I wrote a bit about my Network Map Webpage recently. It’s part of a larger home dashboard project I’m working on, but as part of that I’ve updated things a bit to make them more streamlined and easier to use. The biggest problem with the page as it was originally coded is that it shows everything. I’ve cycled most of my regularly used electronics onto the network so they could be captured by an arp scan, though not all of them are on all the time. For example, I still have a Raspbery Pi and Arduino set up to capture temperature data. I also have several Next Thing CHIP devices, though Next Thing has gone out of business. In total, between my IOT stuff and laptops, phones and tablets and the duplicate IPs from the network extender, I have 55 devices in the raw table.

So I set out to make this more manageable at a glance. My original query in my PHP code looked something like this:

SELECT ip, arpscans.mac, arpscans_known_macs.device_name, arpscans_known_macs.device_description, last_seen, device_owners.user_name FROM arpscans LEFT JOIN arpscans_known_macs on arpscans_known_macs.mac = arpscans.mac LEFT JOIN device_owners on device_owners.id = arpscans_known_macs.device_owner ORDER BY ip

By slipping in “WHERE last_seen >= NOW() – INTERVAL 5 MINUTE” just before ORDER BY, I can make the code return only currently connected devices. The ARP scan runs every 5 minutes, anything that has a last seen time stamp within 5 minutes is assumed to still be attached. This interval could be shorted to almost real time, but I don’t really need that much of a check.

I can also view all disconnected devices with a simple change of the above command, making it “WHERE last_seen <= NOW() – INTERVAL 5 MINUTE”. This wouldn’t work if I were still keeping historical data, but I essentially only capture the last seen data for any device. Essentially what this does is return everything not seen in the last 5 minutes.

I also broke out my PHP code that builds my table from my query into it’s own PHP function. This was I could set the variable $SQL for the active devices, call the function to build the table, then set $SQL for inactive devices and build a second table, under the first.

I immediately scrapped this, because it was ugly. Plus, sometimes I do want to see “everything”.

Enter some GET calls and an if/else statement.

	if($_GET['show'] == "active") {
	// SQL for selecting active devices
	$tabletitle="Active Devices";
	$sql = "SELECT ip, arpscans.mac, arpscans_known_macs.device_name, arpscans_known_macs.device_description, last_seen, device_owners.user_name FROM arpscans LEFT JOIN arpscans_known_macs on arpscans_known_macs.mac = arpscans.mac LEFT JOIN device_owners on device_owners.id = arpscans_known_macs.device_owner WHERE last_seen >= NOW() - INTERVAL 5 MINUTE ORDER BY ip";
	}
	elseif($_GET['show'] == "inactive") {
	// SQL for selecting active devices
	$tabletitle="Inactive Devices";
	$sql = "SELECT ip, arpscans.mac, arpscans_known_macs.device_name, arpscans_known_macs.device_description, last_seen, device_owners.user_name FROM arpscans LEFT JOIN arpscans_known_macs on arpscans_known_macs.mac = arpscans.mac LEFT JOIN device_owners on device_owners.id = arpscans_known_macs.device_owner WHERE last_seen <= NOW() - INTERVAL 5 MINUTE ORDER BY ip";
	}
	else {
	// SQL for Selecting all devices
	$tabletitle="All Devices";
	$sql = "SELECT ip, arpscans.mac, arpscans_known_macs.device_name, arpscans_known_macs.device_description, last_seen, device_owners.user_name FROM arpscans LEFT JOIN arpscans_known_macs on arpscans_known_macs.mac = arpscans.mac LEFT JOIN device_owners on device_owners.id = arpscans_known_macs.device_owner ORDER BY ip";
	}

Basically, if nothing, or a random string is passed by the URL variable “show”, then it goes to the end, and displays everything when accessing the page at index.php. If it passes index.php?show=active, it sets $SQL for showing active devices and if it gets index.php?show=inactive, it shows inactive devices. It also sets a variable called $tabletitle which is just echoed out into some header tags. I then added links across the top of the page to each of these filters.

This allows for a quick and easy toggle of which data is pulled and displayed.

Additionally, I updated the way the Add Device form works. Previously, the form would fill in the MAC, a Device Name and a Device Description, then it would POST to another PHP page that would insert the data into the table, then forward on back to the index page with a header redirect. I’m not going to get into too much detail on it here, but I also integrated the Network Map into my dashboard framework with a header, navigation, sidebar, and footer. It also uses a table based navigation system, so in order to view the network map, I am hitting “index.php?page=4”. Pages basically all need to be wrapped in this structure to work properly, so in order to make things flow better, the Add Device form now POSTs back to the Network Map page itself, which checks to see if the POST variables are set, and if they are, it inserts the new information, before pulling the table.

This also meant slightly altering my page calls to look for “index.php?page=4&show=active” and “index.php?page=4&show=inactive”.

Eventually I want to move the Add Device form to appear at the top of the page, so the whole thing is all handled in one single page.

Lastly, I made up a quick block of code in it’s own page, that simple counts and displays the number of currently connected devices on the network. This block is embedded on the front page of my Dashboard Framework and links to the full Network Map page. The general idea on the Dashboard is to have widgets like this that show quick glance information, with links to detailed information.

I have not built a lot of them yet, but one of the others I have built works somewhat similar to the ARP scanning system. A script makes a call to my TT-RSS instance for each of the segmented accounts I have, then dumps the unread count into a table on the server. The widget shows how many unread articles each topic/account has. I am still really bad about only actually reading the Basic feed (mostly Toys and Video Games).

But I will get into the Dashboard Widgets thing a bit more in a future post probably.

Code Project: Network Map Webpage

I want to start off by saying, there isn’t going to be a ton of code here, and if there is code, it’s going to be super dirty. I’m fairly good at making code for “private use” that is pretty insecure, and not so great at code that’s scrubbed up and user friendly to distribute to others.

I’ve been working a bit on some local code projects, specifically for my little private “Dashboard” that runs on my file server. One project I’ve wanted to try for a while is a dynamic network tracker tool. I’ve looked into some options available, and they all seem to run as a plug in for some complicated 3rd party analytics software that often has some goofy complicated set up procedure that’s beyond “apt-get” or even just dumping a bunch of files in a web server directory.

This project is both kind of simple and not. It was fairly simple in set up and execution, but it’s somewhat complex in design. The first job was getting a list of currently connected devices on the network. This is easily done via the command line with an arp-scan request.

sudo arp-scan --localnet

The output of which looks something like this:

Using a pipe, I can shove all of this into a text file, which contains everything above.

sudo arp-scan --localnet | scan.txt

The trick is, how to display this output on a webpage. One way would be to pull it from a database. Pulling data from MySQL is pretty easy, dumping it to a pretty looking table is also easy. The harder part is getting the output of arp-scan to MySQL in a useful manner.

This is where Python comes into play. I am sure there are other methods or languages available, but like Python, and mostly know how to use Python. Essentially, I wrote a script that would open the file, scan.txt, that was created above. I am only concerned with lines that contain IP addresses, so I used the function “is_number()” to check if the first character of each line is numeric, if it is, it runs through a couple of operations.

Firstly, the output of arp-scan is tab delimited, so I can use the “split” function on “\t”, and dump the result into an array. This gives me an array of the IP address, MAC address, and Manufacturer. This sticks a new line in with the Manufacturer, so I did a “replace” on \n in the third item of the list. Lastly, I wanted the IPs to be uniformly formatted, so I write a little function that would add in leading zeros to the IP octets.

Finally, the Python builds an SQL statement from the line’s list, and make a call to the server to insert the values. A modified version of this code that just displays the resulting SQL commands instead of executing them is below.

#!/usr/bin/python

# Open a file

def is_number(s):
        try:
                float(s)
                return True                                                         except ValueError:
                return False

def format_ip(ipstring):
        octets = ipstring.split(".")
        n=0
        for i in octets:
                while(len(i)<3):
                        i=add_zero(i)
                octets[n]=i
                n=n+1
        return octets[0]+"."+octets[1]+"."+octets[2]+"."+octets[3]
        #return ipstring

def add_zero(shortstring):                                                          return "0"+shortstring


import MySQLdb

mydb = MySQLdb.connect(
  host="localhost",
  user="YOURSQLUSERNAME",
  passwd="YOURSQLPASSWORD",
  database="YOURTARGETDATABASE"
)

mycursor = mydb.cursor()

fo = open("scan.txt", "r")
#print ("Name of the file: ", fo.name)

fo.seek(0)

# read each line of the list
for line in fo:
        #check for lines that contain IP addresses
        if is_number(line[0]):                                                              #Convert lines into list
                line_list = line.split("\t")
                #remove line delimitors
                line_list[2]=line_list[2].replace("\n","")
                #Make IP Octets 3 digits
                line_list[0] = format_ip(line_list[0])
                SQL = "INSERT INTO arpscans (ip, mac, mfg) VALUES ("+line_l$                print SQL                                                   
fo.close()

It’s not super pretty, but it was a quick way to make sure everything came out looking correct. The table I used is called “arpscans” and contains columns called, “ip”, “mac”, “mfg”, and “last_seen”. The time stamp is an automatically generated time stamp.

I then created a shell script that would run the arp-scan piped into scan.txt then runt he python script. I set up this script in the root crontab to run once every half hour. Root is required to run the arp-scan command, so my user crontab wouldn’t cut it. Everything ran fine when I manually did a run of the script using sudo. The PHP on the other end out output the latest values based on the time stamp to a webpage.

This is where I ran into my first major hurdle. The script wasn’t running in cron. After a lot of digging and futzing, I found that basically, when cron runs the script, it works off of different environmental variables. I had to specify in ,y bash file, specifically where each command existed. The end result looks something like this:

#!/usr/bin/env bash
/usr/sbin/arp-scan --localnet > /home/ramen/scripts/arp_sql/scan.txt
/usr/bin/python /home/ramen/scripts/arp_sql/arp_post.py

Eventually the scan was running and posting data automatically as expected. After a few days, I ran into my second major issue. There was simply put, way too much data for my crappy old “server” to handle. The webpage slowed to a crawl as the table contained something like 9000+ entries. It’s possible and likely that my query was also rubbish, but rather than stress more figuring it out, I modified all of the code again.

Instead of adding a new entry for every MAC address every scan, I changed it to check if there already was an entry, and simply update the last_seen time. I had originally designed the system with the idea of getting legacy data for attached devices, but decided I only really cared about a generic history.

The new webpage table now displays all devices, current and previously seen, with the last seen date.

A few issues came up on the output end as well, though none of them were super hard to correct. One, I wanted a way to sort the table by clicking the headers. There are several scripts you can toss in your code to do this online.

I also wanted more data about each device, so I added a form where I could fill in more data about each device. Specifically, the network name, if there was one, a description of what the device is, the User of the device (which family member or if it’s just a network device). This also checks and updates based on MAC address.

I also ran into an issue with MAC addresses and my Network extender. When devices are connected to the Network Extender, the first half of the MAC is replaced with the first part of the Extender’s MAC, though they retain the last half. I may eventually write some code to detect and merge these entries, but for now, I’ve simply been labeling them in the description as “(Extender)”, so I know it’s the same device on the other connection.

The final end result looks something like this:

I used to have the network super organized before I moved, but the new router doesn’t work nicely with my Pi DHCP server, so I have not gotten things quite as nicely sorted as I would like. Everything in the picture is sorted, but above .100, it’s a mess. I also can’t assign IPs to some devices at all, like the DirecTV gear or my Amazon Echos, which is really annoying.

One of my future projects will hopefully correct this, as I want to put a second router on the network with DD-WRT, between the ISP gateway and everything else.

Overall, it’s been a fun little exercise in coding that combined a lot different techniques together in a fun way.

Building A Cross Platform App with Xamarin

So, I made an app.  A for real, runs on things app.  More accurately, I followed a little tutorial to make an app, as part of the Xamarin Challenge over at Thurrott.com.  The app itself is a little weather app, it even includes location based weather and forecasting.  I know this isn’t particularly impressive but I think it’s pretty neat.

Part of the point of the exercise is that it show off the cross platform ability of Xamarin as a development plaform.  The end app uses the same code and runs on Windows 10, iOS and Android.  Unfortunately, I don’t own a MAC or an iOS device to test the iOS code but I was able to run both the Android and Windows 10 versions of the App.  I even ran the Android APK on my Fire Tablet.

I can’t say I learned a ton about how to actually make apps using Xamarin, though I plan to poke through the code provided more later.  What I learned more of was how to trouble shoot Visual Studio, which seems to be a bit more than buggy.  There is a forum set up for people looking for assistance on this contest and there are a lot of issues that all seem unrelated to each other but all related to issues with Visual Studio.

For example, I had issues getting the location based weather to work, until I went through and updated the Android Emulator files and build an emulator that ran on Android 7.  In a later step I found several of the NuGet packages weren’t installing properly, I never really figured out why but I ended up having to add them individually to each of the app platforms rather than the blanket “Install this on all platforms” system.

In the end, I did manage to get through and the app says everything was submitted and accepted.

 

Programming (Part 2)

I promised a part two so here it is…

The first post can be found here.

As previously mentioned, my previous self taught programming experience involved BASIC and whatever the TI-85 uses.  I had my first official taste of taught programming in College a couple of years after High School with C.  My Engineering degree required I take “Programming for Engineers” which was basically C programming to solve iterative math problems.  Most of the programs we did were by the book style involving arrays and graph style equations.  The book for the course was actually two books and we only got through maybe one of them since Engineers aren’t computer programmers.

This was fun but at the time I was rather busy with school so I didn’t to doo much beyond what was required for class.  A couple semesters later however was different.  I finished my Associates in 2.5 years meaning I finished in December.  I wanted to start the Bachelor’s track in the Fall since it makes all of the class schedules work out better so I had a semester to kill.  So in addition to working a whole bunch, I took two classes just for fun, Basic electronics and Computer Science 101.

CS101 was essentially C++ Programming for beginners.  There are some keys here.  I’ve had experience with C, which is very similar to C++, especially at this level of programming.  Also The TI-85 language is reasonably similar to C at this level of programming.  Basically, I’ve had a decent amount of experience at this.  The end result was that I excelled in this class, I did better than everyone else (most of which were actual Computer Science Majors).  The teacher also set up a side class for whomever wanted to attend learning some visual windows based elements.

The fun was in the larger assignments though.  Most of the quick assignments didn’t leave much room for creativity.  “Mr. Shopkeeper needs a program that will calculate 7% sales tax for his sales.  Make a program that takes the total bill in and outputs the total with tax” doesn’t leave much room for expansion.

The larger assignments were all simple games with variable elements that allowed for much more fun.  We also got more time to work on these, maybe a week or two.  I could easily code the basic assignment in a day leaving me, well, a week or two, to code “extras”.

This is where I created my first three “real games”.  Note the scare quotes.  Truth is, these are all rather buggy as evidenced by the fact that they don’t like unexpected input and are generally pretty crummy.

So I present to you, Tug of War, Pearl Diver, and Let’s Win at the Races.  All of these titles were given out by the class as was the basic premise of the game.

The parts that I added was anything graphical, even if it is ASCII graphics.  The requirement was only for a text based input and output.  Ok, yeah, it’s pretty simple, big deal.

I’ve done a few other projects that never got completed, the most ambitious was a PC based sequel to my previously mentioned Dragon Quest series.  This one was somewhat Zork like with a text based interface only it added a one on one random battle system like Final Fantasy and a level up system similar to Final Fantasy 2 where skills build as you use them and class is based on your skills.  I got as far as completing the map (without descriptions).  So you can wander around an empty world in it’s current state.  One day I hope to get back to it.

My other self taught programming project involves HTML, if you want to call it programming.  A better word is probably “coding”.  Back in 1998 when I first started building webpages I started in MS Frontpage.  I noticed there was a lot of flack for people who used Frontpage so I bought this big fat HTML book (HTML Complete, $20, great value).  So I taught myself basic coding of HTML.  These days I don’t use this skill as much since blogs make things much much easier but this skill does come in hand a lot when I’m trying to manually tweak my WordPress templates.

Programming is something I enjoy when I have time though my main complaint is always finding a decent free compiler.  The only free C++ complier I’ve ever found was by Borland and it is absolutely TERRIBLE for user friendlyness.  I plan to do some additional programming in the near future for fun it’s mostly a matter of finding time.  I’d love tog et into more visual element and make actual graphics and program that run in Windows.  Also there are quite a few more modern languages out there now.

Anyway, if I build anything new, you’ll be surely seeing it here.

Programming (Part 1)

Hey, it’s pointless personal trivia day!  Hooray.

Anyway, I’ve never taken up computer programming in any official “this is what I’m going to do for work” sort of level but it’s off and on been a background hobby of mine.  I’ve been considering getting back into it lately which has inspired this historical blog post.

The first programming language I ever learned any part of was BASIC.  This was around 1987-1988 when I was 7 or 8 years old.  I know this because, at the time, my dad had gone back to school to finish his degree in Computer Science and I learned a bit of Basic from him based on what he was learning at the time in college.  I never used it for anything super elaborate.  The most complicated piece of code I ever created involved a simple password that would then display a bunch of text or ask questions that would display different text based on the user’s response.  Simple “If Else” style programming.

This “If Else” method of programming is very simple but it’s a set up that can be used for a lot if managed properly.  It’s a method that would translate over tot he next segment of my programming years in High School.  Because I was in Advance Math, I was required to own a fancy TI-85 Graphic Calculator.  The most exciting thing any of us ever did on these calculators was to program little games and programs.

I had two major programming projects I created for the TI-85 and irritatingly, both have been completely lost to the throws of time.  I didn’t get a PC link cable until well after High School so I had no effective way to back up my hard work.  The first and main undertaking I ever had was a pair of games called “Dragon Quest”.

The name Dragon Quest was chosen in tribute to Dragon Warrior, before I knew Dragon Warrior was in fact also secretly called Dragon Quest in Japan.  The first game was a very simple RPG, all contained in one saved file.  You would exist at the town most of the game.  You could buy potions to heal in this town or stay at the inn to heal.  You also ha the option to “explore”.  Exploring would result in one of two encounters, a small dragon or a wizard.  Everything did a set amount of damage so the whole game was extremely predictable.  After 20 battles, exploring would result in an encounter with the “Dragon Lord” who was a much tougher dragon.

The battles were the shinning point in this game.  It took a ton of memory but the battles were all graphical.  You’d see a wizard, dragon, or Dragon Lord graphic on screen to indicate the type of encounter.  Your two attacks, slash and stab, would overlay a different graphic depending on which was used.  These were basic black and white images created in the calculator’s Graph Drawing function.

Many people liked this game in my High School and it was generally pretty distributed among people who had graphic calculators.

So I created a sequel.  The sequel dropped all graphics in the interest of saving memory.  However, this memory saving allowed for a TON more features.  The player could now learn 4 different spells over town, a fireball, healing, nuke and better healing.  You could also spend money from encounters on new weapons to do more damage.  All damage was now variable. The encounters were much more varied with two dozen types of enemies that would randomly show up.  Halfway through you’d be given the option to solve a series of riddles and fight the mighty White Dragon in order to earn the best Sword, Xcalubur.  Instead of 20 encounters the number was increased to 50.  There was also the addition of a menu driven system since I had figured out how to work this function.  Lastly, since 50 battles was a lot to make it through, the game featured a three file save system that would write everything to a file.

The next best feature, which was highly underlying and not visible to the player, was that the game was broken up into a series of modular files.  I had learned how to call one file within another.  This meant that the main game was in fact, nothing but a series of calls to other files.  It also meant one part could be much more easily tweaked as needed.

The most impressive part of all this really was that I’d never had any level of education in how the TI-85 was supposed to be programmed.  I had the manual, which described the available functions and commands, and the rest as all trial and error or luck.

I mentioned that I had created two major projects.  Dragon Quest was the first, the second was “Windows for the TI-85”.  At least that’s what I called it.  It wasn’t really very Windowsish.  Basically I created a menu based graphical icon based system for launching the programs on my Calculator.  Unfortunately the icons were part of a static background and the menus were all hard coded so it wasn’t portable to anyone else’s calculators.  It was more of an experiment than anything.

Next… Part 2: The HTML, C, and C++ Years….