Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkwinkelmann committed May 14, 2015
0 parents commit 26e7b1f
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
nbproject/private
*~
network.json
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Clark Winkelmann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

32 changes: 32 additions & 0 deletions README.md
@@ -0,0 +1,32 @@
# Simple Network Scanner

This network scanner displays a list of the known and unknown devices on your LAN in a very simplist web page generated via PHP.

Known devices are stored in a json file. The `arp-scan` command is used behind the scene to scan for running hosts.

## How to use

Install the `arp-scan` and `php5` packages. On Ubuntu you can use:

sudo apt-get install arp-scan php5

Copy/rename `network.example.json` to `network.json` and add your known devices.
You can start with an empty file and add mac adresses after they appear in the "unknown" area of the GUI.

cp network.example.json network.json

Start the script in the PHP development server.
Root is required by the `arp-scan` command.

sudo php -S localhost:8000 server.php

or simply use the provided script (will prompt for your password):

./start.sh

You can then access it in your browser by using the address given when starting the server.

Note that a new scan is performed each time you refresh the page.
Also note that `arp-scan` does not always get a response from all hosts, causing them to be sometimes up or down.

Currently the host performing the scan is not shown as up because the arp-scan does not return it.
7 changes: 7 additions & 0 deletions nbproject/project.properties
@@ -0,0 +1,7 @@
include.path=${php.global.include.path}
php.version=PHP_55
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=false
web.root=.
9 changes: 9 additions & 0 deletions nbproject/project.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>Simple Network Scanner</name>
</data>
</configuration>
</project>
26 changes: 26 additions & 0 deletions network.example.json
@@ -0,0 +1,26 @@
{
"devices": [{
"name": "Router",
"type": "router",
"model": "Motorola Router",
"interfaces": [{
"type": "eth",
"mac": "ab:cd:ef:01:23:45",
"ip": {
"type": "static",
"value": "192.168.1.1"
}
}]
},{
"name": "Super PC",
"type": "pc",
"model": "ASUS Barebone",
"interfaces": [{
"type": "eth",
"mac": "ab:cd:ef:01:23:45",
"ip": {
"type": "dhcp"
}
}]
}]
}
175 changes: 175 additions & 0 deletions server.php
@@ -0,0 +1,175 @@
<?php

/**
* Simple Network Scanner
* (c) 2015 Clark Winkelmann
* MIT licensed
*
* HOW TO USE
*
* Start this file in the PHP development server as root (required by arp-scan)
* sudo php -S localhost:8000 server.php
*/

// Launch an ARP-Scan on the local subnet
// Must be run as root
$arp_scan_raw = shell_exec('arp-scan --localnet');

// Get lines as an array
$arp_scan = explode("\n", $arp_scan_raw);

// Will contain matching fields in the regexp
$matches = [];

// Will contain all found interfaces in a mac-indexed array
$found_interfaces = [];

// Scan results
foreach($arp_scan as $scan) {
$matches = []; // reset

// Parse output lines
if(preg_match('/^([0-9\.]+)[[:space:]]+([0-9a-f:]+)[[:space:]]+(.+)$/', $scan, $matches) !== 1) {
// Ignore lines that don't contain results
continue;
}

$ip = $matches[1];
$mac = $matches[2];
$desc = $matches[3];

$found_interfaces[$mac] = [
'ip' => $ip,
'desc' => $desc,
'known' => false, // Will be changed by the loop
];
}

// Read network data from file
$network = json_decode(file_get_contents('network.json'), true);

// Loop trough all described interfaces to mark them up or down
foreach($network['devices'] as $devicekey => $device) {
foreach($device['interfaces'] as $interfacekey => $interface) {
// Is this interface up ?
$up = false;
// IP of the interface (will remain null if neither defined or up)
$ipData = $network['devices'][$devicekey]['interfaces'][$interfacekey]['ip'];
$ip = array_key_exists('value', $ipData) ? $ipData['value'] : null;
$mac = $interface['mac'];

// If the interface is up
if(array_key_exists($mac, $found_interfaces)) {
$up = true;
$ip = $found_interfaces[$mac]['ip'];
$found_interfaces[$mac]['known'] = true;
}

// Save values for display
$network['devices'][$devicekey]['interfaces'][$interfacekey]['up'] = $up;
$network['devices'][$devicekey]['interfaces'][$interfacekey]['ip']['value'] = $ip;
}
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Simple Network Scanner</title>

<style>
* {
box-sizing: border-box;
}

body {
font-family: Arial, sans-serif;
color: #333;
}

.container {
width: 960px;
max-width: 100%;
margin: 0 auto;
}

.device {
background: #dedede;
margin: 0.5em 0;
padding: 0.5em 2em;
display: inline-block;
vertical-align: top;
width: 33%;
font-size: 0.9em;
}

.device.up {
background: #66e658;
}

.device.down {
background: #f7b4bd;
}

.device .name {
font-weight: bold;
font-size: 1.2em;
}
</style>
</head>
<body>
<div class="container">
<h1>Simple Network Scanner</h1>
<h2>Unknown devices</h2>
<div class="devices">
<?php foreach($found_interfaces as $mac => $interface):
if($interface['known']) {
// Here we only display unknown interfaces
continue;
}
?>
<div class="device">
<div class="name"><?= $interface['desc'] ?></div>
<div class="interfaces">
<div class="interface">
<div class="mac"><?= $mac ?></div>
<div class="ip"><?= $interface['ip'] ?></div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<h2>Network</h2>
<div class="devices">
<?php foreach($network['devices'] as $device):
// Is this host up ?
$up = false;
foreach($device['interfaces'] as $interface) {
if($interface['up']) {
$up = true;
break; // The host is considered up if at least one interface is up
}
}
?>
<div class="device <?= $up ? 'up' : 'down' ?>">
<div class="name"><?= $device['name'] ?></div>
<div class="type"><?= $device['type'] ?></div>
<div class="interfaces">
<?php foreach($device['interfaces'] as $interface): ?>
<div class="interface">
<div class="mac"><?= $interface['mac'] ?></div>
<div class="ip"><?= $interface['ip']['value'] ?></div>
<div class="state"><?= $interface['up'] ? 'Up' : 'Down' ?></div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions start.sh
@@ -0,0 +1,3 @@
#!/bin/bash

sudo php -S localhost:8000 server.php

0 comments on commit 26e7b1f

Please sign in to comment.