Skip to content

Instantly share code, notes, and snippets.

@tylerszabo
Created February 24, 2022 04:32
Show Gist options
  • Save tylerszabo/3759a7802ffeb16f3ecf0c77dfbf0f0f to your computer and use it in GitHub Desktop.
Save tylerszabo/3759a7802ffeb16f3ecf0c77dfbf0f0f to your computer and use it in GitHub Desktop.
Get IPv6 address for MAC on PFSense
<?php
/* ip_list.php
*
* Copyright (c) 2022 Tyler Szabo
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Example Usage: https://localhost/ip_list.php?mac=00:09:8c:00:69:63
*/
define('NDP_COMMAND', '/usr/sbin/ndp -n -a');
define('REGEX_GLOBAL_IPV6_IP', '[23][[:xdigit:]][[:xdigit:]][[:xdigit:]]:[[:xdigit:]:]*');
define('REGEX_MAC_ADDR', '[[:xdigit:]:]*');
define('QS_MAC', 'mac');
header("Content-type: text/plain");
$valid_client = false;
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == "127.0.0.1" or $ip == "::1") {
$valid_client = true;
}
if (!$valid_client) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
echo "403 Forbidden\n";
exit;
}
$gen_date = gmdate("D, j M Y H:i:s") . " GMT";
header("Last-Modified: " . $gen_date);
exec(NDP_COMMAND, $ndp_output);
$mac = $_GET[QS_MAC];
echo "## IPv6 neighbors for " . $mac . "\n";
echo "## Generated " . $gen_date . "\n";
foreach ($ndp_output as $line) {
//echo "# " . $line . "\n";
preg_match('/^(?P<ipv6>'.REGEX_GLOBAL_IPV6_IP.') (?P<mac>'.REGEX_MAC_ADDR.')/', $line, $matches);
if ($matches['mac'] == $mac) {
echo $matches['ipv6'] . "\n";
}
}
echo "## End\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment