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

51 lines
1.3 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl
# 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);