aredn/files/usr/local/bin/olsrd-namechange

72 lines
2.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl
=for commnet
Part of BBHN Mesh -- Used for creating Amateur Radio friendly mesh networks
Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
Copyright (c) 2013 David Rivenburg et al. BroadBand-HamNet
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut
# do nothing if olsrd is not running
exit if system "ps | grep -v grep | grep -q 'olsrd '";
($uptime) = `cat /proc/uptime` =~ /^(\d+)/;
# load the hosts file
foreach(`cat /var/run/hosts_olsr 2>/dev/null`)
{
next unless /^\d/;
($ip, $name, $junk, $originator, $mid) = split /\s+/, $_;
next unless $originator;
next if $originator eq "myself";
next unless ($ip eq $originator) or (defined $mid and $mid eq "(mid");
if($hosts{$ip}) { $hosts{$ip} .= "/$name" }
else { $hosts{$ip} = $name }
}
# find the current neighbors
foreach(`echo /links | nc 127.0.0.1 2006 2>/dev/null`)
{
next unless /^\d/;
($my_ip, $ip) = split /\s+/, $_;
$history{$ip}{age} = $uptime;
$history{$ip}{name} = $hosts{$ip};
}
exit unless keys %history;
# load and strip the current history
foreach(`cat /tmp/node.history 2>/dev/null`)
{
chomp;
($ip, $age, $name) = split / /, $_;
next unless $age;
next if $history{$ip};
next if $uptime - $age > 86400;
$name = "" unless $name;
$history{$ip}{age} = $age;
$history{$ip}{name} = $name;
}
# write the new history
open(FILE, ">/tmp/node.history") or exit;
foreach $ip (keys %history)
{
printf FILE "%s %s %s\n", $ip, $history{$ip}{age}, $history{$ip}{name};
}
close(FILE);